File indexing completed on 2026-09-14 08:47:51
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #ifndef BOOST_INTERPROCESS_PTHREAD_HELPERS_HPP
0012 #define BOOST_INTERPROCESS_PTHREAD_HELPERS_HPP
0013
0014 #ifndef BOOST_CONFIG_HPP
0015 # include <boost/config.hpp>
0016 #endif
0017 0018 ">#
0019 #if defined(BOOST_HAS_PRAGMA_ONCE)
0020 # pragma once
0021 #endif
0022
0023 #include <boost/interprocess/detail/config_begin.hpp>
0024 #include <boost/interprocess/detail/workaround.hpp>
0025
0026 #include <pthread.h>
0027 #include <errno.h>
0028 #include <boost/interprocess/exceptions.hpp>
0029
0030 namespace boost {
0031 namespace interprocess {
0032 namespace ipcdetail{
0033
0034 #if defined BOOST_INTERPROCESS_POSIX_PROCESS_SHARED
0035
0036
0037 struct mutexattr_wrapper
0038 {
0039
0040 mutexattr_wrapper(bool recursive = false)
0041 {
0042 if(pthread_mutexattr_init(&m_attr)!=0 ||
0043 pthread_mutexattr_setpshared(&m_attr, PTHREAD_PROCESS_SHARED)!= 0 ||
0044 (recursive &&
0045 pthread_mutexattr_settype(&m_attr, PTHREAD_MUTEX_RECURSIVE) != 0 )
0046 #ifdef BOOST_INTERPROCESS_POSIX_ROBUST_MUTEXES
0047 || pthread_mutexattr_setrobust(&m_attr, PTHREAD_MUTEX_ROBUST) != 0
0048 #endif
0049 )
0050 throw interprocess_exception("pthread_mutexattr_xxxx failed");
0051 }
0052
0053
0054 ~mutexattr_wrapper() { pthread_mutexattr_destroy(&m_attr); }
0055
0056
0057 operator pthread_mutexattr_t&() { return m_attr; }
0058
0059 pthread_mutexattr_t m_attr;
0060 };
0061
0062
0063 struct condattr_wrapper
0064 {
0065
0066 condattr_wrapper()
0067 {
0068 if(pthread_condattr_init(&m_attr)!=0 ||
0069 pthread_condattr_setpshared(&m_attr, PTHREAD_PROCESS_SHARED)!= 0)
0070 throw interprocess_exception("pthread_condattr_xxxx failed");
0071 }
0072
0073
0074 ~condattr_wrapper() { pthread_condattr_destroy(&m_attr); }
0075
0076
0077 operator pthread_condattr_t&(){ return m_attr; }
0078
0079 pthread_condattr_t m_attr;
0080 };
0081
0082
0083 class mutex_initializer
0084 {
0085 public:
0086
0087 mutex_initializer(pthread_mutex_t &mut, pthread_mutexattr_t &mut_attr)
0088 : mp_mut(&mut)
0089 {
0090 if(pthread_mutex_init(mp_mut, &mut_attr) != 0)
0091 throw interprocess_exception("pthread_mutex_init failed");
0092 }
0093
0094 ~mutex_initializer() { if(mp_mut) pthread_mutex_destroy(mp_mut); }
0095
0096 void release() {mp_mut = 0; }
0097
0098 private:
0099 pthread_mutex_t *mp_mut;
0100 };
0101
0102
0103 class condition_initializer
0104 {
0105 public:
0106 condition_initializer(pthread_cond_t &cond, pthread_condattr_t &cond_attr)
0107 : mp_cond(&cond)
0108 {
0109 if(pthread_cond_init(mp_cond, &cond_attr)!= 0)
0110 throw interprocess_exception("pthread_cond_init failed");
0111 }
0112
0113 ~condition_initializer() { if(mp_cond) pthread_cond_destroy(mp_cond); }
0114
0115 void release() { mp_cond = 0; }
0116
0117 private:
0118 pthread_cond_t *mp_cond;
0119 };
0120
0121 #endif
0122
0123 #if defined(BOOST_INTERPROCESS_POSIX_BARRIERS) && defined(BOOST_INTERPROCESS_POSIX_PROCESS_SHARED)
0124
0125
0126 struct barrierattr_wrapper
0127 {
0128
0129 barrierattr_wrapper()
0130 {
0131 if(pthread_barrierattr_init(&m_attr)!=0 ||
0132 pthread_barrierattr_setpshared(&m_attr, PTHREAD_PROCESS_SHARED)!= 0)
0133 throw interprocess_exception("pthread_barrierattr_xxx failed");
0134 }
0135
0136
0137 ~barrierattr_wrapper() { pthread_barrierattr_destroy(&m_attr); }
0138
0139
0140 operator pthread_barrierattr_t&() { return m_attr; }
0141
0142 pthread_barrierattr_t m_attr;
0143 };
0144
0145
0146 class barrier_initializer
0147 {
0148 public:
0149
0150 barrier_initializer(pthread_barrier_t &mut,
0151 pthread_barrierattr_t &mut_attr,
0152 unsigned int count)
0153 : mp_barrier(&mut)
0154 {
0155 if(pthread_barrier_init(mp_barrier, &mut_attr, count) != 0)
0156 throw interprocess_exception("pthread_barrier_init failed");
0157 }
0158
0159 ~barrier_initializer() { if(mp_barrier) pthread_barrier_destroy(mp_barrier); }
0160
0161 void release() {mp_barrier = 0; }
0162
0163 private:
0164 pthread_barrier_t *mp_barrier;
0165 };
0166
0167 #endif
0168
0169 }
0170
0171 }
0172
0173 }
0174
0175 #include <boost/interprocess/detail/config_end.hpp>
0176
0177 #endif