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 #ifdef BOOST_INTERPROCESS_POSIX_ROBUST_MUTEXES
46 || pthread_mutexattr_setrobust(&m_attr, PTHREAD_MUTEX_ROBUST) != 0
49 throw interprocess_exception("pthread_mutexattr_xxxx failed");
53 ~mutexattr_wrapper() { pthread_mutexattr_destroy(&m_attr); }
55 //!This allows using mutexattr_wrapper as pthread_mutexattr_t
56 operator pthread_mutexattr_t&() { return m_attr; }
58 pthread_mutexattr_t m_attr;
61 //!Makes pthread_condattr_t cleanup easy when using exceptions
62 struct condattr_wrapper
67 if(pthread_condattr_init(&m_attr)!=0 ||
68 pthread_condattr_setpshared(&m_attr, PTHREAD_PROCESS_SHARED)!= 0)
69 throw interprocess_exception("pthread_condattr_xxxx failed");
73 ~condattr_wrapper() { pthread_condattr_destroy(&m_attr); }
75 //!This allows using condattr_wrapper as pthread_condattr_t
76 operator pthread_condattr_t&(){ return m_attr; }
78 pthread_condattr_t m_attr;
81 //!Makes initialized pthread_mutex_t cleanup easy when using exceptions
82 class mutex_initializer
85 //!Constructor. Takes interprocess_mutex attributes to initialize the interprocess_mutex
86 mutex_initializer(pthread_mutex_t &mut, pthread_mutexattr_t &mut_attr)
89 if(pthread_mutex_init(mp_mut, &mut_attr) != 0)
90 throw interprocess_exception("pthread_mutex_init failed");
93 ~mutex_initializer() { if(mp_mut) pthread_mutex_destroy(mp_mut); }
95 void release() {mp_mut = 0; }
98 pthread_mutex_t *mp_mut;
101 //!Makes initialized pthread_cond_t cleanup easy when using exceptions
102 class condition_initializer
105 condition_initializer(pthread_cond_t &cond, pthread_condattr_t &cond_attr)
108 if(pthread_cond_init(mp_cond, &cond_attr)!= 0)
109 throw interprocess_exception("pthread_cond_init failed");
112 ~condition_initializer() { if(mp_cond) pthread_cond_destroy(mp_cond); }
114 void release() { mp_cond = 0; }
117 pthread_cond_t *mp_cond;
120 #endif // #if defined BOOST_INTERPROCESS_POSIX_PROCESS_SHARED
122 #if defined(BOOST_INTERPROCESS_POSIX_BARRIERS) && defined(BOOST_INTERPROCESS_POSIX_PROCESS_SHARED)
124 //!Makes pthread_barrierattr_t cleanup easy when using exceptions
125 struct barrierattr_wrapper
128 barrierattr_wrapper()
130 if(pthread_barrierattr_init(&m_attr)!=0 ||
131 pthread_barrierattr_setpshared(&m_attr, PTHREAD_PROCESS_SHARED)!= 0)
132 throw interprocess_exception("pthread_barrierattr_xxx failed");
136 ~barrierattr_wrapper() { pthread_barrierattr_destroy(&m_attr); }
138 //!This allows using mutexattr_wrapper as pthread_barrierattr_t
139 operator pthread_barrierattr_t&() { return m_attr; }
141 pthread_barrierattr_t m_attr;
144 //!Makes initialized pthread_barrier_t cleanup easy when using exceptions
145 class barrier_initializer
148 //!Constructor. Takes barrier attributes to initialize the barrier
149 barrier_initializer(pthread_barrier_t &mut,
150 pthread_barrierattr_t &mut_attr,
154 if(pthread_barrier_init(mp_barrier, &mut_attr, count) != 0)
155 throw interprocess_exception("pthread_barrier_init failed");
158 ~barrier_initializer() { if(mp_barrier) pthread_barrier_destroy(mp_barrier); }
160 void release() {mp_barrier = 0; }
163 pthread_barrier_t *mp_barrier;
166 #endif //#if defined(BOOST_INTERPROCESS_POSIX_BARRIERS) && defined(BOOST_INTERPROCESS_POSIX_PROCESS_SHARED)
168 }//namespace ipcdetail
170 }//namespace interprocess
174 #include <boost/interprocess/detail/config_end.hpp>
176 #endif //ifdef BOOST_INTERPROCESS_PTHREAD_HELPERS_HPP