Boost logo

Boost-Commit :

From: pdimov_at_[hidden]
Date: 2008-04-09 13:49:21


Author: pdimov
Date: 2008-04-09 13:49:20 EDT (Wed, 09 Apr 2008)
New Revision: 44132
URL: http://svn.boost.org/trac/boost/changeset/44132

Log:
Proper try_lock semantics.
Text files modified:
   trunk/boost/detail/spinlock_nt.hpp | 28 +++++++++++++++++++++++++---
   1 files changed, 25 insertions(+), 3 deletions(-)

Modified: trunk/boost/detail/spinlock_nt.hpp
==============================================================================
--- trunk/boost/detail/spinlock_nt.hpp (original)
+++ trunk/boost/detail/spinlock_nt.hpp 2008-04-09 13:49:20 EDT (Wed, 09 Apr 2008)
@@ -15,6 +15,8 @@
 // http://www.boost.org/LICENSE_1_0.txt)
 //
 
+#include <boost/assert.hpp>
+
 namespace boost
 {
 
@@ -25,17 +27,33 @@
 {
 public:
 
+ bool locked_;
+
+public:
+
     inline bool try_lock()
     {
- return true;
+ if( locked_ )
+ {
+ return false;
+ }
+ else
+ {
+ locked_ = true;
+ return true;
+ }
     }
 
     inline void lock()
     {
+ BOOST_ASSERT( !locked_ );
+ locked_ = true;
     }
 
     inline void unlock()
     {
+ BOOST_ASSERT( locked_ );
+ locked_ = false;
     }
 
 public:
@@ -44,17 +62,21 @@
     {
     private:
 
+ spinlock & sp_;
+
         scoped_lock( scoped_lock const & );
         scoped_lock & operator=( scoped_lock const & );
 
     public:
 
- explicit scoped_lock( spinlock & /*sp*/ )
+ explicit scoped_lock( spinlock & sp ): sp_( sp )
         {
+ sp.lock();
         }
 
         ~scoped_lock()
         {
+ sp_.unlock();
         }
     };
 };
@@ -62,6 +84,6 @@
 } // namespace detail
 } // namespace boost
 
-#define BOOST_DETAIL_SPINLOCK_INIT {}
+#define BOOST_DETAIL_SPINLOCK_INIT { false }
 
 #endif // #ifndef BOOST_DETAIL_SPINLOCK_NT_HPP_INCLUDED


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