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