[asterisk-dev] Locking, coding guidelines addition
Hans Petter Selasky
hselasky at c2i.net
Mon Jun 30 08:56:24 CDT 2008
Hi,
I think this same issue was discussed some years ago.
Extract from my solution:
/*
* LOCKING RULES
* =============
*
* This channel driver uses several locks. One must be
* careful not to reverse the locking order, which will
* lead to a so called deadlock. Here is the locking order
* that must be followed:
*
* struct call_desc *cd;
* struct cc_capi_application *p_app;
*
...
*
* 2. cc_mutex_lock(&modlock); (See Asterisk source code)
*
* 3. cc_mutex_lock(&chlock); (See Asterisk source code)
*
* 4. cc_mutex_lock(&cd->pbx_chan->lock); **
*
* 5. cc_mutex_lock(&p_app->lock);
*
* 6. cc_mutex_lock(&capi_global_lock);
...
*
* ** the PBX will call the callback functions with
* this lock locked. This lock protects the
* structure pointed to by 'cd->pbx_chan'. Also note
* that calling some PBX functions will lock
* this lock!
*/
Rule #1: You cannot call any "ast_xxx()" functions with any private channel
driver locks locked. You need to handle any races when you pickup your locks
again.
Rule #2: In all Asterisk callbacks you have to lookup your softc in a list
from the ast_channel pointer! Never access any private channel structures
before you know that the channel is still there!
If you follow these two simple rules you will be safe from trouble!
--HPS
Reference:
http://www.selasky.org/hans_petter/capi4pbx/sources/chan_capi.c
More information about the asterisk-dev
mailing list