can scheduled callback on core( )->CallOnMainThread( ) be cancelled?

68 views
Skip to first unread message

Yong

unread,
Nov 18, 2014, 12:25:04 AM11/18/14
to native-cli...@googlegroups.com
Hi there, 
say if I scheduled a callback by CAllOnMainThread (delay, callback), suppose the callback will be fired after the delay, but what if I don't need this callback anymore before the delay timeout, is there a way I can cancel this scheduled callback? 
Or I have to implement some intelligence inside the callback therefore it will figure out whether just do a no-op or carry on the planned action?

thanks
-Yong

Ben Smith

unread,
Nov 18, 2014, 1:42:14 PM11/18/14
to native-cli...@googlegroups.com
Hi Yong,

As far as I know this functionality does not exist, so you'll have to do it yourself. If you do this a lot, you could wrap pp::CompletionCallback and add a bool (untested):

class MyCallback : public pp::CompletionCallback {
 public:
  MyCallback(PP_CompletionCallback_Func func, void* user_data) :
    pp::CompletionCallback(&MyCallback::RunThunk, this),
    real_func_(func),
    real_user_data_(user_data),
    cancelled_(false) {}

  void Cancel() { cancelled_ = true; }

 private:
  static void RunThunk(void* user_data, int32_t* result) {
    MyCallback* cb = static_cast<MyCallback*>(user_data);
    if (cb->cancelled_) { return; }
    (*real_func_)(real_user_data_, result);
  }

  PP_CompletionCallback_Func real_func_;
  void* real_user_data_;
  bool cancelled_;
};

Yong Kang

unread,
Nov 18, 2014, 2:33:51 PM11/18/14
to native-cli...@googlegroups.com
Thanks for the reply Ben!
If this is the case, I would maintain a state inside the callback and check the state to decide if the callback just doing nothing and return , or continue its planned action. 


I do have another question regarding to pp::CompletionCallback. 

1. when using it with pp::API(…, callback),
    in this case,  CallOnMainThread (delay, callback),  usually the ‘callback’ is create by:

  callback = CompletionCallbackFactory::NewCallback(&my_method).

    do I need to do something to delete this ‘callback’ object,  
    after ‘my_method’ being called, or the pp::API(…,callback) should take care of it already?
    in another word,  for me as a user,  I just need ‘new' the callback, pass it into pp:API(…, callback), then don’t worry free the memory at all?

2. I want to use pp:CompletionCallback by my own 'Class A' to provide a public method for other 'Class B' to use, by pass-in a ‘method' to be called after Class A did something. 

here is the example:

Class A {
public:
… …
int offered_service (...,  pp::CompletionCallback &cb) {
a_cb = cb;
… ...
}

~A () {

/* do I need to delete ‘a_cb’ when this object be freed???*/
}

private:
… ...
pp:CompletionCallback *a_cb; // Class A remember the CB pointer, will call CB->run() after service done.
… …
void do_service (…) {
… ...
a_cb->Run(result);  
}
}

say another 'class B' register a method by:
 
a->offered_service (…,   B_cb_Factory.NewCallback(&B::method));

my question is, I guess 2 ways to free CB memory:

(1), Class B can free the memory by calling B_cb_Factory.CancelAll()
(2). Class A can delete ‘a_cb’ since it has the pointer to the CB.

did I get it wrong? and,
what’s the best practice to take care deleting the CompletionCallback object?

thanks alot
-Yong






--
You received this message because you are subscribed to a topic in the Google Groups "Native-Client-Discuss" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/native-client-discuss/kSJqrXhpasY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to native-client-di...@googlegroups.com.
To post to this group, send email to native-cli...@googlegroups.com.
Visit this group at http://groups.google.com/group/native-client-discuss.
For more options, visit https://groups.google.com/d/optout.

Ben Smith

unread,
Nov 18, 2014, 7:40:53 PM11/18/14
to native-cli...@googlegroups.com
On Tuesday, November 18, 2014 11:33:51 AM UTC-8, Yong wrote:
Thanks for the reply Ben!
If this is the case, I would maintain a state inside the callback and check the state to decide if the callback just doing nothing and return , or continue its planned action. 


I do have another question regarding to pp::CompletionCallback. 

1. when using it with pp::API(…, callback),
    in this case,  CallOnMainThread (delay, callback),  usually the ‘callback’ is create by:

  callback = CompletionCallbackFactory::NewCallback(&my_method).

    do I need to do something to delete this ‘callback’ object,  
    after ‘my_method’ being called, or the pp::API(…,callback) should take care of it already?
    in another word,  for me as a user,  I just need ‘new' the callback, pass it into pp:API(…, callback), then don’t worry free the memory at all?

It looks like the memory is automatically cleaned up when the callback is called when using the completion callback factory. See the documentation here.

 

2. I want to use pp:CompletionCallback by my own 'Class A' to provide a public method for other 'Class B' to use, by pass-in a ‘method' to be called after Class A did something. 

IMO, you'd be better off using a different library for this; CompletionCallbacks are useful for interacting with PPAPI, but don't provide nearly as much functionality as something like
 

here is the example:

Class A {
public:
… …
int offered_service (...,  pp::CompletionCallback &cb) {
a_cb = cb;
… ...
}

~A () {

/* do I need to delete ‘a_cb’ when this object be freed???*/
}

private:
… ...
pp:CompletionCallback *a_cb; // Class A remember the CB pointer, will call CB->run() after service done.
… …
void do_service (…) {
… ...
a_cb->Run(result);  
}
}

say another 'class B' register a method by:
 
a->offered_service (…,   B_cb_Factory.NewCallback(&B::method));

my question is, I guess 2 ways to free CB memory:

(1), Class B can free the memory by calling B_cb_Factory.CancelAll()
(2). Class A can delete ‘a_cb’ since it has the pointer to the CB.

did I get it wrong? and,
what’s the best practice to take care deleting the CompletionCallback object?


I'd suggest not using a pointer; CompletionCallbacks are really just wrappers around PP_CompletionCallback, so they can be copied freely:

class A {
 public:
  int offered_service(..., const pp::CompletionCallback& cb) {
    a_cb = cb;
  }

 private:
  pp::CompletionCallback a_cb;

  void do_service(...) {
    a_cb.Run(result);
  }
};
  
thanks alot
-Yong






To unsubscribe from this group and all its topics, send an email to native-client-discuss+unsub...@googlegroups.com.

m.abum...@gmail.com

unread,
Aug 28, 2016, 1:05:10 PM8/28/16
to Native-Client-Discuss, Mohammad Abu Musa
Hi both, 

Can you please share with your final result? I am stuck a bit with this point. 

I am building an audio recording based on the audio_media_stream example I want to stop the scheduler when I stop the recording 

Yong

unread,
Sep 6, 2016, 2:36:18 PM9/6/16
to Native-Client-Discuss, m.ab...@gmail.com
Opps, just saw your question. 
my implementation maintain a STATE variable which will be checked inside the callback, this STATE will tell the callback if any action need to be taken, or schedule another callback.
if the whole class was deleted while the callback(implemented as a method of the class) was scheduled, I didn't see anything wrong. 
so in your case, if stop recording, you might just delete the whole class. 
Best,
-Yong
Reply all
Reply to author
Forward
0 new messages