[Asterisk-bsd] How to implement locking in channel drivers

Hans Petter Selasky hselasky at c2i.net
Mon Aug 8 16:18:03 CDT 2005


On Monday 08 August 2005 17:24, Soren Telfer wrote:
> Use your own mutex, and don't rely on the channel mutex for anything.

Yes, but the problem is that I cannot hold my own mutex while calling into 
Asterisk, hence one would get reverse locking order. But I can hold 
"(ast_channel)->lock" while before calling into Asterisk.

This is how I have implemented it:

ast_hangup():
 lock(ch->lock);
 pvt->hangup();
   lock(my_private_lock);
   unlock(my_private_lock);
 unlock(ch->lock);

Now I cannot have my lock locked while calling ast_hangup().

my_driver_disconnect():
 lock(my_private_lock);
 release private calldescriptor for this call
 unlock(my_private_lock);

 // What if the CPU sleeps here and the other
 // connected party disconnects at the same time.
 //
 // Then you get two threads calling
 // ast_hangup() at the same time, and the
 // result is unpredictable.
 //

 ast_hangup();


>
> Did you get an answer on the dev list?

No.

>
> Asterisk has a way of making things disappear.  For instance, why oh
> why does asterisk do this?
>
>    if (chan->tech_pvt) {
>                 ast_log(LOG_WARNING, "Channel '%s' may not have been
> hung up pr\operly\n", chan->name);
>                 free(chan->tech_pvt);
>         }

Yes, but there is no check to verify that the "chan" pointer is still valid, 
and that the memory pointed to by "chan" has not been freed.

--HPS


More information about the Asterisk-BSD mailing list