File indexing completed on 2025-07-15 08:36:51
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015 #ifndef BOOST_INTERPROCESS_SHARABLE_MUTEX_HPP
0016 #define BOOST_INTERPROCESS_SHARABLE_MUTEX_HPP
0017
0018 #ifndef BOOST_CONFIG_HPP
0019 # include <boost/config.hpp>
0020 #endif
0021 #
0022 #if defined(BOOST_HAS_PRAGMA_ONCE)
0023 # pragma once
0024 #endif
0025
0026 #include <boost/interprocess/detail/config_begin.hpp>
0027 #include <boost/interprocess/detail/workaround.hpp>
0028 #include <boost/interprocess/sync/scoped_lock.hpp>
0029 #include <boost/interprocess/timed_utils.hpp>
0030 #include <boost/interprocess/sync/interprocess_mutex.hpp>
0031 #include <boost/interprocess/sync/interprocess_condition.hpp>
0032 #include <climits>
0033
0034
0035
0036
0037
0038 namespace boost {
0039 namespace interprocess {
0040
0041
0042
0043 class interprocess_sharable_mutex
0044 {
0045
0046 interprocess_sharable_mutex(const interprocess_sharable_mutex &);
0047 interprocess_sharable_mutex &operator=(const interprocess_sharable_mutex &);
0048
0049 friend class interprocess_condition;
0050 public:
0051
0052
0053
0054 interprocess_sharable_mutex();
0055
0056
0057
0058 ~interprocess_sharable_mutex();
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072 void lock();
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086 bool try_lock();
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099 template<class TimePoint>
0100 bool timed_lock(const TimePoint &abs_time);
0101
0102
0103
0104 template<class TimePoint> bool try_lock_until(const TimePoint &abs_time)
0105 { return this->timed_lock(abs_time); }
0106
0107
0108
0109 template<class Duration> bool try_lock_for(const Duration &dur)
0110 { return this->timed_lock(ipcdetail::duration_to_ustime(dur)); }
0111
0112
0113
0114
0115 void unlock();
0116
0117
0118
0119
0120
0121
0122
0123
0124
0125
0126
0127
0128
0129 void lock_sharable();
0130
0131
0132
0133 void lock_shared()
0134 { this->lock_sharable(); }
0135
0136
0137
0138
0139
0140
0141
0142
0143
0144
0145
0146
0147
0148 bool try_lock_sharable();
0149
0150
0151
0152 bool try_lock_shared()
0153 { return this->try_lock_sharable(); }
0154
0155
0156
0157
0158
0159
0160
0161
0162
0163
0164
0165
0166 template<class TimePoint>
0167 bool timed_lock_sharable(const TimePoint &abs_time);
0168
0169
0170
0171 template<class TimePoint> bool try_lock_shared_until(const TimePoint &abs_time)
0172 { return this->timed_lock_sharable(abs_time); }
0173
0174
0175
0176 template<class Duration> bool try_lock_shared_for(const Duration &dur)
0177 { return this->timed_lock_sharable(ipcdetail::duration_to_ustime(dur)); }
0178
0179
0180
0181
0182 void unlock_sharable();
0183
0184
0185
0186 void unlock_shared()
0187 { this->unlock_sharable(); }
0188
0189 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0190 private:
0191 typedef scoped_lock<interprocess_mutex> scoped_lock_t;
0192
0193
0194
0195 struct control_word_t
0196 {
0197 unsigned exclusive_in : 1;
0198 unsigned num_shared : sizeof(unsigned)*CHAR_BIT-1;
0199 } m_ctrl;
0200
0201 interprocess_mutex m_mut;
0202 interprocess_condition m_first_gate;
0203 interprocess_condition m_second_gate;
0204
0205 private:
0206
0207 struct exclusive_rollback
0208 {
0209 exclusive_rollback(control_word_t &ctrl
0210 ,interprocess_condition &first_gate)
0211 : mp_ctrl(&ctrl), m_first_gate(first_gate)
0212 {}
0213
0214 void release()
0215 { mp_ctrl = 0; }
0216
0217 ~exclusive_rollback()
0218 {
0219 if(mp_ctrl){
0220 mp_ctrl->exclusive_in = 0;
0221 m_first_gate.notify_all();
0222 }
0223 }
0224 control_word_t *mp_ctrl;
0225 interprocess_condition &m_first_gate;
0226 };
0227
0228 template<int Dummy>
0229 struct base_constants_t
0230 {
0231 static const unsigned max_readers
0232 = ~(unsigned(1) << (sizeof(unsigned)*CHAR_BIT-1));
0233 };
0234 typedef base_constants_t<0> constants;
0235 #endif
0236 };
0237
0238 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0239
0240 template <int Dummy>
0241 const unsigned interprocess_sharable_mutex::base_constants_t<Dummy>::max_readers;
0242
0243 inline interprocess_sharable_mutex::interprocess_sharable_mutex()
0244 {
0245 this->m_ctrl.exclusive_in = 0;
0246 this->m_ctrl.num_shared = 0;
0247 }
0248
0249 inline interprocess_sharable_mutex::~interprocess_sharable_mutex()
0250 {}
0251
0252 inline void interprocess_sharable_mutex::lock()
0253 {
0254 scoped_lock_t lck(m_mut);
0255
0256
0257
0258 while (this->m_ctrl.exclusive_in){
0259 this->m_first_gate.wait(lck);
0260 }
0261
0262
0263 this->m_ctrl.exclusive_in = 1;
0264
0265
0266 exclusive_rollback rollback(this->m_ctrl, this->m_first_gate);
0267
0268
0269 while (this->m_ctrl.num_shared){
0270 this->m_second_gate.wait(lck);
0271 }
0272 rollback.release();
0273 }
0274
0275 inline bool interprocess_sharable_mutex::try_lock()
0276 {
0277 scoped_lock_t lck(m_mut, try_to_lock);
0278
0279
0280
0281 if(!lck.owns()
0282 || this->m_ctrl.exclusive_in
0283 || this->m_ctrl.num_shared){
0284 return false;
0285 }
0286 this->m_ctrl.exclusive_in = 1;
0287 return true;
0288 }
0289
0290 template<class TimePoint>
0291 inline bool interprocess_sharable_mutex::timed_lock
0292 (const TimePoint &abs_time)
0293 {
0294 scoped_lock_t lck(m_mut, abs_time);
0295 if(!lck.owns()) return false;
0296
0297
0298
0299 while (this->m_ctrl.exclusive_in){
0300
0301
0302 if(!this->m_first_gate.timed_wait(lck, abs_time)){
0303 if(this->m_ctrl.exclusive_in){
0304 return false;
0305 }
0306 break;
0307 }
0308 }
0309
0310
0311 this->m_ctrl.exclusive_in = 1;
0312
0313
0314 exclusive_rollback rollback(this->m_ctrl, this->m_first_gate);
0315
0316
0317 while (this->m_ctrl.num_shared){
0318
0319
0320 if(!this->m_second_gate.timed_wait(lck, abs_time)){
0321 if(this->m_ctrl.num_shared){
0322 return false;
0323 }
0324 break;
0325 }
0326 }
0327 rollback.release();
0328 return true;
0329 }
0330
0331 inline void interprocess_sharable_mutex::unlock()
0332 {
0333 scoped_lock_t lck(m_mut);
0334 this->m_ctrl.exclusive_in = 0;
0335 this->m_first_gate.notify_all();
0336 }
0337
0338
0339
0340 inline void interprocess_sharable_mutex::lock_sharable()
0341 {
0342 scoped_lock_t lck(m_mut);
0343
0344
0345
0346
0347 while(this->m_ctrl.exclusive_in
0348 || this->m_ctrl.num_shared == constants::max_readers){
0349 this->m_first_gate.wait(lck);
0350 }
0351
0352
0353 ++this->m_ctrl.num_shared;
0354 }
0355
0356 inline bool interprocess_sharable_mutex::try_lock_sharable()
0357 {
0358 scoped_lock_t lck(m_mut, try_to_lock);
0359
0360
0361
0362
0363 if(!lck.owns()
0364 || this->m_ctrl.exclusive_in
0365 || this->m_ctrl.num_shared == constants::max_readers){
0366 return false;
0367 }
0368
0369
0370 ++this->m_ctrl.num_shared;
0371 return true;
0372 }
0373
0374 template<class TimePoint>
0375 inline bool interprocess_sharable_mutex::timed_lock_sharable
0376 (const TimePoint &abs_time)
0377 {
0378 scoped_lock_t lck(m_mut, abs_time);
0379 if(!lck.owns()) return false;
0380
0381
0382
0383
0384 while (this->m_ctrl.exclusive_in
0385 || this->m_ctrl.num_shared == constants::max_readers){
0386
0387
0388 if(!this->m_first_gate.timed_wait(lck, abs_time)){
0389 if(this->m_ctrl.exclusive_in
0390 || this->m_ctrl.num_shared == constants::max_readers){
0391 return false;
0392 }
0393 break;
0394 }
0395 }
0396
0397
0398 ++this->m_ctrl.num_shared;
0399 return true;
0400 }
0401
0402 inline void interprocess_sharable_mutex::unlock_sharable()
0403 {
0404 scoped_lock_t lck(m_mut);
0405
0406 --this->m_ctrl.num_shared;
0407 if (this->m_ctrl.num_shared == 0){
0408 this->m_second_gate.notify_one();
0409 }
0410
0411
0412 else if(this->m_ctrl.num_shared == (constants::max_readers-1)){
0413 this->m_first_gate.notify_all();
0414 }
0415 }
0416
0417 #endif
0418
0419 }
0420 }
0421
0422 #include <boost/interprocess/detail/config_end.hpp>
0423
0424 #endif