1 //////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Ion Gaztanaga 2005-2012. Distributed under the Boost
4 // Software License, Version 1.0. (See accompanying file
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 // See http://www.boost.org/libs/interprocess for documentation.
9 //////////////////////////////////////////////////////////////////////////////
11 #ifndef BOOST_INTERPROCESS_PTHREAD_HELPERS_HPP
12 #define BOOST_INTERPROCESS_PTHREAD_HELPERS_HPP
14 #ifndef BOOST_CONFIG_HPP
15 # include <boost/config.hpp>
18 #if defined(BOOST_HAS_PRAGMA_ONCE)
22 #include <boost/interprocess/detail/config_begin.hpp>
23 #include <boost/interprocess/detail/workaround.hpp>
27 #include <boost/interprocess/exceptions.hpp>
30 namespace interprocess {
33 #if defined BOOST_INTERPROCESS_POSIX_PROCESS_SHARED
35 //!Makes pthread_mutexattr_t cleanup easy when using exceptions
36 struct mutexattr_wrapper
39 mutexattr_wrapper(bool recursive = false)
41 if(pthread_mutexattr_init(&m_attr)!=0 ||
42 pthread_mutexattr_setpshared(&m_attr, PTHREAD_PROCESS_SHARED)!= 0 ||
44 pthread_mutexattr_settype(&m_attr, PTHREAD_MUTEX_RECURSIVE)!= 0 ))
45 throw interprocess_exception("pthread_mutexattr_xxxx failed");
49 ~mutexattr_wrapper() { pthread_mutexattr_destroy(&m_attr); }
51 //!This allows using mutexattr_wrapper as pthread_mutexattr_t
52 operator pthread_mutexattr_t&() { return m_attr; }
54 pthread_mutexattr_t m_attr;
57 //!Makes pthread_condattr_t cleanup easy when using exceptions
58 struct condattr_wrapper
63 if(pthread_condattr_init(&m_attr)!=0 ||
64 pthread_condattr_setpshared(&m_attr, PTHREAD_PROCESS_SHARED)!= 0)
65 throw interprocess_exception("pthread_condattr_xxxx failed");
69 ~condattr_wrapper() { pthread_condattr_destroy(&m_attr); }
71 //!This allows using condattr_wrapper as pthread_condattr_t
72 operator pthread_condattr_t&(){ return m_attr; }
74 pthread_condattr_t m_attr;
77 //!Makes initialized pthread_mutex_t cleanup easy when using exceptions
78 class mutex_initializer
81 //!Constructor. Takes interprocess_mutex attributes to initialize the interprocess_mutex
82 mutex_initializer(pthread_mutex_t &mut, pthread_mutexattr_t &mut_attr)
85 if(pthread_mutex_init(mp_mut, &mut_attr) != 0)
86 throw interprocess_exception("pthread_mutex_init failed");
89 ~mutex_initializer() { if(mp_mut) pthread_mutex_destroy(mp_mut); }
91 void release() {mp_mut = 0; }
94 pthread_mutex_t *mp_mut;
97 //!Makes initialized pthread_cond_t cleanup easy when using exceptions
98 class condition_initializer
101 condition_initializer(pthread_cond_t &cond, pthread_condattr_t &cond_attr)
104 if(pthread_cond_init(mp_cond, &cond_attr)!= 0)
105 throw interprocess_exception("pthread_cond_init failed");
108 ~condition_initializer() { if(mp_cond) pthread_cond_destroy(mp_cond); }
110 void release() { mp_cond = 0; }
113 pthread_cond_t *mp_cond;
116 #endif // #if defined BOOST_INTERPROCESS_POSIX_PROCESS_SHARED
118 #if defined(BOOST_INTERPROCESS_POSIX_BARRIERS) && defined(BOOST_INTERPROCESS_POSIX_PROCESS_SHARED)
120 //!Makes pthread_barrierattr_t cleanup easy when using exceptions
121 struct barrierattr_wrapper
124 barrierattr_wrapper()
126 if(pthread_barrierattr_init(&m_attr)!=0 ||
127 pthread_barrierattr_setpshared(&m_attr, PTHREAD_PROCESS_SHARED)!= 0)
128 throw interprocess_exception("pthread_barrierattr_xxx failed");
132 ~barrierattr_wrapper() { pthread_barrierattr_destroy(&m_attr); }
134 //!This allows using mutexattr_wrapper as pthread_barrierattr_t
135 operator pthread_barrierattr_t&() { return m_attr; }
137 pthread_barrierattr_t m_attr;
140 //!Makes initialized pthread_barrier_t cleanup easy when using exceptions
141 class barrier_initializer
144 //!Constructor. Takes barrier attributes to initialize the barrier
145 barrier_initializer(pthread_barrier_t &mut,
146 pthread_barrierattr_t &mut_attr,
150 if(pthread_barrier_init(mp_barrier, &mut_attr, count) != 0)
151 throw interprocess_exception("pthread_barrier_init failed");
154 ~barrier_initializer() { if(mp_barrier) pthread_barrier_destroy(mp_barrier); }
156 void release() {mp_barrier = 0; }
159 pthread_barrier_t *mp_barrier;
162 #endif //#if defined(BOOST_INTERPROCESS_POSIX_BARRIERS) && defined(BOOST_INTERPROCESS_POSIX_PROCESS_SHARED)
164 }//namespace ipcdetail
166 }//namespace interprocess
170 #include <boost/interprocess/detail/config_end.hpp>
172 #endif //ifdef BOOST_INTERPROCESS_PTHREAD_HELPERS_HPP