[asterisk-dev] Scheduler and event callbacks - what thread?

Russell Bryant russell at russellbryant.net
Tue Nov 1 18:12:24 CDT 2011


On Tue, Nov 1, 2011 at 6:48 PM, Ian Pilcher <arequipeno at gmail.com> wrote:
> I'm back with another thread-related question.
>
> chan_sip uses at least two types of callbacks -- an event callback
> registered with ast_event_subscribe() and a scheduler callback
> registered with ast_sched_add().  The two callback functions are using
> a shared static variable to communicate state.
>
> Pseudo-code:
>
>  static int event_sched_id = -1;
>
>  static void event_callback(...)
>  {
>      if (event_sched_id == -1) {
>          event_sched_id = ast_sched_add(..., sched_callback, ...);
>  }
>
>  static int sched_callback(...)
>  {
>      event_sched_id == -1;
>      ...
>  }
>
> Is there anything ensures that these two callbacks will always be called
> from the same thread?  If not, then the shared variable needs a mutex.
>
> Even if they are always executed in the same thread, am I correct in
> thinking that there's a possibility of the thread being interrupted by
> an event or scheduled action -- in which case the shared variable needs
> to be volatile and atomic?

They will always be called by different threads.  The event callbacks
come from the event dispatch thread (see main/event.c).  The scheduler
callbacks are done in the do_monitor thread in chan_sip.  Locking is
needed for any data shared between them.

-- 
Russell Bryant



More information about the asterisk-dev mailing list