I am writing an Asterisk module. When a certain SIP request is received, I want to wait until a certain condition is triggered, and only after it finish (max 5 seconds), should Asterisk's core functions be allowed to process this request further.
I tried creating a pj_thread_t
where my long-running task runs, and there is a loop that runs multiple times with a delay (pj_thread_sleep
) until the expected condition is satisfied. However, this blocks all SIP message handling in Asterisk and not just the one I'm handling.
Is using a pj_thread_t
not the right approach?
Update: I missed out a detail in my original post. I was waiting for a semaphor in the main thread. The new thread wasn't actually blocking when calling pj_thread_sleep
. However, the problem still remains. I need to return PJ_FALSE
from the main thread after the new thread finishes executing, so that the rest of the modules can continue processing the request after I'm done with it.
You should not(I will repeat, SHOULD NOT) do any threaded logic like that in the core processing thread. It will result in very hard-catching issues with core and logic. Your RTP processing thread should be as light as possible.
The best option is to do it outside asterisk at all, in external logic. Using AMI, ARI or FastAGI.
If you still want do some backgroundround checks, see the source code of Background or Read applications. Another waiting and quite complex example is AMD app. Anyway, it should be done in application, not RTP processing.