|
Boost-Commit : |
From: anthony_at_[hidden]
Date: 2008-07-01 12:04:51
Author: anthonyw
Date: 2008-07-01 12:04:51 EDT (Tue, 01 Jul 2008)
New Revision: 46956
URL: http://svn.boost.org/trac/boost/changeset/46956
Log:
Partial fix for issue #1867 - ensure boost::shared_mutex supports try_lock
Text files modified:
trunk/boost/thread/win32/shared_mutex.hpp | 183 +++++++++------------------------------
1 files changed, 42 insertions(+), 141 deletions(-)
Modified: trunk/boost/thread/win32/shared_mutex.hpp
==============================================================================
--- trunk/boost/thread/win32/shared_mutex.hpp (original)
+++ trunk/boost/thread/win32/shared_mutex.hpp 2008-07-01 12:04:51 EDT (Tue, 01 Jul 2008)
@@ -91,7 +91,7 @@
bool try_lock_shared()
{
state_data old_state=state;
- do
+ for(;;)
{
state_data new_state=old_state;
if(!new_state.exclusive && !new_state.exclusive_waiting_blocked)
@@ -106,14 +106,6 @@
}
old_state=current_state;
}
-#ifdef BOOST_MSVC
-#pragma warning(push)
-#pragma warning(disable:4127)
-#endif
- while(true);
-#ifdef BOOST_MSVC
-#pragma warning(pop)
-#endif
return !(old_state.exclusive| old_state.exclusive_waiting_blocked);
}
@@ -130,17 +122,10 @@
bool timed_lock_shared(boost::system_time const& wait_until)
{
-#ifdef BOOST_MSVC
-#pragma warning(push)
-#pragma warning(disable:4127)
-#endif
- while(true)
-#ifdef BOOST_MSVC
-#pragma warning(pop)
-#endif
+ for(;;)
{
state_data old_state=state;
- do
+ for(;;)
{
state_data new_state=old_state;
if(new_state.exclusive || new_state.exclusive_waiting_blocked)
@@ -159,14 +144,6 @@
}
old_state=current_state;
}
-#ifdef BOOST_MSVC
-#pragma warning(push)
-#pragma warning(disable:4127)
-#endif
- while(true);
-#ifdef BOOST_MSVC
-#pragma warning(pop)
-#endif
if(!(old_state.exclusive| old_state.exclusive_waiting_blocked))
{
@@ -176,7 +153,7 @@
unsigned long const res=detail::win32::WaitForSingleObject(unlock_sem,::boost::detail::get_milliseconds_until(wait_until));
if(res==detail::win32::timeout)
{
- do
+ for(;;)
{
state_data new_state=old_state;
if(new_state.exclusive || new_state.exclusive_waiting_blocked)
@@ -198,14 +175,6 @@
}
old_state=current_state;
}
-#ifdef BOOST_MSVC
-#pragma warning(push)
-#pragma warning(disable:4127)
-#endif
- while(true);
-#ifdef BOOST_MSVC
-#pragma warning(pop)
-#endif
if(!(old_state.exclusive| old_state.exclusive_waiting_blocked))
{
@@ -221,7 +190,7 @@
void unlock_shared()
{
state_data old_state=state;
- do
+ for(;;)
{
state_data new_state=old_state;
bool const last_reader=!--new_state.shared_count;
@@ -262,14 +231,6 @@
}
old_state=current_state;
}
-#ifdef BOOST_MSVC
-#pragma warning(push)
-#pragma warning(disable:4127)
-#endif
- while(true);
-#ifdef BOOST_MSVC
-#pragma warning(pop)
-#endif
}
void lock()
@@ -283,20 +244,39 @@
return timed_lock(get_system_time()+relative_time);
}
+ bool try_lock()
+ {
+ state_data old_state=state;
+ for(;;)
+ {
+ state_data new_state=old_state;
+ if(new_state.shared_count || new_state.exclusive)
+ {
+ return false;
+ }
+ else
+ {
+ new_state.exclusive=true;
+ }
+
+ state_data const current_state=interlocked_compare_exchange(&state,new_state,old_state);
+ if(current_state==old_state)
+ {
+ break;
+ }
+ old_state=current_state;
+ }
+ return true;
+ }
+
+
bool timed_lock(boost::system_time const& wait_until)
{
-#ifdef BOOST_MSVC
-#pragma warning(push)
-#pragma warning(disable:4127)
-#endif
- while(true)
-#ifdef BOOST_MSVC
-#pragma warning(pop)
-#endif
+ for(;;)
{
state_data old_state=state;
- do
+ for(;;)
{
state_data new_state=old_state;
if(new_state.shared_count || new_state.exclusive)
@@ -316,14 +296,6 @@
}
old_state=current_state;
}
-#ifdef BOOST_MSVC
-#pragma warning(push)
-#pragma warning(disable:4127)
-#endif
- while(true);
-#ifdef BOOST_MSVC
-#pragma warning(pop)
-#endif
if(!old_state.shared_count && !old_state.exclusive)
{
@@ -332,7 +304,7 @@
unsigned long const wait_res=detail::win32::WaitForMultipleObjects(2,semaphores,true,::boost::detail::get_milliseconds_until(wait_until));
if(wait_res==detail::win32::timeout)
{
- do
+ for(;;)
{
state_data new_state=old_state;
if(new_state.shared_count || new_state.exclusive)
@@ -357,14 +329,6 @@
}
old_state=current_state;
}
-#ifdef BOOST_MSVC
-#pragma warning(push)
-#pragma warning(disable:4127)
-#endif
- while(true);
-#ifdef BOOST_MSVC
-#pragma warning(pop)
-#endif
if(!old_state.shared_count && !old_state.exclusive)
{
return true;
@@ -378,7 +342,7 @@
void unlock()
{
state_data old_state=state;
- do
+ for(;;)
{
state_data new_state=old_state;
new_state.exclusive=false;
@@ -396,30 +360,15 @@
}
old_state=current_state;
}
-#ifdef BOOST_MSVC
-#pragma warning(push)
-#pragma warning(disable:4127)
-#endif
- while(true);
-#ifdef BOOST_MSVC
-#pragma warning(pop)
-#endif
release_waiters(old_state);
}
void lock_upgrade()
{
-#ifdef BOOST_MSVC
-#pragma warning(push)
-#pragma warning(disable:4127)
-#endif
- while(true)
-#ifdef BOOST_MSVC
-#pragma warning(pop)
-#endif
+ for(;;)
{
state_data old_state=state;
- do
+ for(;;)
{
state_data new_state=old_state;
if(new_state.exclusive || new_state.exclusive_waiting_blocked || new_state.upgrade)
@@ -439,14 +388,6 @@
}
old_state=current_state;
}
-#ifdef BOOST_MSVC
-#pragma warning(push)
-#pragma warning(disable:4127)
-#endif
- while(true);
-#ifdef BOOST_MSVC
-#pragma warning(pop)
-#endif
if(!(old_state.exclusive|| old_state.exclusive_waiting_blocked|| old_state.upgrade))
{
@@ -460,7 +401,7 @@
void unlock_upgrade()
{
state_data old_state=state;
- do
+ for(;;)
{
state_data new_state=old_state;
new_state.upgrade=false;
@@ -487,20 +428,12 @@
}
old_state=current_state;
}
-#ifdef BOOST_MSVC
-#pragma warning(push)
-#pragma warning(disable:4127)
-#endif
- while(true);
-#ifdef BOOST_MSVC
-#pragma warning(pop)
-#endif
}
void unlock_upgrade_and_lock()
{
state_data old_state=state;
- do
+ for(;;)
{
state_data new_state=old_state;
bool const last_reader=!--new_state.shared_count;
@@ -522,20 +455,12 @@
}
old_state=current_state;
}
-#ifdef BOOST_MSVC
-#pragma warning(push)
-#pragma warning(disable:4127)
-#endif
- while(true);
-#ifdef BOOST_MSVC
-#pragma warning(pop)
-#endif
}
void unlock_and_lock_upgrade()
{
state_data old_state=state;
- do
+ for(;;)
{
state_data new_state=old_state;
new_state.exclusive=false;
@@ -555,21 +480,13 @@
}
old_state=current_state;
}
-#ifdef BOOST_MSVC
-#pragma warning(push)
-#pragma warning(disable:4127)
-#endif
- while(true);
-#ifdef BOOST_MSVC
-#pragma warning(pop)
-#endif
release_waiters(old_state);
}
void unlock_and_lock_shared()
{
state_data old_state=state;
- do
+ for(;;)
{
state_data new_state=old_state;
new_state.exclusive=false;
@@ -588,21 +505,13 @@
}
old_state=current_state;
}
-#ifdef BOOST_MSVC
-#pragma warning(push)
-#pragma warning(disable:4127)
-#endif
- while(true);
-#ifdef BOOST_MSVC
-#pragma warning(pop)
-#endif
release_waiters(old_state);
}
void unlock_upgrade_and_lock_shared()
{
state_data old_state=state;
- do
+ for(;;)
{
state_data new_state=old_state;
new_state.upgrade=false;
@@ -620,14 +529,6 @@
}
old_state=current_state;
}
-#ifdef BOOST_MSVC
-#pragma warning(push)
-#pragma warning(disable:4127)
-#endif
- while(true);
-#ifdef BOOST_MSVC
-#pragma warning(pop)
-#endif
release_waiters(old_state);
}
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