[Freeswitch-dev] upgrade a rdlock to wrlock

Seven Du dujinfang at gmail.com
Fri Aug 19 02:16:40 MSD 2011



On Friday, August 19, 2011 at 1:35 AM, Christopher Rienzo wrote:

> Even if that works, what happens when two threads that have a read lock attempt to switch to a write lock simultaneously?  How does the thread know it got the write lock first and the data is unchanged?
> 
> 
maybe try_get_write_lock and let the latter one failed ? no idea. see following comments.

> 
>  On Thu, Aug 18, 2011 at 12:20 PM, Anthony Minessale <anthony.minessale at gmail.com (mailto:anthony.minessale at gmail.com)> wrote:
> >  can't you just write lock it right away?

I think there's a potential race when two threads try to find and launch new listeners respectively. So I try to add a find_or_create_listener() to avoid race. It usually only need to create once unless 1) at the first time, 2) the node is broken so need a reconnect


http://pastebin.freeswitch.org/17091 ( I moved some codes, so just look for find_or_create_listener() )


So my concern is - isn't it expensive for a write lock than a read lock? Do you have other suggestions on this? or should I just get a write lock on not found and simply find again?

I'm not sure if this is related to http://jira.freeswitch.org/browse/FS-3432  but want to fix the potential race first.

let me know if you want to attach the patch to jira but it's not the final patch still need some further work.

Thanks.
> > 
> >  On Thu, Aug 18, 2011 at 7:19 AM, Christopher Rienzo <cmrienzo at gmail.com (mailto:cmrienzo at gmail.com)> wrote:
> > > I forgot to mention that you'd have to repeat the find operation once you
> > > get the write lock since there is a small window between the unlock and lock
> > > where another thread may have created the item.
> > >
> > >
> > > On Thu, Aug 18, 2011 at 6:42 AM, Christopher Rienzo <cmrienzo at gmail.com (mailto:cmrienzo at gmail.com)>
> > > wrote:
> > >>
> > >> Unlock the read lock and then lock as a write lock.
> > >>
> > >> On Aug 17, 2011 8:20 PM, "Seven Du" <dujinfang at gmail.com (mailto:dujinfang at gmail.com)> wrote:
> > >>
> > >> Hi,
> > >>
> > >>
> > >> Just wondering how to upgrade a read lock to a write lock, say I do a
> > >> find_or_create_item_in_queue()
> > >> switch_thread_rwlock_rdlock(lock);
> > >> find an item from queue, if not found, create one
> > >> switch_thread_rwlock_upgrade_to_wrlock(lock);
> > >> write
> > >> switch_thread_rwlock_unlock(lock);
> > >> Thanks
> > >> --
> > >> Seven Du
> > >> About: http://about.me/dujinfang
> > >> Blog: http://www.dujinfang.com
> > >> Proj: http://www.freeswitch.org.cn
> > >> Sent with Sparrow
> > >>
> > >>
> > >> _______________________________________________
> > >> Join us at ClueCon 2011, Aug 9-11, Chicago
> > >> http://www.cluecon.com 877-7-4ACLUE
> > >>
> > >> FreeSWITCH-dev mailing list
> > >> FreeSWITCH-dev at lists.freeswitch.org (mailto:FreeSWITCH-dev at lists.freeswitch.org)
> > >> http://lists.freeswitch.org/mailman/listinfo/freeswitch-dev
> > >> UNSUBSCRIBE:http://lists.freeswitch.org/mailman/options/freeswitch-dev
> > >> http://www.freeswitch.org
> > >>
> > >
> > >
> > > _______________________________________________
> > > Join us at ClueCon 2011, Aug 9-11, Chicago
> > > http://www.cluecon.com 877-7-4ACLUE
> > >
> > > FreeSWITCH-dev mailing list
> > > FreeSWITCH-dev at lists.freeswitch.org (mailto:FreeSWITCH-dev at lists.freeswitch.org)
> > > http://lists.freeswitch.org/mailman/listinfo/freeswitch-dev
> > > UNSUBSCRIBE:http://lists.freeswitch.org/mailman/options/freeswitch-dev
> > > http://www.freeswitch.org
> > >
> > >
> > 
> > 
> > 
> > --
> >  Anthony Minessale II
> > 
> >  FreeSWITCH http://www.freeswitch.org/
> >  ClueCon http://www.cluecon.com/
> >  Twitter: http://twitter.com/FreeSWITCH_wire
> > 
> >  AIM: anthm
> > MSN:anthony_minessale at hotmail.com (mailto:MSN%3Aanthony_minessale at hotmail.com)
> >  GTALK/JABBER/PAYPAL:anthony.minessale at gmail.com (mailto:PAYPAL%3Aanthony.minessale at gmail.com)
> >  IRC: irc.freenode.net (http://irc.freenode.net) #freeswitch
> > 
> >  FreeSWITCH Developer Conference
> > sip:888 at conference.freeswitch.org (mailto:sip%3A888 at conference.freeswitch.org)
> > googletalk:conf+888 at conference.freeswitch.org (mailto:googletalk%3Aconf%2B888 at conference.freeswitch.org)
> >  pstn:+19193869900 (tel:%2B19193869900)
> > 
> >  _______________________________________________
> >  Join us at ClueCon 2011, Aug 9-11, Chicago
> > http://www.cluecon.com 877-7-4ACLUE
> > 
> >  FreeSWITCH-dev mailing list
> > FreeSWITCH-dev at lists.freeswitch.org (mailto:FreeSWITCH-dev at lists.freeswitch.org)
> > http://lists.freeswitch.org/mailman/listinfo/freeswitch-dev
> >  UNSUBSCRIBE:http://lists.freeswitch.org/mailman/options/freeswitch-dev
> > http://www.freeswitch.org
> 
> _______________________________________________
> Join us at ClueCon 2011, Aug 9-11, Chicago
> http://www.cluecon.com 877-7-4ACLUE
> 
> FreeSWITCH-dev mailing list
> FreeSWITCH-dev at lists.freeswitch.org (mailto:FreeSWITCH-dev at lists.freeswitch.org)
> http://lists.freeswitch.org/mailman/listinfo/freeswitch-dev
> UNSUBSCRIBE:http://lists.freeswitch.org/mailman/options/freeswitch-dev
> http://www.freeswitch.org

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.freeswitch.org/pipermail/freeswitch-dev/attachments/20110819/7828ece6/attachment.html 


Join us at ClueCon 2011 Aug 9-11, 2011
More information about the FreeSWITCH-dev mailing list