]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/interprocess/sync/posix/pthread_helpers.hpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / boost / boost / interprocess / sync / posix / pthread_helpers.hpp
1 //////////////////////////////////////////////////////////////////////////////
2 //
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)
6 //
7 // See http://www.boost.org/libs/interprocess for documentation.
8 //
9 //////////////////////////////////////////////////////////////////////////////
10
11 #ifndef BOOST_INTERPROCESS_PTHREAD_HELPERS_HPP
12 #define BOOST_INTERPROCESS_PTHREAD_HELPERS_HPP
13
14 #ifndef BOOST_CONFIG_HPP
15 # include <boost/config.hpp>
16 #endif
17 #
18 #if defined(BOOST_HAS_PRAGMA_ONCE)
19 # pragma once
20 #endif
21
22 #include <boost/interprocess/detail/config_begin.hpp>
23 #include <boost/interprocess/detail/workaround.hpp>
24
25 #include <pthread.h>
26 #include <errno.h>
27 #include <boost/interprocess/exceptions.hpp>
28
29 namespace boost {
30 namespace interprocess {
31 namespace ipcdetail{
32
33 #if defined BOOST_INTERPROCESS_POSIX_PROCESS_SHARED
34
35 //!Makes pthread_mutexattr_t cleanup easy when using exceptions
36 struct mutexattr_wrapper
37 {
38 //!Constructor
39 mutexattr_wrapper(bool recursive = false)
40 {
41 if(pthread_mutexattr_init(&m_attr)!=0 ||
42 pthread_mutexattr_setpshared(&m_attr, PTHREAD_PROCESS_SHARED)!= 0 ||
43 (recursive &&
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
47 #endif
48 )
49 throw interprocess_exception("pthread_mutexattr_xxxx failed");
50 }
51
52 //!Destructor
53 ~mutexattr_wrapper() { pthread_mutexattr_destroy(&m_attr); }
54
55 //!This allows using mutexattr_wrapper as pthread_mutexattr_t
56 operator pthread_mutexattr_t&() { return m_attr; }
57
58 pthread_mutexattr_t m_attr;
59 };
60
61 //!Makes pthread_condattr_t cleanup easy when using exceptions
62 struct condattr_wrapper
63 {
64 //!Constructor
65 condattr_wrapper()
66 {
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");
70 }
71
72 //!Destructor
73 ~condattr_wrapper() { pthread_condattr_destroy(&m_attr); }
74
75 //!This allows using condattr_wrapper as pthread_condattr_t
76 operator pthread_condattr_t&(){ return m_attr; }
77
78 pthread_condattr_t m_attr;
79 };
80
81 //!Makes initialized pthread_mutex_t cleanup easy when using exceptions
82 class mutex_initializer
83 {
84 public:
85 //!Constructor. Takes interprocess_mutex attributes to initialize the interprocess_mutex
86 mutex_initializer(pthread_mutex_t &mut, pthread_mutexattr_t &mut_attr)
87 : mp_mut(&mut)
88 {
89 if(pthread_mutex_init(mp_mut, &mut_attr) != 0)
90 throw interprocess_exception("pthread_mutex_init failed");
91 }
92
93 ~mutex_initializer() { if(mp_mut) pthread_mutex_destroy(mp_mut); }
94
95 void release() {mp_mut = 0; }
96
97 private:
98 pthread_mutex_t *mp_mut;
99 };
100
101 //!Makes initialized pthread_cond_t cleanup easy when using exceptions
102 class condition_initializer
103 {
104 public:
105 condition_initializer(pthread_cond_t &cond, pthread_condattr_t &cond_attr)
106 : mp_cond(&cond)
107 {
108 if(pthread_cond_init(mp_cond, &cond_attr)!= 0)
109 throw interprocess_exception("pthread_cond_init failed");
110 }
111
112 ~condition_initializer() { if(mp_cond) pthread_cond_destroy(mp_cond); }
113
114 void release() { mp_cond = 0; }
115
116 private:
117 pthread_cond_t *mp_cond;
118 };
119
120 #endif // #if defined BOOST_INTERPROCESS_POSIX_PROCESS_SHARED
121
122 #if defined(BOOST_INTERPROCESS_POSIX_BARRIERS) && defined(BOOST_INTERPROCESS_POSIX_PROCESS_SHARED)
123
124 //!Makes pthread_barrierattr_t cleanup easy when using exceptions
125 struct barrierattr_wrapper
126 {
127 //!Constructor
128 barrierattr_wrapper()
129 {
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");
133 }
134
135 //!Destructor
136 ~barrierattr_wrapper() { pthread_barrierattr_destroy(&m_attr); }
137
138 //!This allows using mutexattr_wrapper as pthread_barrierattr_t
139 operator pthread_barrierattr_t&() { return m_attr; }
140
141 pthread_barrierattr_t m_attr;
142 };
143
144 //!Makes initialized pthread_barrier_t cleanup easy when using exceptions
145 class barrier_initializer
146 {
147 public:
148 //!Constructor. Takes barrier attributes to initialize the barrier
149 barrier_initializer(pthread_barrier_t &mut,
150 pthread_barrierattr_t &mut_attr,
151 unsigned int count)
152 : mp_barrier(&mut)
153 {
154 if(pthread_barrier_init(mp_barrier, &mut_attr, count) != 0)
155 throw interprocess_exception("pthread_barrier_init failed");
156 }
157
158 ~barrier_initializer() { if(mp_barrier) pthread_barrier_destroy(mp_barrier); }
159
160 void release() {mp_barrier = 0; }
161
162 private:
163 pthread_barrier_t *mp_barrier;
164 };
165
166 #endif //#if defined(BOOST_INTERPROCESS_POSIX_BARRIERS) && defined(BOOST_INTERPROCESS_POSIX_PROCESS_SHARED)
167
168 }//namespace ipcdetail
169
170 }//namespace interprocess
171
172 }//namespace boost
173
174 #include <boost/interprocess/detail/config_end.hpp>
175
176 #endif //ifdef BOOST_INTERPROCESS_PTHREAD_HELPERS_HPP