summaryrefslogtreecommitdiff
path: root/boost/interprocess/sync/posix
diff options
context:
space:
mode:
Diffstat (limited to 'boost/interprocess/sync/posix')
-rw-r--r--boost/interprocess/sync/posix/condition.hpp191
-rw-r--r--boost/interprocess/sync/posix/mutex.hpp152
-rw-r--r--boost/interprocess/sync/posix/named_mutex.hpp116
-rw-r--r--boost/interprocess/sync/posix/named_semaphore.hpp84
-rw-r--r--boost/interprocess/sync/posix/pthread_helpers.hpp168
-rw-r--r--boost/interprocess/sync/posix/ptime_to_timespec.hpp38
-rw-r--r--boost/interprocess/sync/posix/recursive_mutex.hpp142
-rw-r--r--boost/interprocess/sync/posix/semaphore.hpp63
-rw-r--r--boost/interprocess/sync/posix/semaphore_wrapper.hpp211
9 files changed, 1165 insertions, 0 deletions
diff --git a/boost/interprocess/sync/posix/condition.hpp b/boost/interprocess/sync/posix/condition.hpp
new file mode 100644
index 0000000..213e4ce
--- /dev/null
+++ b/boost/interprocess/sync/posix/condition.hpp
@@ -0,0 +1,191 @@
+//////////////////////////////////////////////////////////////////////////////
+//
+// (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_POSIX_CONDITION_HPP
+#define BOOST_INTERPROCESS_POSIX_CONDITION_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 <pthread.h>
+#include <errno.h>
+#include <boost/interprocess/sync/posix/pthread_helpers.hpp>
+#include <boost/interprocess/sync/posix/ptime_to_timespec.hpp>
+#include <boost/interprocess/detail/posix_time_types_wrk.hpp>
+#include <boost/interprocess/sync/posix/mutex.hpp>
+#include <boost/assert.hpp>
+
+namespace boost {
+namespace interprocess {
+namespace ipcdetail {
+
+class posix_condition
+{
+ //Non-copyable
+ posix_condition(const posix_condition &);
+ posix_condition &operator=(const posix_condition &);
+
+ public:
+ //!Constructs a posix_condition. On error throws interprocess_exception.
+ posix_condition();
+
+ //!Destroys *this
+ //!liberating system resources.
+ ~posix_condition();
+
+ //!If there is a thread waiting on *this, change that
+ //!thread's state to ready. Otherwise there is no effect.
+ void notify_one();
+
+ //!Change the state of all threads waiting on *this to ready.
+ //!If there are no waiting threads, notify_all() has no effect.
+ void notify_all();
+
+ //!Releases the lock on the posix_mutex object associated with lock, blocks
+ //!the current thread of execution until readied by a call to
+ //!this->notify_one() or this->notify_all(), and then reacquires the lock.
+ template <typename L>
+ void wait(L& lock)
+ {
+ if (!lock)
+ throw lock_exception();
+ this->do_wait(*lock.mutex());
+ }
+
+ //!The same as:
+ //!while (!pred()) wait(lock)
+ template <typename L, typename Pr>
+ void wait(L& lock, Pr pred)
+ {
+ if (!lock)
+ throw lock_exception();
+
+ while (!pred())
+ this->do_wait(*lock.mutex());
+ }
+
+ //!Releases the lock on the posix_mutex object associated with lock, blocks
+ //!the current thread of execution until readied by a call to
+ //!this->notify_one() or this->notify_all(), or until time abs_time is reached,
+ //!and then reacquires the lock.
+ //!Returns: false if time abs_time is reached, otherwise true.
+ 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());
+ }
+
+ //!The same as: while (!pred()) {
+ //! if (!timed_wait(lock, abs_time)) return pred();
+ //! } return true;
+ 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;
+ }
+
+
+ void do_wait(posix_mutex &mut);
+
+ bool do_timed_wait(const boost::posix_time::ptime &abs_time, posix_mutex &mut);
+
+ private:
+ pthread_cond_t m_condition;
+};
+
+inline posix_condition::posix_condition()
+{
+ int res;
+ pthread_condattr_t cond_attr;
+ res = pthread_condattr_init(&cond_attr);
+ if(res != 0){
+ throw interprocess_exception("pthread_condattr_init failed");
+ }
+ res = pthread_condattr_setpshared(&cond_attr, PTHREAD_PROCESS_SHARED);
+ if(res != 0){
+ pthread_condattr_destroy(&cond_attr);
+ throw interprocess_exception(res);
+ }
+ res = pthread_cond_init(&m_condition, &cond_attr);
+ pthread_condattr_destroy(&cond_attr);
+ if(res != 0){
+ throw interprocess_exception(res);
+ }
+}
+
+inline posix_condition::~posix_condition()
+{
+ int res = 0;
+ res = pthread_cond_destroy(&m_condition);
+ BOOST_ASSERT(res == 0);
+}
+
+inline void posix_condition::notify_one()
+{
+ int res = 0;
+ res = pthread_cond_signal(&m_condition);
+ BOOST_ASSERT(res == 0);
+}
+
+inline void posix_condition::notify_all()
+{
+ int res = 0;
+ res = pthread_cond_broadcast(&m_condition);
+ BOOST_ASSERT(res == 0);
+}
+
+inline void posix_condition::do_wait(posix_mutex &mut)
+{
+ pthread_mutex_t* pmutex = &mut.m_mut;
+ int res = 0;
+ res = pthread_cond_wait(&m_condition, pmutex);
+ BOOST_ASSERT(res == 0);
+}
+
+inline bool posix_condition::do_timed_wait
+ (const boost::posix_time::ptime &abs_time, posix_mutex &mut)
+{
+ timespec ts = ptime_to_timespec(abs_time);
+ pthread_mutex_t* pmutex = &mut.m_mut;
+ int res = 0;
+ res = pthread_cond_timedwait(&m_condition, pmutex, &ts);
+ BOOST_ASSERT(res == 0 || res == ETIMEDOUT);
+
+ return res != ETIMEDOUT;
+}
+
+} //namespace ipcdetail
+} //namespace interprocess
+} //namespace boost
+
+#include <boost/interprocess/detail/config_end.hpp>
+
+#endif //#ifndef BOOST_INTERPROCESS_POSIX_CONDITION_HPP
diff --git a/boost/interprocess/sync/posix/mutex.hpp b/boost/interprocess/sync/posix/mutex.hpp
new file mode 100644
index 0000000..22e2ec0
--- /dev/null
+++ b/boost/interprocess/sync/posix/mutex.hpp
@@ -0,0 +1,152 @@
+//////////////////////////////////////////////////////////////////////////////
+//
+// (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_POSIX_MUTEX_HPP
+#define BOOST_INTERPROCESS_DETAIL_POSIX_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 <pthread.h>
+#include <errno.h>
+#include <boost/interprocess/exceptions.hpp>
+#include <boost/interprocess/sync/posix/ptime_to_timespec.hpp>
+#include <boost/interprocess/detail/posix_time_types_wrk.hpp>
+#include <boost/interprocess/exceptions.hpp>
+#include <boost/interprocess/sync/posix/pthread_helpers.hpp>
+
+#ifndef BOOST_INTERPROCESS_POSIX_TIMEOUTS
+# include <boost/interprocess/detail/os_thread_functions.hpp>
+#endif
+#include <boost/assert.hpp>
+
+namespace boost {
+namespace interprocess {
+namespace ipcdetail {
+
+class posix_condition;
+
+class posix_mutex
+{
+ posix_mutex(const posix_mutex &);
+ posix_mutex &operator=(const posix_mutex &);
+ public:
+
+ posix_mutex();
+ ~posix_mutex();
+
+ void lock();
+ bool try_lock();
+ bool timed_lock(const boost::posix_time::ptime &abs_time);
+ void unlock();
+
+ friend class posix_condition;
+
+ private:
+ pthread_mutex_t m_mut;
+};
+
+inline posix_mutex::posix_mutex()
+{
+ mutexattr_wrapper mut_attr;
+ mutex_initializer mut(m_mut, mut_attr);
+ mut.release();
+}
+
+inline posix_mutex::~posix_mutex()
+{
+ int res = pthread_mutex_destroy(&m_mut);
+ BOOST_ASSERT(res == 0);(void)res;
+}
+
+inline void posix_mutex::lock()
+{
+ if (pthread_mutex_lock(&m_mut) != 0)
+ throw lock_exception();
+}
+
+inline bool posix_mutex::try_lock()
+{
+ int res = pthread_mutex_trylock(&m_mut);
+ if (!(res == 0 || res == EBUSY))
+ throw lock_exception();
+ return res == 0;
+}
+
+inline bool posix_mutex::timed_lock(const boost::posix_time::ptime &abs_time)
+{
+ if(abs_time == boost::posix_time::pos_infin){
+ this->lock();
+ return true;
+ }
+ #ifdef BOOST_INTERPROCESS_POSIX_TIMEOUTS
+
+ timespec ts = ptime_to_timespec(abs_time);
+ int res = pthread_mutex_timedlock(&m_mut, &ts);
+ if (res != 0 && res != ETIMEDOUT)
+ throw lock_exception();
+ return res == 0;
+
+ #else //BOOST_INTERPROCESS_POSIX_TIMEOUTS
+
+ //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
+ thread_yield();
+ }while (true);
+ return true;
+
+ #endif //BOOST_INTERPROCESS_POSIX_TIMEOUTS
+}
+
+inline void posix_mutex::unlock()
+{
+ int res = 0;
+ res = pthread_mutex_unlock(&m_mut);
+ BOOST_ASSERT(res == 0);
+}
+
+} //namespace ipcdetail {
+} //namespace interprocess {
+} //namespace boost {
+
+#include <boost/interprocess/detail/config_end.hpp>
+
+#endif //#ifndef BOOST_INTERPROCESS_DETAIL_POSIX_MUTEX_HPP
diff --git a/boost/interprocess/sync/posix/named_mutex.hpp b/boost/interprocess/sync/posix/named_mutex.hpp
new file mode 100644
index 0000000..931c731
--- /dev/null
+++ b/boost/interprocess/sync/posix/named_mutex.hpp
@@ -0,0 +1,116 @@
+//////////////////////////////////////////////////////////////////////////////
+//
+// (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_POSIX_NAMED_MUTEX_HPP
+#define BOOST_INTERPROCESS_POSIX_NAMED_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/creation_tags.hpp>
+#include <boost/interprocess/exceptions.hpp>
+#include <boost/interprocess/detail/interprocess_tester.hpp>
+#include <boost/interprocess/permissions.hpp>
+
+#include <boost/interprocess/sync/posix/named_semaphore.hpp>
+
+namespace boost {
+namespace interprocess {
+namespace ipcdetail {
+
+class named_condition;
+
+class posix_named_mutex
+{
+ /// @cond
+
+ posix_named_mutex();
+ posix_named_mutex(const posix_named_mutex &);
+ posix_named_mutex &operator=(const posix_named_mutex &);
+ friend class named_condition;
+ /// @endcond
+
+ public:
+ posix_named_mutex(create_only_t create_only, const char *name, const permissions &perm = permissions());
+
+ posix_named_mutex(open_or_create_t open_or_create, const char *name, const permissions &perm = permissions());
+
+ posix_named_mutex(open_only_t open_only, const char *name);
+
+ ~posix_named_mutex();
+
+ void unlock();
+ void lock();
+ bool try_lock();
+ bool timed_lock(const boost::posix_time::ptime &abs_time);
+ static bool remove(const char *name);
+
+ /// @cond
+ private:
+ friend class interprocess_tester;
+ void dont_close_on_destruction();
+
+ posix_named_semaphore m_sem;
+ /// @endcond
+};
+
+/// @cond
+
+inline posix_named_mutex::posix_named_mutex(create_only_t, const char *name, const permissions &perm)
+ : m_sem(create_only, name, 1, perm)
+{}
+
+inline posix_named_mutex::posix_named_mutex(open_or_create_t, const char *name, const permissions &perm)
+ : m_sem(open_or_create, name, 1, perm)
+{}
+
+inline posix_named_mutex::posix_named_mutex(open_only_t, const char *name)
+ : m_sem(open_only, name)
+{}
+
+inline void posix_named_mutex::dont_close_on_destruction()
+{ interprocess_tester::dont_close_on_destruction(m_sem); }
+
+inline posix_named_mutex::~posix_named_mutex()
+{}
+
+inline void posix_named_mutex::lock()
+{ m_sem.wait(); }
+
+inline void posix_named_mutex::unlock()
+{ m_sem.post(); }
+
+inline bool posix_named_mutex::try_lock()
+{ return m_sem.try_wait(); }
+
+inline bool posix_named_mutex::timed_lock(const boost::posix_time::ptime &abs_time)
+{
+ if(abs_time == boost::posix_time::pos_infin){
+ this->lock();
+ return true;
+ }
+ return m_sem.timed_wait(abs_time);
+}
+
+inline bool posix_named_mutex::remove(const char *name)
+{ return posix_named_semaphore::remove(name); }
+
+/// @endcond
+
+} //namespace ipcdetail {
+} //namespace interprocess {
+} //namespace boost {
+
+#include <boost/interprocess/detail/config_end.hpp>
+
+#endif //BOOST_INTERPROCESS_POSIX_NAMED_MUTEX_HPP
diff --git a/boost/interprocess/sync/posix/named_semaphore.hpp b/boost/interprocess/sync/posix/named_semaphore.hpp
new file mode 100644
index 0000000..f0327a4
--- /dev/null
+++ b/boost/interprocess/sync/posix/named_semaphore.hpp
@@ -0,0 +1,84 @@
+//////////////////////////////////////////////////////////////////////////////
+//
+// (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_POSIX_NAMED_CONDITION_HPP
+#define BOOST_INTERPROCESS_POSIX_NAMED_CONDITION_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/sync/posix/semaphore_wrapper.hpp>
+
+namespace boost {
+namespace interprocess {
+
+/// @cond
+namespace ipcdetail{ class interprocess_tester; }
+/// @endcond
+
+namespace ipcdetail {
+
+class posix_named_semaphore
+{
+ posix_named_semaphore();
+ posix_named_semaphore(const posix_named_semaphore&);
+ posix_named_semaphore &operator= (const posix_named_semaphore &);
+
+ public:
+ posix_named_semaphore
+ (create_only_t, const char *name, unsigned int initialCount, const permissions &perm = permissions())
+ { semaphore_open(mp_sem, DoCreate, name, initialCount, perm); }
+
+ posix_named_semaphore(open_or_create_t, const char *name, unsigned int initialCount, const permissions &perm = permissions())
+ { semaphore_open(mp_sem, DoOpenOrCreate, name, initialCount, perm); }
+
+ posix_named_semaphore(open_only_t, const char *name)
+ { semaphore_open(mp_sem, DoOpen, name); }
+
+ ~posix_named_semaphore()
+ {
+ if(mp_sem != BOOST_INTERPROCESS_POSIX_SEM_FAILED)
+ semaphore_close(mp_sem);
+ }
+
+ void post()
+ { semaphore_post(mp_sem); }
+
+ void wait()
+ { semaphore_wait(mp_sem); }
+
+ bool try_wait()
+ { return semaphore_try_wait(mp_sem); }
+
+ bool timed_wait(const boost::posix_time::ptime &abs_time)
+ { return semaphore_timed_wait(mp_sem, abs_time); }
+
+ static bool remove(const char *name)
+ { return semaphore_unlink(name); }
+
+ private:
+ friend class ipcdetail::interprocess_tester;
+ void dont_close_on_destruction()
+ { mp_sem = BOOST_INTERPROCESS_POSIX_SEM_FAILED; }
+
+ sem_t *mp_sem;
+};
+
+} //namespace ipcdetail {
+} //namespace interprocess {
+} //namespace boost {
+
+#include <boost/interprocess/detail/config_end.hpp>
+
+#endif //#ifndef BOOST_INTERPROCESS_POSIX_NAMED_CONDITION_HPP
diff --git a/boost/interprocess/sync/posix/pthread_helpers.hpp b/boost/interprocess/sync/posix/pthread_helpers.hpp
new file mode 100644
index 0000000..c09ce20
--- /dev/null
+++ b/boost/interprocess/sync/posix/pthread_helpers.hpp
@@ -0,0 +1,168 @@
+//////////////////////////////////////////////////////////////////////////////
+//
+// (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_PTHREAD_HELPERS_HPP
+#define BOOST_INTERPROCESS_PTHREAD_HELPERS_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 <pthread.h>
+#include <errno.h>
+#include <boost/interprocess/exceptions.hpp>
+
+namespace boost {
+namespace interprocess {
+namespace ipcdetail{
+
+ #if defined BOOST_INTERPROCESS_POSIX_PROCESS_SHARED
+
+ //!Makes pthread_mutexattr_t cleanup easy when using exceptions
+ struct mutexattr_wrapper
+ {
+ //!Constructor
+ mutexattr_wrapper(bool recursive = false)
+ {
+ if(pthread_mutexattr_init(&m_attr)!=0 ||
+ pthread_mutexattr_setpshared(&m_attr, PTHREAD_PROCESS_SHARED)!= 0 ||
+ (recursive &&
+ pthread_mutexattr_settype(&m_attr, PTHREAD_MUTEX_RECURSIVE)!= 0 ))
+ throw interprocess_exception("pthread_mutexattr_xxxx failed");
+ }
+
+ //!Destructor
+ ~mutexattr_wrapper() { pthread_mutexattr_destroy(&m_attr); }
+
+ //!This allows using mutexattr_wrapper as pthread_mutexattr_t
+ operator pthread_mutexattr_t&() { return m_attr; }
+
+ pthread_mutexattr_t m_attr;
+ };
+
+ //!Makes pthread_condattr_t cleanup easy when using exceptions
+ struct condattr_wrapper
+ {
+ //!Constructor
+ condattr_wrapper()
+ {
+ if(pthread_condattr_init(&m_attr)!=0 ||
+ pthread_condattr_setpshared(&m_attr, PTHREAD_PROCESS_SHARED)!= 0)
+ throw interprocess_exception("pthread_condattr_xxxx failed");
+ }
+
+ //!Destructor
+ ~condattr_wrapper() { pthread_condattr_destroy(&m_attr); }
+
+ //!This allows using condattr_wrapper as pthread_condattr_t
+ operator pthread_condattr_t&(){ return m_attr; }
+
+ pthread_condattr_t m_attr;
+ };
+
+ //!Makes initialized pthread_mutex_t cleanup easy when using exceptions
+ class mutex_initializer
+ {
+ public:
+ //!Constructor. Takes interprocess_mutex attributes to initialize the interprocess_mutex
+ mutex_initializer(pthread_mutex_t &mut, pthread_mutexattr_t &mut_attr)
+ : mp_mut(&mut)
+ {
+ if(pthread_mutex_init(mp_mut, &mut_attr) != 0)
+ throw interprocess_exception("pthread_mutex_init failed");
+ }
+
+ ~mutex_initializer() { if(mp_mut) pthread_mutex_destroy(mp_mut); }
+
+ void release() {mp_mut = 0; }
+
+ private:
+ pthread_mutex_t *mp_mut;
+ };
+
+ //!Makes initialized pthread_cond_t cleanup easy when using exceptions
+ class condition_initializer
+ {
+ public:
+ condition_initializer(pthread_cond_t &cond, pthread_condattr_t &cond_attr)
+ : mp_cond(&cond)
+ {
+ if(pthread_cond_init(mp_cond, &cond_attr)!= 0)
+ throw interprocess_exception("pthread_cond_init failed");
+ }
+
+ ~condition_initializer() { if(mp_cond) pthread_cond_destroy(mp_cond); }
+
+ void release() { mp_cond = 0; }
+
+ private:
+ pthread_cond_t *mp_cond;
+ };
+
+ #endif // #if defined BOOST_INTERPROCESS_POSIX_PROCESS_SHARED
+
+ #if defined(BOOST_INTERPROCESS_POSIX_BARRIERS) && defined(BOOST_INTERPROCESS_POSIX_PROCESS_SHARED)
+
+ //!Makes pthread_barrierattr_t cleanup easy when using exceptions
+ struct barrierattr_wrapper
+ {
+ //!Constructor
+ barrierattr_wrapper()
+ {
+ if(pthread_barrierattr_init(&m_attr)!=0 ||
+ pthread_barrierattr_setpshared(&m_attr, PTHREAD_PROCESS_SHARED)!= 0)
+ throw interprocess_exception("pthread_barrierattr_xxx failed");
+ }
+
+ //!Destructor
+ ~barrierattr_wrapper() { pthread_barrierattr_destroy(&m_attr); }
+
+ //!This allows using mutexattr_wrapper as pthread_barrierattr_t
+ operator pthread_barrierattr_t&() { return m_attr; }
+
+ pthread_barrierattr_t m_attr;
+ };
+
+ //!Makes initialized pthread_barrier_t cleanup easy when using exceptions
+ class barrier_initializer
+ {
+ public:
+ //!Constructor. Takes barrier attributes to initialize the barrier
+ barrier_initializer(pthread_barrier_t &mut,
+ pthread_barrierattr_t &mut_attr,
+ int count)
+ : mp_barrier(&mut)
+ {
+ if(pthread_barrier_init(mp_barrier, &mut_attr, count) != 0)
+ throw interprocess_exception("pthread_barrier_init failed");
+ }
+
+ ~barrier_initializer() { if(mp_barrier) pthread_barrier_destroy(mp_barrier); }
+
+ void release() {mp_barrier = 0; }
+
+ private:
+ pthread_barrier_t *mp_barrier;
+ };
+
+ #endif //#if defined(BOOST_INTERPROCESS_POSIX_BARRIERS) && defined(BOOST_INTERPROCESS_POSIX_PROCESS_SHARED)
+
+}//namespace ipcdetail
+
+}//namespace interprocess
+
+}//namespace boost
+
+#include <boost/interprocess/detail/config_end.hpp>
+
+#endif //ifdef BOOST_INTERPROCESS_PTHREAD_HELPERS_HPP
diff --git a/boost/interprocess/sync/posix/ptime_to_timespec.hpp b/boost/interprocess/sync/posix/ptime_to_timespec.hpp
new file mode 100644
index 0000000..7d78735
--- /dev/null
+++ b/boost/interprocess/sync/posix/ptime_to_timespec.hpp
@@ -0,0 +1,38 @@
+//////////////////////////////////////////////////////////////////////////////
+//
+// (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_PTIME_TO_TIMESPEC_HPP
+#define BOOST_INTERPROCESS_DETAIL_PTIME_TO_TIMESPEC_HPP
+
+#include <boost/interprocess/detail/posix_time_types_wrk.hpp>
+
+namespace boost {
+
+namespace interprocess {
+
+namespace ipcdetail {
+
+inline timespec ptime_to_timespec (const boost::posix_time::ptime &tm)
+{
+ const boost::posix_time::ptime epoch(boost::gregorian::date(1970,1,1));
+ boost::posix_time::time_duration duration (tm - epoch);
+ timespec ts;
+ ts.tv_sec = duration.total_seconds();
+ ts.tv_nsec = duration.total_nanoseconds() % 1000000000;
+ return ts;
+}
+
+} //namespace ipcdetail {
+
+} //namespace interprocess {
+
+} //namespace boost {
+
+#endif //ifndef BOOST_INTERPROCESS_DETAIL_PTIME_TO_TIMESPEC_HPP
diff --git a/boost/interprocess/sync/posix/recursive_mutex.hpp b/boost/interprocess/sync/posix/recursive_mutex.hpp
new file mode 100644
index 0000000..baa670f
--- /dev/null
+++ b/boost/interprocess/sync/posix/recursive_mutex.hpp
@@ -0,0 +1,142 @@
+//////////////////////////////////////////////////////////////////////////////
+//
+// (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_POSIX_RECURSIVE_MUTEX_HPP
+#define BOOST_INTERPROCESS_DETAIL_POSIX_RECURSIVE_MUTEX_HPP
+
+#include <boost/interprocess/detail/config_begin.hpp>
+#include <boost/interprocess/detail/workaround.hpp>
+
+#include <pthread.h>
+#include <errno.h>
+#include <boost/interprocess/sync/posix/pthread_helpers.hpp>
+#include <boost/interprocess/sync/posix/ptime_to_timespec.hpp>
+#include <boost/interprocess/detail/posix_time_types_wrk.hpp>
+#include <boost/interprocess/exceptions.hpp>
+#ifndef BOOST_INTERPROCESS_POSIX_TIMEOUTS
+# include <boost/interprocess/detail/os_thread_functions.hpp>
+#endif
+#include <boost/assert.hpp>
+
+namespace boost {
+namespace interprocess {
+namespace ipcdetail {
+
+class posix_recursive_mutex
+{
+ posix_recursive_mutex(const posix_recursive_mutex &);
+ posix_recursive_mutex &operator=(const posix_recursive_mutex &);
+ public:
+
+ posix_recursive_mutex();
+ ~posix_recursive_mutex();
+
+ void lock();
+ bool try_lock();
+ bool timed_lock(const boost::posix_time::ptime &abs_time);
+ void unlock();
+
+ private:
+ pthread_mutex_t m_mut;
+};
+
+inline posix_recursive_mutex::posix_recursive_mutex()
+{
+ mutexattr_wrapper mut_attr(true);
+ mutex_initializer mut(m_mut, mut_attr);
+ mut.release();
+}
+
+inline posix_recursive_mutex::~posix_recursive_mutex()
+{
+ int res = pthread_mutex_destroy(&m_mut);
+ BOOST_ASSERT(res == 0);(void)res;
+}
+
+inline void posix_recursive_mutex::lock()
+{
+ if (pthread_mutex_lock(&m_mut) != 0)
+ throw lock_exception();
+}
+
+inline bool posix_recursive_mutex::try_lock()
+{
+ int res = pthread_mutex_trylock(&m_mut);
+ if (!(res == 0 || res == EBUSY))
+ throw lock_exception();
+ return res == 0;
+}
+
+inline bool posix_recursive_mutex::timed_lock(const boost::posix_time::ptime &abs_time)
+{
+ if(abs_time == boost::posix_time::pos_infin){
+ this->lock();
+ return true;
+ }
+ #ifdef BOOST_INTERPROCESS_POSIX_TIMEOUTS
+
+ timespec ts = ptime_to_timespec(abs_time);
+ int res = pthread_mutex_timedlock(&m_mut, &ts);
+ if (res != 0 && res != ETIMEDOUT)
+ throw lock_exception();
+ return res == 0;
+
+ #else //BOOST_INTERPROCESS_POSIX_TIMEOUTS
+
+ //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
+ thread_yield();
+ }while (true);
+ return true;
+
+ #endif //BOOST_INTERPROCESS_POSIX_TIMEOUTS
+}
+
+inline void posix_recursive_mutex::unlock()
+{
+ int res = 0;
+ res = pthread_mutex_unlock(&m_mut);
+ BOOST_ASSERT(res == 0);
+}
+
+} //namespace ipcdetail {
+} //namespace interprocess {
+} //namespace boost {
+
+#include <boost/interprocess/detail/config_end.hpp>
+
+#endif //#ifndef BOOST_INTERPROCESS_DETAIL_POSIX_RECURSIVE_MUTEX_HPP
diff --git a/boost/interprocess/sync/posix/semaphore.hpp b/boost/interprocess/sync/posix/semaphore.hpp
new file mode 100644
index 0000000..b7f62cc
--- /dev/null
+++ b/boost/interprocess/sync/posix/semaphore.hpp
@@ -0,0 +1,63 @@
+//////////////////////////////////////////////////////////////////////////////
+//
+// (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_POSIX_SEMAPHORE_HPP
+#define BOOST_INTERPROCESS_POSIX_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/posix_time_types_wrk.hpp>
+#include <boost/interprocess/sync/posix/semaphore_wrapper.hpp>
+
+namespace boost {
+namespace interprocess {
+namespace ipcdetail {
+
+class posix_semaphore
+{
+ posix_semaphore();
+ posix_semaphore(const posix_semaphore&);
+ posix_semaphore &operator= (const posix_semaphore &);
+
+ public:
+ posix_semaphore(unsigned int initialCount)
+ { semaphore_init(&m_sem, initialCount); }
+
+ ~posix_semaphore()
+ { semaphore_destroy(&m_sem); }
+
+ void post()
+ { semaphore_post(&m_sem); }
+
+ void wait()
+ { semaphore_wait(&m_sem); }
+
+ bool try_wait()
+ { return semaphore_try_wait(&m_sem); }
+
+ bool timed_wait(const boost::posix_time::ptime &abs_time)
+ { return semaphore_timed_wait(&m_sem, abs_time); }
+
+ private:
+ sem_t m_sem;
+};
+
+} //namespace ipcdetail {
+} //namespace interprocess {
+} //namespace boost {
+
+#include <boost/interprocess/detail/config_end.hpp>
+
+#endif //#ifndef BOOST_INTERPROCESS_POSIX_SEMAPHORE_HPP
diff --git a/boost/interprocess/sync/posix/semaphore_wrapper.hpp b/boost/interprocess/sync/posix/semaphore_wrapper.hpp
new file mode 100644
index 0000000..1aeef47
--- /dev/null
+++ b/boost/interprocess/sync/posix/semaphore_wrapper.hpp
@@ -0,0 +1,211 @@
+//////////////////////////////////////////////////////////////////////////////
+//
+// (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_POSIX_SEMAPHORE_WRAPPER_HPP
+#define BOOST_INTERPROCESS_POSIX_SEMAPHORE_WRAPPER_HPP
+
+#include <boost/interprocess/detail/posix_time_types_wrk.hpp>
+#include <boost/interprocess/exceptions.hpp>
+#include <boost/interprocess/creation_tags.hpp>
+#include <boost/interprocess/detail/os_file_functions.hpp>
+#include <boost/interprocess/detail/tmp_dir_helpers.hpp>
+#include <boost/interprocess/permissions.hpp>
+
+#include <fcntl.h> //O_CREAT, O_*...
+#include <unistd.h> //close
+#include <string> //std::string
+#include <semaphore.h> //sem_* family, SEM_VALUE_MAX
+#include <sys/stat.h> //mode_t, S_IRWXG, S_IRWXO, S_IRWXU,
+#include <boost/assert.hpp>
+
+#ifdef SEM_FAILED
+#define BOOST_INTERPROCESS_POSIX_SEM_FAILED (reinterpret_cast<sem_t*>(SEM_FAILED))
+#else
+#define BOOST_INTERPROCESS_POSIX_SEM_FAILED (reinterpret_cast<sem_t*>(-1))
+#endif
+
+#ifdef BOOST_INTERPROCESS_POSIX_TIMEOUTS
+#include <boost/interprocess/sync/posix/ptime_to_timespec.hpp>
+#else
+#include <boost/interprocess/detail/os_thread_functions.hpp>
+#endif
+
+namespace boost {
+namespace interprocess {
+namespace ipcdetail {
+
+inline bool semaphore_open
+ (sem_t *&handle, create_enum_t type, const char *origname,
+ unsigned int count = 0, const permissions &perm = permissions())
+{
+ std::string name;
+ #ifndef BOOST_INTERPROCESS_FILESYSTEM_BASED_POSIX_SEMAPHORES
+ add_leading_slash(origname, name);
+ #else
+ create_tmp_and_clean_old_and_get_filename(origname, name);
+ #endif
+
+ //Create new mapping
+ int oflag = 0;
+ switch(type){
+ case DoOpen:
+ {
+ //No addition
+ handle = ::sem_open(name.c_str(), oflag);
+ }
+ break;
+ case DoOpenOrCreate:
+ case DoCreate:
+ {
+ while(1){
+ oflag = (O_CREAT | O_EXCL);
+ handle = ::sem_open(name.c_str(), oflag, perm.get_permissions(), count);
+ if(handle != BOOST_INTERPROCESS_POSIX_SEM_FAILED){
+ //We can't change semaphore permissions!
+ //::fchmod(handle, perm.get_permissions());
+ break;
+ }
+ else if(errno == EEXIST && type == DoOpenOrCreate){
+ oflag = 0;
+ if( (handle = ::sem_open(name.c_str(), oflag)) != BOOST_INTERPROCESS_POSIX_SEM_FAILED
+ || (errno != ENOENT) ){
+ break;
+ }
+ }
+ else{
+ break;
+ }
+ }
+ }
+ break;
+ default:
+ {
+ error_info err(other_error);
+ throw interprocess_exception(err);
+ }
+ }
+
+ //Check for error
+ if(handle == BOOST_INTERPROCESS_POSIX_SEM_FAILED){
+ throw interprocess_exception(error_info(errno));
+ }
+
+ return true;
+}
+
+inline void semaphore_close(sem_t *handle)
+{
+ int ret = sem_close(handle);
+ if(ret != 0){
+ BOOST_ASSERT(0);
+ }
+}
+
+inline bool semaphore_unlink(const char *semname)
+{
+ try{
+ std::string sem_str;
+ #ifndef BOOST_INTERPROCESS_FILESYSTEM_BASED_POSIX_SEMAPHORES
+ add_leading_slash(semname, sem_str);
+ #else
+ tmp_filename(semname, sem_str);
+ #endif
+ return 0 == sem_unlink(sem_str.c_str());
+ }
+ catch(...){
+ return false;
+ }
+}
+
+inline void semaphore_init(sem_t *handle, unsigned int initialCount)
+{
+ int ret = sem_init(handle, 1, initialCount);
+ //According to SUSV3 version 2003 edition, the return value of a successful
+ //sem_init call is not defined, but -1 is returned on failure.
+ //In the future, a successful call might be required to return 0.
+ if(ret == -1){
+ throw interprocess_exception(system_error_code());
+ }
+}
+
+inline void semaphore_destroy(sem_t *handle)
+{
+ int ret = sem_destroy(handle);
+ if(ret != 0){
+ BOOST_ASSERT(0);
+ }
+}
+
+inline void semaphore_post(sem_t *handle)
+{
+ int ret = sem_post(handle);
+ if(ret != 0){
+ throw interprocess_exception(system_error_code());
+ }
+}
+
+inline void semaphore_wait(sem_t *handle)
+{
+ int ret = sem_wait(handle);
+ if(ret != 0){
+ throw interprocess_exception(system_error_code());
+ }
+}
+
+inline bool semaphore_try_wait(sem_t *handle)
+{
+ int res = sem_trywait(handle);
+ if(res == 0)
+ return true;
+ if(system_error_code() == EAGAIN){
+ return false;
+ }
+ throw interprocess_exception(system_error_code());
+ return false;
+}
+
+inline bool semaphore_timed_wait(sem_t *handle, const boost::posix_time::ptime &abs_time)
+{
+ if(abs_time == boost::posix_time::pos_infin){
+ semaphore_wait(handle);
+ return true;
+ }
+ #ifdef BOOST_INTERPROCESS_POSIX_TIMEOUTS
+ timespec tspec = ptime_to_timespec(abs_time);
+ for (;;){
+ int res = sem_timedwait(handle, &tspec);
+ if(res == 0)
+ return true;
+ if (res > 0){
+ //buggy glibc, copy the returned error code to errno
+ errno = res;
+ }
+ if(system_error_code() == ETIMEDOUT){
+ return false;
+ }
+ throw interprocess_exception(system_error_code());
+ }
+ return false;
+ #else //#ifdef BOOST_INTERPROCESS_POSIX_TIMEOUTS
+ boost::posix_time::ptime now;
+ do{
+ if(semaphore_try_wait(handle))
+ return true;
+ thread_yield();
+ }while((now = microsec_clock::universal_time()) < abs_time);
+ return false;
+ #endif //#ifdef BOOST_INTERPROCESS_POSIX_TIMEOUTS
+}
+
+} //namespace ipcdetail {
+} //namespace interprocess {
+} //namespace boost {
+
+#endif //#ifndef BOOST_INTERPROCESS_POSIX_SEMAPHORE_WRAPPER_HPP