summaryrefslogtreecommitdiff
path: root/boost/interprocess/sync/spin
diff options
context:
space:
mode:
Diffstat (limited to 'boost/interprocess/sync/spin')
-rw-r--r--boost/interprocess/sync/spin/condition.hpp295
-rw-r--r--boost/interprocess/sync/spin/interprocess_barrier.hpp46
-rw-r--r--boost/interprocess/sync/spin/mutex.hpp114
-rw-r--r--boost/interprocess/sync/spin/recursive_mutex.hpp175
-rw-r--r--boost/interprocess/sync/spin/semaphore.hpp110
5 files changed, 740 insertions, 0 deletions
diff --git a/boost/interprocess/sync/spin/condition.hpp b/boost/interprocess/sync/spin/condition.hpp
new file mode 100644
index 0000000..5a37d9b
--- /dev/null
+++ b/boost/interprocess/sync/spin/condition.hpp
@@ -0,0 +1,295 @@
+//////////////////////////////////////////////////////////////////////////////
+//
+// (C) Copyright Ion Gaztanaga 2005-2011. Distributed under the Boost
+// Software License, Version 1.0. (See accompanying file
+// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+//
+// See http://www.boost.org/libs/interprocess for documentation.
+//
+//////////////////////////////////////////////////////////////////////////////
+
+#ifndef BOOST_INTERPROCESS_DETAIL_SPIN_CONDITION_HPP
+#define BOOST_INTERPROCESS_DETAIL_SPIN_CONDITION_HPP
+
+#include <boost/interprocess/detail/config_begin.hpp>
+#include <boost/interprocess/detail/workaround.hpp>
+#include <boost/interprocess/sync/spin/mutex.hpp>
+#include <boost/interprocess/detail/posix_time_types_wrk.hpp>
+#include <boost/interprocess/detail/atomic.hpp>
+#include <boost/interprocess/sync/scoped_lock.hpp>
+#include <boost/interprocess/exceptions.hpp>
+#include <boost/interprocess/detail/os_thread_functions.hpp>
+#include <boost/move/move.hpp>
+#include <boost/cstdint.hpp>
+
+namespace boost {
+namespace interprocess {
+namespace ipcdetail {
+
+class spin_condition
+{
+ spin_condition(const spin_condition &);
+ spin_condition &operator=(const spin_condition &);
+ public:
+ spin_condition();
+ ~spin_condition();
+
+ void notify_one();
+ void notify_all();
+
+ template <typename L>
+ bool timed_wait(L& lock, const boost::posix_time::ptime &abs_time)
+ {
+ if(abs_time == boost::posix_time::pos_infin){
+ this->wait(lock);
+ return true;
+ }
+ if (!lock)
+ throw lock_exception();
+ return this->do_timed_wait(abs_time, *lock.mutex());
+ }
+
+ template <typename L, typename Pr>
+ bool timed_wait(L& lock, const boost::posix_time::ptime &abs_time, Pr pred)
+ {
+ if(abs_time == boost::posix_time::pos_infin){
+ this->wait(lock, pred);
+ return true;
+ }
+ if (!lock)
+ throw lock_exception();
+ while (!pred()){
+ if (!this->do_timed_wait(abs_time, *lock.mutex()))
+ return pred();
+ }
+ return true;
+ }
+
+ template <typename L>
+ void wait(L& lock)
+ {
+ if (!lock)
+ throw lock_exception();
+ do_wait(*lock.mutex());
+ }
+
+ template <typename L, typename Pr>
+ void wait(L& lock, Pr pred)
+ {
+ if (!lock)
+ throw lock_exception();
+
+ while (!pred())
+ do_wait(*lock.mutex());
+ }
+
+ template<class InterprocessMutex>
+ void do_wait(InterprocessMutex &mut);
+
+ template<class InterprocessMutex>
+ bool do_timed_wait(const boost::posix_time::ptime &abs_time, InterprocessMutex &mut);
+
+ private:
+ template<class InterprocessMutex>
+ bool do_timed_wait(bool tout_enabled, const boost::posix_time::ptime &abs_time, InterprocessMutex &mut);
+
+ enum { SLEEP = 0, NOTIFY_ONE, NOTIFY_ALL };
+ spin_mutex m_enter_mut;
+ volatile boost::uint32_t m_command;
+ volatile boost::uint32_t m_num_waiters;
+ void notify(boost::uint32_t command);
+};
+
+inline spin_condition::spin_condition()
+{
+ //Note that this class is initialized to zero.
+ //So zeroed memory can be interpreted as an initialized
+ //condition variable
+ m_command = SLEEP;
+ m_num_waiters = 0;
+}
+
+inline spin_condition::~spin_condition()
+{
+ //Trivial destructor
+}
+
+inline void spin_condition::notify_one()
+{
+ this->notify(NOTIFY_ONE);
+}
+
+inline void spin_condition::notify_all()
+{
+ this->notify(NOTIFY_ALL);
+}
+
+inline void spin_condition::notify(boost::uint32_t command)
+{
+ //This mutex guarantees that no other thread can enter to the
+ //do_timed_wait method logic, so that thread count will be
+ //constant until the function writes a NOTIFY_ALL command.
+ //It also guarantees that no other notification can be signaled
+ //on this spin_condition before this one ends
+ m_enter_mut.lock();
+
+ //Return if there are no waiters
+ if(!atomic_read32(&m_num_waiters)) {
+ m_enter_mut.unlock();
+ return;
+ }
+
+ //Notify that all threads should execute wait logic
+ while(SLEEP != atomic_cas32(const_cast<boost::uint32_t*>(&m_command), command, SLEEP)){
+ thread_yield();
+ }
+/*
+ //Wait until the threads are woken
+ while(SLEEP != atomic_cas32(const_cast<boost::uint32_t*>(&m_command), 0)){
+ thread_yield();
+ }
+*/
+ //The enter mutex will rest locked until the last waiting thread unlocks it
+}
+
+template<class InterprocessMutex>
+inline void spin_condition::do_wait(InterprocessMutex &mut)
+{
+ this->do_timed_wait(false, boost::posix_time::ptime(), mut);
+}
+
+template<class InterprocessMutex>
+inline bool spin_condition::do_timed_wait
+ (const boost::posix_time::ptime &abs_time, InterprocessMutex &mut)
+{
+ return this->do_timed_wait(true, abs_time, mut);
+}
+
+template<class InterprocessMutex>
+inline bool spin_condition::do_timed_wait(bool tout_enabled,
+ const boost::posix_time::ptime &abs_time,
+ InterprocessMutex &mut)
+{
+ boost::posix_time::ptime now = microsec_clock::universal_time();
+
+ if(tout_enabled){
+ if(now >= abs_time) return false;
+ }
+
+ typedef boost::interprocess::scoped_lock<spin_mutex> InternalLock;
+ //The enter mutex guarantees that while executing a notification,
+ //no other thread can execute the do_timed_wait method.
+ {
+ //---------------------------------------------------------------
+ InternalLock lock;
+ if(tout_enabled){
+ InternalLock dummy(m_enter_mut, abs_time);
+ lock = boost::move(dummy);
+ }
+ else{
+ InternalLock dummy(m_enter_mut);
+ lock = boost::move(dummy);
+ }
+
+ if(!lock)
+ return false;
+ //---------------------------------------------------------------
+ //We increment the waiting thread count protected so that it will be
+ //always constant when another thread enters the notification logic.
+ //The increment marks this thread as "waiting on spin_condition"
+ atomic_inc32(const_cast<boost::uint32_t*>(&m_num_waiters));
+
+ //We unlock the external mutex atomically with the increment
+ mut.unlock();
+ }
+
+ //By default, we suppose that no timeout has happened
+ bool timed_out = false, unlock_enter_mut= false;
+
+ //Loop until a notification indicates that the thread should
+ //exit or timeout occurs
+ while(1){
+ //The thread sleeps/spins until a spin_condition commands a notification
+ //Notification occurred, we will lock the checking mutex so that
+ while(atomic_read32(&m_command) == SLEEP){
+ thread_yield();
+
+ //Check for timeout
+ if(tout_enabled){
+ now = microsec_clock::universal_time();
+
+ if(now >= abs_time){
+ //If we can lock the mutex it means that no notification
+ //is being executed in this spin_condition variable
+ timed_out = m_enter_mut.try_lock();
+
+ //If locking fails, indicates that another thread is executing
+ //notification, so we play the notification game
+ if(!timed_out){
+ //There is an ongoing notification, we will try again later
+ continue;
+ }
+ //No notification in execution, since enter mutex is locked.
+ //We will execute time-out logic, so we will decrement count,
+ //release the enter mutex and return false.
+ break;
+ }
+ }
+ }
+
+ //If a timeout occurred, the mutex will not execute checking logic
+ if(tout_enabled && timed_out){
+ //Decrement wait count
+ atomic_dec32(const_cast<boost::uint32_t*>(&m_num_waiters));
+ unlock_enter_mut = true;
+ break;
+ }
+ else{
+ boost::uint32_t result = atomic_cas32
+ (const_cast<boost::uint32_t*>(&m_command), SLEEP, NOTIFY_ONE);
+ if(result == SLEEP){
+ //Other thread has been notified and since it was a NOTIFY one
+ //command, this thread must sleep again
+ continue;
+ }
+ else if(result == NOTIFY_ONE){
+ //If it was a NOTIFY_ONE command, only this thread should
+ //exit. This thread has atomically marked command as sleep before
+ //so no other thread will exit.
+ //Decrement wait count.
+ unlock_enter_mut = true;
+ atomic_dec32(const_cast<boost::uint32_t*>(&m_num_waiters));
+ break;
+ }
+ else{
+ //If it is a NOTIFY_ALL command, all threads should return
+ //from do_timed_wait function. Decrement wait count.
+ unlock_enter_mut = 1 == atomic_dec32(const_cast<boost::uint32_t*>(&m_num_waiters));
+ //Check if this is the last thread of notify_all waiters
+ //Only the last thread will release the mutex
+ if(unlock_enter_mut){
+ atomic_cas32(const_cast<boost::uint32_t*>(&m_command), SLEEP, NOTIFY_ALL);
+ }
+ break;
+ }
+ }
+ }
+
+ //Unlock the enter mutex if it is a single notification, if this is
+ //the last notified thread in a notify_all or a timeout has occurred
+ if(unlock_enter_mut){
+ m_enter_mut.unlock();
+ }
+
+ //Lock external again before returning from the method
+ mut.lock();
+ return !timed_out;
+}
+
+} //namespace ipcdetail
+} //namespace interprocess
+} //namespace boost
+
+#include <boost/interprocess/detail/config_end.hpp>
+
+#endif //BOOST_INTERPROCESS_DETAIL_SPIN_CONDITION_HPP
diff --git a/boost/interprocess/sync/spin/interprocess_barrier.hpp b/boost/interprocess/sync/spin/interprocess_barrier.hpp
new file mode 100644
index 0000000..f8ee099
--- /dev/null
+++ b/boost/interprocess/sync/spin/interprocess_barrier.hpp
@@ -0,0 +1,46 @@
+//////////////////////////////////////////////////////////////////////////////
+//
+// (C) Copyright Ion Gaztanaga 2006. Distributed under the Boost
+// Software License, Version 1.0. (See accompanying file
+// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+//
+// See http://www.boost.org/libs/interprocess for documentation.
+//
+//////////////////////////////////////////////////////////////////////////////
+
+#include<boost/interprocess/exceptions.hpp>
+#include <boost/interprocess/detail/posix_time_types_wrk.hpp>
+#include <boost/interprocess/sync/scoped_lock.hpp>
+
+namespace boost {
+namespace interprocess {
+
+inline barrier::barrier(unsigned int count)
+ : m_threshold(count), m_count(count), m_generation(0)
+{
+ if (count == 0)
+ throw std::invalid_argument("count cannot be zero.");
+}
+
+inline barrier::~barrier(){}
+
+inline bool barrier::wait()
+{
+ scoped_lock<interprocess_mutex> lock(m_mutex);
+ unsigned int gen = m_generation;
+
+ if (--m_count == 0){
+ m_generation++;
+ m_count = m_threshold;
+ m_cond.notify_all();
+ return true;
+ }
+
+ while (gen == m_generation){
+ m_cond.wait(lock);
+ }
+ return false;
+}
+
+} //namespace interprocess {
+} //namespace boost {
diff --git a/boost/interprocess/sync/spin/mutex.hpp b/boost/interprocess/sync/spin/mutex.hpp
new file mode 100644
index 0000000..ef0b47d
--- /dev/null
+++ b/boost/interprocess/sync/spin/mutex.hpp
@@ -0,0 +1,114 @@
+//////////////////////////////////////////////////////////////////////////////
+//
+// (C) Copyright Ion Gaztanaga 2005-2011. Distributed under the Boost
+// Software License, Version 1.0. (See accompanying file
+// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+//
+// See http://www.boost.org/libs/interprocess for documentation.
+//
+//////////////////////////////////////////////////////////////////////////////
+
+#ifndef BOOST_INTERPROCESS_DETAIL_SPIN_MUTEX_HPP
+#define BOOST_INTERPROCESS_DETAIL_SPIN_MUTEX_HPP
+
+#if (defined _MSC_VER) && (_MSC_VER >= 1200)
+# pragma once
+#endif
+
+#include <boost/interprocess/detail/config_begin.hpp>
+#include <boost/interprocess/detail/workaround.hpp>
+#include <boost/interprocess/detail/posix_time_types_wrk.hpp>
+#include <boost/assert.hpp>
+#include <boost/interprocess/detail/atomic.hpp>
+#include <boost/cstdint.hpp>
+#include <boost/interprocess/detail/os_thread_functions.hpp>
+
+namespace boost {
+namespace interprocess {
+namespace ipcdetail {
+
+class spin_mutex
+{
+ spin_mutex(const spin_mutex &);
+ spin_mutex &operator=(const spin_mutex &);
+ public:
+
+ spin_mutex();
+ ~spin_mutex();
+
+ void lock();
+ bool try_lock();
+ bool timed_lock(const boost::posix_time::ptime &abs_time);
+ void unlock();
+ void take_ownership(){};
+ private:
+ volatile boost::uint32_t m_s;
+};
+
+inline spin_mutex::spin_mutex()
+ : m_s(0)
+{
+ //Note that this class is initialized to zero.
+ //So zeroed memory can be interpreted as an
+ //initialized mutex
+}
+
+inline spin_mutex::~spin_mutex()
+{
+ //Trivial destructor
+}
+
+inline void spin_mutex::lock(void)
+{
+ do{
+ boost::uint32_t prev_s = ipcdetail::atomic_cas32(const_cast<boost::uint32_t*>(&m_s), 1, 0);
+
+ if (m_s == 1 && prev_s == 0){
+ break;
+ }
+ // relinquish current timeslice
+ ipcdetail::thread_yield();
+ }while (true);
+}
+
+inline bool spin_mutex::try_lock(void)
+{
+ boost::uint32_t prev_s = ipcdetail::atomic_cas32(const_cast<boost::uint32_t*>(&m_s), 1, 0);
+ return m_s == 1 && prev_s == 0;
+}
+
+inline bool spin_mutex::timed_lock(const boost::posix_time::ptime &abs_time)
+{
+ if(abs_time == boost::posix_time::pos_infin){
+ this->lock();
+ return true;
+ }
+ //Obtain current count and target time
+ boost::posix_time::ptime now = microsec_clock::universal_time();
+
+ do{
+ if(this->try_lock()){
+ break;
+ }
+ now = microsec_clock::universal_time();
+
+ if(now >= abs_time){
+ return false;
+ }
+ // relinquish current time slice
+ ipcdetail::thread_yield();
+ }while (true);
+
+ return true;
+}
+
+inline void spin_mutex::unlock(void)
+{ ipcdetail::atomic_cas32(const_cast<boost::uint32_t*>(&m_s), 0, 1); }
+
+} //namespace ipcdetail {
+} //namespace interprocess {
+} //namespace boost {
+
+#include <boost/interprocess/detail/config_end.hpp>
+
+#endif //BOOST_INTERPROCESS_DETAIL_SPIN_MUTEX_HPP
diff --git a/boost/interprocess/sync/spin/recursive_mutex.hpp b/boost/interprocess/sync/spin/recursive_mutex.hpp
new file mode 100644
index 0000000..05ad65e
--- /dev/null
+++ b/boost/interprocess/sync/spin/recursive_mutex.hpp
@@ -0,0 +1,175 @@
+//////////////////////////////////////////////////////////////////////////////
+//
+// (C) Copyright Ion Gaztanaga 2005-2011. Distributed under the Boost
+// Software License, Version 1.0. (See accompanying file
+// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+//
+// See http://www.boost.org/libs/interprocess for documentation.
+//
+//////////////////////////////////////////////////////////////////////////////
+//
+// Parts of the pthread code come from Boost Threads code:
+//
+//////////////////////////////////////////////////////////////////////////////
+//
+// Copyright (C) 2001-2003
+// William E. Kempf
+//
+// Permission to use, copy, modify, distribute and sell this software
+// and its documentation for any purpose is hereby granted without fee,
+// provided that the above copyright notice appear in all copies and
+// that both that copyright notice and this permission notice appear
+// in supporting documentation. William E. Kempf makes no representations
+// about the suitability of this software for any purpose.
+// It is provided "as is" without express or implied warranty.
+//////////////////////////////////////////////////////////////////////////////
+
+#ifndef BOOST_INTERPROCESS_DETAIL_SPIN_RECURSIVE_MUTEX_HPP
+#define BOOST_INTERPROCESS_DETAIL_SPIN_RECURSIVE_MUTEX_HPP
+
+#if (defined _MSC_VER) && (_MSC_VER >= 1200)
+# pragma once
+#endif
+
+#include <boost/interprocess/detail/config_begin.hpp>
+#include <boost/interprocess/detail/workaround.hpp>
+
+#include <boost/interprocess/detail/posix_time_types_wrk.hpp>
+#include <boost/interprocess/detail/os_thread_functions.hpp>
+#include <boost/interprocess/exceptions.hpp>
+#include <boost/interprocess/detail/atomic.hpp>
+#include <boost/cstdint.hpp>
+#include <boost/interprocess/detail/os_thread_functions.hpp>
+#include <boost/interprocess/sync/spin/mutex.hpp>
+#include <boost/assert.hpp>
+
+namespace boost {
+namespace interprocess {
+namespace ipcdetail {
+
+class spin_recursive_mutex
+{
+ spin_recursive_mutex(const spin_recursive_mutex &);
+ spin_recursive_mutex &operator=(const spin_recursive_mutex &);
+ public:
+
+ spin_recursive_mutex();
+ ~spin_recursive_mutex();
+
+ void lock();
+ bool try_lock();
+ bool timed_lock(const boost::posix_time::ptime &abs_time);
+ void unlock();
+ void take_ownership();
+ private:
+ spin_mutex m_mutex;
+ unsigned int m_nLockCount;
+ volatile ipcdetail::OS_systemwide_thread_id_t m_nOwner;
+ volatile boost::uint32_t m_s;
+};
+
+inline spin_recursive_mutex::spin_recursive_mutex()
+ : m_nLockCount(0), m_nOwner(ipcdetail::get_invalid_systemwide_thread_id()){}
+
+inline spin_recursive_mutex::~spin_recursive_mutex(){}
+
+inline void spin_recursive_mutex::lock()
+{
+ typedef ipcdetail::OS_systemwide_thread_id_t handle_t;
+ const handle_t thr_id(ipcdetail::get_current_systemwide_thread_id());
+ handle_t old_id;
+ ipcdetail::systemwide_thread_id_copy(m_nOwner, old_id);
+ if(ipcdetail::equal_systemwide_thread_id(thr_id , old_id)){
+ if((unsigned int)(m_nLockCount+1) == 0){
+ //Overflow, throw an exception
+ throw interprocess_exception("boost::interprocess::spin_recursive_mutex recursive lock overflow");
+ }
+ ++m_nLockCount;
+ }
+ else{
+ m_mutex.lock();
+ ipcdetail::systemwide_thread_id_copy(thr_id, m_nOwner);
+ m_nLockCount = 1;
+ }
+}
+
+inline bool spin_recursive_mutex::try_lock()
+{
+ typedef ipcdetail::OS_systemwide_thread_id_t handle_t;
+ handle_t thr_id(ipcdetail::get_current_systemwide_thread_id());
+ handle_t old_id;
+ ipcdetail::systemwide_thread_id_copy(m_nOwner, old_id);
+ if(ipcdetail::equal_systemwide_thread_id(thr_id , old_id)) { // we own it
+ if((unsigned int)(m_nLockCount+1) == 0){
+ //Overflow, throw an exception
+ throw interprocess_exception("boost::interprocess::spin_recursive_mutex recursive lock overflow");
+ }
+ ++m_nLockCount;
+ return true;
+ }
+ if(m_mutex.try_lock()){
+ ipcdetail::systemwide_thread_id_copy(thr_id, m_nOwner);
+ m_nLockCount = 1;
+ return true;
+ }
+ return false;
+}
+
+inline bool spin_recursive_mutex::timed_lock(const boost::posix_time::ptime &abs_time)
+{
+ typedef ipcdetail::OS_systemwide_thread_id_t handle_t;
+ if(abs_time == boost::posix_time::pos_infin){
+ this->lock();
+ return true;
+ }
+ const handle_t thr_id(ipcdetail::get_current_systemwide_thread_id());
+ handle_t old_id;
+ ipcdetail::systemwide_thread_id_copy(m_nOwner, old_id);
+ if(ipcdetail::equal_systemwide_thread_id(thr_id , old_id)) { // we own it
+ if((unsigned int)(m_nLockCount+1) == 0){
+ //Overflow, throw an exception
+ throw interprocess_exception("boost::interprocess::spin_recursive_mutex recursive lock overflow");
+ }
+ ++m_nLockCount;
+ return true;
+ }
+ if(m_mutex.timed_lock(abs_time)){
+ ipcdetail::systemwide_thread_id_copy(thr_id, m_nOwner);
+ m_nLockCount = 1;
+ return true;
+ }
+ return false;
+}
+
+inline void spin_recursive_mutex::unlock()
+{
+ typedef ipcdetail::OS_systemwide_thread_id_t handle_t;
+ handle_t old_id;
+ ipcdetail::systemwide_thread_id_copy(m_nOwner, old_id);
+ const handle_t thr_id(ipcdetail::get_current_systemwide_thread_id());
+ (void)old_id;
+ (void)thr_id;
+ BOOST_ASSERT(ipcdetail::equal_systemwide_thread_id(thr_id, old_id));
+ --m_nLockCount;
+ if(!m_nLockCount){
+ const handle_t new_id(ipcdetail::get_invalid_systemwide_thread_id());
+ ipcdetail::systemwide_thread_id_copy(new_id, m_nOwner);
+ m_mutex.unlock();
+ }
+}
+
+inline void spin_recursive_mutex::take_ownership()
+{
+ typedef ipcdetail::OS_systemwide_thread_id_t handle_t;
+ this->m_nLockCount = 1;
+ const handle_t thr_id(ipcdetail::get_current_systemwide_thread_id());
+ ipcdetail::systemwide_thread_id_copy(thr_id, m_nOwner);
+}
+
+} //namespace ipcdetail {
+} //namespace interprocess {
+} //namespace boost {
+
+#include <boost/interprocess/detail/config_end.hpp>
+
+#endif //BOOST_INTERPROCESS_DETAIL_SPIN_RECURSIVE_MUTEX_HPP
diff --git a/boost/interprocess/sync/spin/semaphore.hpp b/boost/interprocess/sync/spin/semaphore.hpp
new file mode 100644
index 0000000..1b8cac3
--- /dev/null
+++ b/boost/interprocess/sync/spin/semaphore.hpp
@@ -0,0 +1,110 @@
+//////////////////////////////////////////////////////////////////////////////
+//
+// (C) Copyright Ion Gaztanaga 2005-2011. Distributed under the Boost
+// Software License, Version 1.0. (See accompanying file
+// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+//
+// See http://www.boost.org/libs/interprocess for documentation.
+//
+//////////////////////////////////////////////////////////////////////////////
+
+#ifndef BOOST_INTERPROCESS_DETAIL_SPIN_SEMAPHORE_HPP
+#define BOOST_INTERPROCESS_DETAIL_SPIN_SEMAPHORE_HPP
+
+#if (defined _MSC_VER) && (_MSC_VER >= 1200)
+# pragma once
+#endif
+
+#include <boost/interprocess/detail/config_begin.hpp>
+#include <boost/interprocess/detail/workaround.hpp>
+#include <boost/interprocess/detail/atomic.hpp>
+#include <boost/interprocess/detail/os_thread_functions.hpp>
+#include <boost/interprocess/detail/posix_time_types_wrk.hpp>
+#include <boost/cstdint.hpp>
+
+namespace boost {
+namespace interprocess {
+namespace ipcdetail {
+
+class spin_semaphore
+{
+ spin_semaphore(const spin_semaphore &);
+ spin_semaphore &operator=(const spin_semaphore &);
+
+ public:
+ spin_semaphore(unsigned int initialCount);
+ ~spin_semaphore();
+
+ void post();
+ void wait();
+ bool try_wait();
+ bool timed_wait(const boost::posix_time::ptime &abs_time);
+
+// int get_count() const;
+ private:
+ volatile boost::uint32_t m_count;
+};
+
+
+inline spin_semaphore::~spin_semaphore()
+{}
+
+inline spin_semaphore::spin_semaphore(unsigned int initialCount)
+{ ipcdetail::atomic_write32(&this->m_count, boost::uint32_t(initialCount)); }
+
+inline void spin_semaphore::post()
+{
+ ipcdetail::atomic_inc32(&m_count);
+}
+
+inline void spin_semaphore::wait()
+{
+ while(!ipcdetail::atomic_add_unless32(&m_count, boost::uint32_t(-1), boost::uint32_t(0))){
+ while(ipcdetail::atomic_read32(&m_count) == 0){
+ ipcdetail::thread_yield();
+ }
+ }
+}
+
+inline bool spin_semaphore::try_wait()
+{
+ return ipcdetail::atomic_add_unless32(&m_count, boost::uint32_t(-1), boost::uint32_t(0));
+}
+
+inline bool spin_semaphore::timed_wait(const boost::posix_time::ptime &abs_time)
+{
+ if(abs_time == boost::posix_time::pos_infin){
+ this->wait();
+ return true;
+ }
+ //Obtain current count and target time
+ boost::posix_time::ptime now(microsec_clock::universal_time());
+
+ do{
+ if(this->try_wait()){
+ break;
+ }
+ now = microsec_clock::universal_time();
+
+ if(now >= abs_time){
+ return this->try_wait();
+ }
+ // relinquish current time slice
+ ipcdetail::thread_yield();
+ }while (true);
+ return true;
+}
+
+
+//inline int spin_semaphore::get_count() const
+//{
+ //return (int)ipcdetail::atomic_read32(&m_count);
+//}
+
+} //namespace ipcdetail {
+} //namespace interprocess {
+} //namespace boost {
+
+#include <boost/interprocess/detail/config_end.hpp>
+
+#endif //BOOST_INTERPROCESS_DETAIL_SPIN_SEMAPHORE_HPP