Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r57936 - trunk/boost/thread/win32
From: anthony_at_[hidden]
Date: 2009-11-26 03:13:02


Author: anthonyw
Date: 2009-11-26 03:13:01 EST (Thu, 26 Nov 2009)
New Revision: 57936
URL: http://svn.boost.org/trac/boost/changeset/57936

Log:
Don't use timed_lock to do a lock
Text files modified:
   trunk/boost/thread/win32/basic_timed_mutex.hpp | 67 +++++++++++++++++++++++++++++----------
   1 files changed, 49 insertions(+), 18 deletions(-)

Modified: trunk/boost/thread/win32/basic_timed_mutex.hpp
==============================================================================
--- trunk/boost/thread/win32/basic_timed_mutex.hpp (original)
+++ trunk/boost/thread/win32/basic_timed_mutex.hpp 2009-11-26 03:13:01 EST (Thu, 26 Nov 2009)
@@ -61,15 +61,30 @@
             
             void lock()
             {
- BOOST_VERIFY(timed_lock(::boost::detail::get_system_time_sentinel()));
- }
- bool timed_lock(::boost::system_time const& wait_until)
- {
- if(!win32::interlocked_bit_test_and_set(&active_count,lock_flag_bit))
+ if(try_lock())
                 {
- return true;
+ return;
                 }
                 long old_count=active_count;
+ mark_waiting_and_try_lock(old_count);
+
+ if(old_count&lock_flag_value)
+ {
+ bool lock_acquired=false;
+ void* const sem=get_event();
+
+ do
+ {
+ BOOST_VERIFY(win32::WaitForSingleObject(
+ sem,::boost::detail::win32::infinite)==0);
+ clear_waiting_and_try_lock(old_count);
+ lock_acquired=!(old_count&lock_flag_value);
+ }
+ while(!lock_acquired);
+ }
+ }
+ void mark_waiting_and_try_lock(long& old_count)
+ {
                 for(;;)
                 {
                     long const new_count=(old_count&lock_flag_value)?(old_count+1):(old_count|lock_flag_value);
@@ -80,6 +95,33 @@
                     }
                     old_count=current;
                 }
+ }
+
+ void clear_waiting_and_try_lock(long& old_count)
+ {
+ old_count&=~lock_flag_value;
+ old_count|=event_set_flag_value;
+ for(;;)
+ {
+ long const new_count=((old_count&lock_flag_value)?old_count:((old_count-1)|lock_flag_value))&~event_set_flag_value;
+ long const current=BOOST_INTERLOCKED_COMPARE_EXCHANGE(&active_count,new_count,old_count);
+ if(current==old_count)
+ {
+ break;
+ }
+ old_count=current;
+ }
+ }
+
+
+ bool timed_lock(::boost::system_time const& wait_until)
+ {
+ if(try_lock())
+ {
+ return true;
+ }
+ long old_count=active_count;
+ mark_waiting_and_try_lock(old_count);
 
                 if(old_count&lock_flag_value)
                 {
@@ -93,18 +135,7 @@
                             BOOST_INTERLOCKED_DECREMENT(&active_count);
                             return false;
                         }
- old_count&=~lock_flag_value;
- old_count|=event_set_flag_value;
- for(;;)
- {
- long const new_count=((old_count&lock_flag_value)?old_count:((old_count-1)|lock_flag_value))&~event_set_flag_value;
- long const current=BOOST_INTERLOCKED_COMPARE_EXCHANGE(&active_count,new_count,old_count);
- if(current==old_count)
- {
- break;
- }
- old_count=current;
- }
+ clear_waiting_and_try_lock(old_count);
                         lock_acquired=!(old_count&lock_flag_value);
                     }
                     while(!lock_acquired);


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk