File indexing completed on 2026-04-07 08:07:50
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #ifndef BOOST_INTERPROCESS_UPGRADABLE_MUTEX_HPP
0014 #define BOOST_INTERPROCESS_UPGRADABLE_MUTEX_HPP
0015
0016 #ifndef BOOST_CONFIG_HPP
0017 # include <boost/config.hpp>
0018 #endif
0019 0020 ">#
0021 #if defined(BOOST_HAS_PRAGMA_ONCE)
0022 # pragma once
0023 #endif
0024
0025 #include <boost/interprocess/detail/config_begin.hpp>
0026 #include <boost/interprocess/detail/workaround.hpp>
0027 #include <boost/interprocess/sync/scoped_lock.hpp>
0028 #include <boost/interprocess/timed_utils.hpp>
0029 #include <boost/interprocess/sync/interprocess_mutex.hpp>
0030 #include <boost/interprocess/sync/interprocess_condition.hpp>
0031 #include <climits>
0032
0033
0034
0035
0036
0037 namespace boost {
0038 namespace interprocess {
0039
0040
0041
0042 class interprocess_upgradable_mutex
0043 {
0044
0045 interprocess_upgradable_mutex(const interprocess_upgradable_mutex &);
0046 interprocess_upgradable_mutex &operator=(const interprocess_upgradable_mutex &);
0047
0048 friend class interprocess_condition;
0049 public:
0050
0051
0052
0053 interprocess_upgradable_mutex();
0054
0055
0056
0057 ~interprocess_upgradable_mutex();
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071 void lock();
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085 bool try_lock();
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098 template<class TimePoint>
0099 bool timed_lock(const TimePoint &abs_time);
0100
0101
0102
0103 template<class TimePoint> bool try_lock_until(const TimePoint &abs_time)
0104 { return this->timed_lock(abs_time); }
0105
0106
0107
0108 template<class Duration> bool try_lock_for(const Duration &dur)
0109 { return this->timed_lock(ipcdetail::duration_to_ustime(dur)); }
0110
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
0190
0191
0192
0193
0194
0195
0196
0197
0198
0199
0200
0201 void lock_upgradable();
0202
0203
0204
0205
0206
0207
0208
0209
0210
0211
0212
0213
0214
0215 bool try_lock_upgradable();
0216
0217
0218
0219
0220
0221
0222
0223
0224
0225
0226
0227
0228 template<class TimePoint>
0229 bool timed_lock_upgradable(const TimePoint &abs_time);
0230
0231
0232
0233
0234 void unlock_upgradable();
0235
0236
0237
0238
0239
0240
0241
0242 void unlock_and_lock_upgradable();
0243
0244
0245
0246
0247
0248 void unlock_and_lock_sharable();
0249
0250
0251
0252
0253
0254 void unlock_upgradable_and_lock_sharable();
0255
0256
0257
0258
0259
0260
0261
0262
0263 void unlock_upgradable_and_lock();
0264
0265
0266
0267
0268
0269
0270
0271 bool try_unlock_upgradable_and_lock();
0272
0273
0274
0275
0276
0277
0278
0279
0280 template<class TimePoint>
0281 bool timed_unlock_upgradable_and_lock(const TimePoint &abs_time);
0282
0283
0284
0285
0286
0287
0288
0289 bool try_unlock_sharable_and_lock();
0290
0291
0292
0293
0294
0295
0296
0297 bool try_unlock_sharable_and_lock_upgradable();
0298
0299 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0300 private:
0301 typedef scoped_lock<interprocess_mutex> scoped_lock_t;
0302
0303
0304
0305 struct control_word_t
0306 {
0307 unsigned exclusive_in : 1;
0308 unsigned upgradable_in : 1;
0309 unsigned num_upr_shar : sizeof(unsigned)*CHAR_BIT-2;
0310 } m_ctrl;
0311
0312 interprocess_mutex m_mut;
0313 interprocess_condition m_first_gate;
0314 interprocess_condition m_second_gate;
0315
0316 private:
0317
0318 struct exclusive_rollback
0319 {
0320 exclusive_rollback(control_word_t &ctrl
0321 ,interprocess_condition &first_gate)
0322 : mp_ctrl(&ctrl), m_first_gate(first_gate)
0323 {}
0324
0325 void release()
0326 { mp_ctrl = 0; }
0327
0328 ~exclusive_rollback()
0329 {
0330 if(mp_ctrl){
0331 mp_ctrl->exclusive_in = 0;
0332 m_first_gate.notify_all();
0333 }
0334 }
0335 control_word_t *mp_ctrl;
0336 interprocess_condition &m_first_gate;
0337 };
0338
0339 struct upgradable_to_exclusive_rollback
0340 {
0341 upgradable_to_exclusive_rollback(control_word_t &ctrl)
0342 : mp_ctrl(&ctrl)
0343 {}
0344
0345 void release()
0346 { mp_ctrl = 0; }
0347
0348 ~upgradable_to_exclusive_rollback()
0349 {
0350 if(mp_ctrl){
0351
0352 mp_ctrl->upgradable_in = 1;
0353 ++mp_ctrl->num_upr_shar;
0354
0355 mp_ctrl->exclusive_in = 0;
0356 }
0357 }
0358 control_word_t *mp_ctrl;
0359 };
0360
0361 template<int Dummy>
0362 struct base_constants_t
0363 {
0364 static const unsigned max_readers
0365 = ~(unsigned(3) << (sizeof(unsigned)*CHAR_BIT-2));
0366 };
0367 typedef base_constants_t<0> constants;
0368 #endif
0369 };
0370
0371 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0372
0373 template <int Dummy>
0374 const unsigned interprocess_upgradable_mutex::base_constants_t<Dummy>::max_readers;
0375
0376 inline interprocess_upgradable_mutex::interprocess_upgradable_mutex()
0377 {
0378 this->m_ctrl.exclusive_in = 0;
0379 this->m_ctrl.upgradable_in = 0;
0380 this->m_ctrl.num_upr_shar = 0;
0381 }
0382
0383 inline interprocess_upgradable_mutex::~interprocess_upgradable_mutex()
0384 {}
0385
0386 inline void interprocess_upgradable_mutex::lock()
0387 {
0388 scoped_lock_t lck(m_mut);
0389
0390
0391
0392 while (this->m_ctrl.exclusive_in || this->m_ctrl.upgradable_in){
0393 this->m_first_gate.wait(lck);
0394 }
0395
0396
0397 this->m_ctrl.exclusive_in = 1;
0398
0399
0400 exclusive_rollback rollback(this->m_ctrl, this->m_first_gate);
0401
0402
0403 while (this->m_ctrl.num_upr_shar){
0404 this->m_second_gate.wait(lck);
0405 }
0406 rollback.release();
0407 }
0408
0409 inline bool interprocess_upgradable_mutex::try_lock()
0410 {
0411 scoped_lock_t lck(m_mut, try_to_lock);
0412
0413
0414
0415 if(!lck.owns()
0416 || this->m_ctrl.exclusive_in
0417 || this->m_ctrl.num_upr_shar){
0418 return false;
0419 }
0420 this->m_ctrl.exclusive_in = 1;
0421 return true;
0422 }
0423
0424 template<class TimePoint>
0425 bool interprocess_upgradable_mutex::timed_lock(const TimePoint &abs_time)
0426 {
0427
0428
0429 scoped_lock_t lck(m_mut, abs_time);
0430 if(!lck.owns()) return false;
0431
0432
0433
0434 while (this->m_ctrl.exclusive_in || this->m_ctrl.upgradable_in){
0435 if(!this->m_first_gate.timed_wait(lck, abs_time)){
0436 if(this->m_ctrl.exclusive_in || this->m_ctrl.upgradable_in){
0437 return false;
0438 }
0439 break;
0440 }
0441 }
0442
0443
0444 this->m_ctrl.exclusive_in = 1;
0445
0446
0447 exclusive_rollback rollback(this->m_ctrl, this->m_first_gate);
0448
0449
0450 while (this->m_ctrl.num_upr_shar){
0451 if(!this->m_second_gate.timed_wait(lck, abs_time)){
0452 if(this->m_ctrl.num_upr_shar){
0453 return false;
0454 }
0455 break;
0456 }
0457 }
0458 rollback.release();
0459 return true;
0460 }
0461
0462 inline void interprocess_upgradable_mutex::unlock()
0463 {
0464 scoped_lock_t lck(m_mut);
0465 this->m_ctrl.exclusive_in = 0;
0466 this->m_first_gate.notify_all();
0467 }
0468
0469
0470
0471 inline void interprocess_upgradable_mutex::lock_upgradable()
0472 {
0473 scoped_lock_t lck(m_mut);
0474
0475
0476
0477
0478 while(this->m_ctrl.exclusive_in || this->m_ctrl.upgradable_in
0479 || this->m_ctrl.num_upr_shar == constants::max_readers){
0480 this->m_first_gate.wait(lck);
0481 }
0482
0483
0484
0485 this->m_ctrl.upgradable_in = 1;
0486 ++this->m_ctrl.num_upr_shar;
0487 }
0488
0489 inline bool interprocess_upgradable_mutex::try_lock_upgradable()
0490 {
0491 scoped_lock_t lck(m_mut, try_to_lock);
0492
0493
0494
0495
0496 if(!lck.owns()
0497 || this->m_ctrl.exclusive_in
0498 || this->m_ctrl.upgradable_in
0499 || this->m_ctrl.num_upr_shar == constants::max_readers){
0500 return false;
0501 }
0502
0503
0504
0505 this->m_ctrl.upgradable_in = 1;
0506 ++this->m_ctrl.num_upr_shar;
0507 return true;
0508 }
0509
0510 template<class TimePoint>
0511 bool interprocess_upgradable_mutex::timed_lock_upgradable(const TimePoint &abs_time)
0512 {
0513
0514
0515 scoped_lock_t lck(m_mut, abs_time);
0516 if(!lck.owns()) return false;
0517
0518
0519
0520
0521 while(this->m_ctrl.exclusive_in
0522 || this->m_ctrl.upgradable_in
0523 || this->m_ctrl.num_upr_shar == constants::max_readers){
0524 if(!this->m_first_gate.timed_wait(lck, abs_time)){
0525 if((this->m_ctrl.exclusive_in
0526 || this->m_ctrl.upgradable_in
0527 || this->m_ctrl.num_upr_shar == constants::max_readers)){
0528 return false;
0529 }
0530 break;
0531 }
0532 }
0533
0534
0535
0536 this->m_ctrl.upgradable_in = 1;
0537 ++this->m_ctrl.num_upr_shar;
0538 return true;
0539 }
0540
0541 inline void interprocess_upgradable_mutex::unlock_upgradable()
0542 {
0543 scoped_lock_t lck(m_mut);
0544
0545
0546 this->m_ctrl.upgradable_in = 0;
0547 --this->m_ctrl.num_upr_shar;
0548 this->m_first_gate.notify_all();
0549 }
0550
0551
0552
0553 inline void interprocess_upgradable_mutex::lock_sharable()
0554 {
0555 scoped_lock_t lck(m_mut);
0556
0557
0558
0559
0560 while(this->m_ctrl.exclusive_in
0561 || this->m_ctrl.num_upr_shar == constants::max_readers){
0562 this->m_first_gate.wait(lck);
0563 }
0564
0565
0566 ++this->m_ctrl.num_upr_shar;
0567 }
0568
0569 inline bool interprocess_upgradable_mutex::try_lock_sharable()
0570 {
0571 scoped_lock_t lck(m_mut, try_to_lock);
0572
0573
0574
0575
0576 if(!lck.owns()
0577 || this->m_ctrl.exclusive_in
0578 || this->m_ctrl.num_upr_shar == constants::max_readers){
0579 return false;
0580 }
0581
0582
0583 ++this->m_ctrl.num_upr_shar;
0584 return true;
0585 }
0586
0587 template<class TimePoint>
0588 inline bool interprocess_upgradable_mutex::timed_lock_sharable(const TimePoint &abs_time)
0589 {
0590
0591
0592 scoped_lock_t lck(m_mut, abs_time);
0593 if(!lck.owns()) return false;
0594
0595
0596
0597
0598 while (this->m_ctrl.exclusive_in
0599 || this->m_ctrl.num_upr_shar == constants::max_readers){
0600 if(!this->m_first_gate.timed_wait(lck, abs_time)){
0601 if(this->m_ctrl.exclusive_in
0602 || this->m_ctrl.num_upr_shar == constants::max_readers){
0603 return false;
0604 }
0605 break;
0606 }
0607 }
0608
0609
0610 ++this->m_ctrl.num_upr_shar;
0611 return true;
0612 }
0613
0614 inline void interprocess_upgradable_mutex::unlock_sharable()
0615 {
0616 scoped_lock_t lck(m_mut);
0617
0618 --this->m_ctrl.num_upr_shar;
0619 if (this->m_ctrl.num_upr_shar == 0){
0620 this->m_second_gate.notify_one();
0621 }
0622
0623
0624 else if(this->m_ctrl.num_upr_shar == (constants::max_readers-1)){
0625 this->m_first_gate.notify_all();
0626 }
0627 }
0628
0629
0630
0631 inline void interprocess_upgradable_mutex::unlock_and_lock_upgradable()
0632 {
0633 scoped_lock_t lck(m_mut);
0634
0635 this->m_ctrl.exclusive_in = 0;
0636
0637 this->m_ctrl.upgradable_in = 1;
0638
0639 this->m_ctrl.num_upr_shar = 1;
0640
0641 m_first_gate.notify_all();
0642 }
0643
0644 inline void interprocess_upgradable_mutex::unlock_and_lock_sharable()
0645 {
0646 scoped_lock_t lck(m_mut);
0647
0648 this->m_ctrl.exclusive_in = 0;
0649
0650 this->m_ctrl.num_upr_shar = 1;
0651
0652 m_first_gate.notify_all();
0653 }
0654
0655 inline void interprocess_upgradable_mutex::unlock_upgradable_and_lock_sharable()
0656 {
0657 scoped_lock_t lck(m_mut);
0658
0659 this->m_ctrl.upgradable_in = 0;
0660
0661 m_first_gate.notify_all();
0662 }
0663
0664
0665
0666 inline void interprocess_upgradable_mutex::unlock_upgradable_and_lock()
0667 {
0668 scoped_lock_t lck(m_mut);
0669
0670
0671 this->m_ctrl.upgradable_in = 0;
0672 --this->m_ctrl.num_upr_shar;
0673
0674 this->m_ctrl.exclusive_in = 1;
0675
0676
0677 upgradable_to_exclusive_rollback rollback(m_ctrl);
0678
0679 while (this->m_ctrl.num_upr_shar){
0680 this->m_second_gate.wait(lck);
0681 }
0682 rollback.release();
0683 }
0684
0685 inline bool interprocess_upgradable_mutex::try_unlock_upgradable_and_lock()
0686 {
0687 scoped_lock_t lck(m_mut, try_to_lock);
0688
0689 if(!lck.owns()
0690 || this->m_ctrl.num_upr_shar != 1){
0691 return false;
0692 }
0693
0694 this->m_ctrl.upgradable_in = 0;
0695 --this->m_ctrl.num_upr_shar;
0696 this->m_ctrl.exclusive_in = 1;
0697 return true;
0698 }
0699
0700 template<class TimePoint>
0701 bool interprocess_upgradable_mutex::timed_unlock_upgradable_and_lock(const TimePoint &abs_time)
0702 {
0703
0704
0705 scoped_lock_t lck(m_mut, abs_time);
0706 if(!lck.owns()) return false;
0707
0708
0709
0710 this->m_ctrl.upgradable_in = 0;
0711 --this->m_ctrl.num_upr_shar;
0712
0713 this->m_ctrl.exclusive_in = 1;
0714
0715
0716 upgradable_to_exclusive_rollback rollback(m_ctrl);
0717
0718 while (this->m_ctrl.num_upr_shar){
0719 if(!this->m_second_gate.timed_wait(lck, abs_time)){
0720 if(this->m_ctrl.num_upr_shar){
0721 return false;
0722 }
0723 break;
0724 }
0725 }
0726 rollback.release();
0727 return true;
0728 }
0729
0730 inline bool interprocess_upgradable_mutex::try_unlock_sharable_and_lock()
0731 {
0732 scoped_lock_t lck(m_mut, try_to_lock);
0733
0734
0735
0736 if(!lck.owns()
0737 || this->m_ctrl.exclusive_in
0738 || this->m_ctrl.upgradable_in
0739 || this->m_ctrl.num_upr_shar != 1){
0740 return false;
0741 }
0742 this->m_ctrl.exclusive_in = 1;
0743 this->m_ctrl.num_upr_shar = 0;
0744 return true;
0745 }
0746
0747 inline bool interprocess_upgradable_mutex::try_unlock_sharable_and_lock_upgradable()
0748 {
0749 scoped_lock_t lck(m_mut, try_to_lock);
0750
0751
0752
0753 if(!lck.owns()
0754 || this->m_ctrl.exclusive_in
0755 || this->m_ctrl.upgradable_in){
0756 return false;
0757 }
0758
0759
0760 this->m_ctrl.upgradable_in = 1;
0761 return true;
0762 }
0763
0764 #endif
0765
0766 }
0767 }
0768
0769
0770 #include <boost/interprocess/detail/config_end.hpp>
0771
0772 #endif