[asterisk-dev] Is this locking sequence correct?

Russell Bryant russell at digium.com
Thu Feb 3 03:03:33 CST 2011

----- Original Message -----
> I am probably missing something about locking. Here is a trimmed down
> piece of code from chan_sip.c:
> if (p->owner) {
> struct ast_channel *owner = p->owner;
> ast_channel_ref(owner);
> sip_pvt_unlock(p);
> ast_channel_unlock(owner);
> /* ... Do something interesting here ... */
> ast_channel_unref(owner);
> /* Oops, or not? */
> sip_pvt_lock(p);
> }
> My question is about the 2 last calls. After ast_channel_unref(), the
> channel is neither locked nor referenced. Can the channel then just go
> away such that sip_pvt_lock() might be too late to call already? Is it
> more correct to call sip_pvt_lock() before ast_channel_unref(), or am
> I just misunderstanding the semantics? Is it guaranteed here that
> ast_channel_unref() does not unref the last reference?

You are not guaranteed that the channel unref was not the last reference.  It may be, but there is nothing in what you have shown that makes that a problem.  Just because the channel goes away does not mean that the sip_pvt goes away.  The destruction of each is independent of each other.

Russell Bryant
Digium, Inc.  |  Engineering Manager, Open Source Software
445 Jan Davis Drive NW   -    Huntsville, AL 35806  -  USA
jabber: rbryant at digium.com    -=-    skype: russell-bryant
www.digium.com -=- www.asterisk.org -=- blogs.asterisk.org

More information about the asterisk-dev mailing list