File indexing completed on 2025-07-09 08:13:45
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #ifndef BOOST_INTERPROCESS_NAMED_SHARABLE_MUTEX_HPP
0012 #define BOOST_INTERPROCESS_NAMED_SHARABLE_MUTEX_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 #include <boost/interprocess/creation_tags.hpp>
0025 #include <boost/interprocess/exceptions.hpp>
0026 #include <boost/interprocess/shared_memory_object.hpp>
0027 #include <boost/interprocess/timed_utils.hpp>
0028 #include <boost/interprocess/detail/managed_open_or_create_impl.hpp>
0029 #include <boost/interprocess/sync/interprocess_sharable_mutex.hpp>
0030 #include <boost/interprocess/sync/shm/named_creation_functor.hpp>
0031 #include <boost/interprocess/permissions.hpp>
0032
0033
0034
0035
0036 namespace boost {
0037 namespace interprocess {
0038
0039 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0040 namespace ipcdetail{ class interprocess_tester; }
0041 #endif
0042
0043 class named_condition;
0044
0045
0046
0047
0048 class named_sharable_mutex
0049 {
0050 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0051
0052 named_sharable_mutex();
0053 named_sharable_mutex(const named_sharable_mutex &);
0054 named_sharable_mutex &operator=(const named_sharable_mutex &);
0055 #endif
0056 public:
0057
0058
0059
0060 named_sharable_mutex(create_only_t, const char *name, const permissions &perm = permissions());
0061
0062
0063
0064
0065
0066
0067 named_sharable_mutex(open_or_create_t, const char *name, const permissions &perm = permissions());
0068
0069
0070
0071
0072
0073 named_sharable_mutex(open_only_t, const char *name);
0074
0075 #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0076
0077
0078
0079
0080
0081
0082 named_sharable_mutex(create_only_t, const wchar_t *name, const permissions &perm = permissions());
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092 named_sharable_mutex(open_or_create_t, const wchar_t *name, const permissions &perm = permissions());
0093
0094
0095
0096
0097
0098
0099
0100
0101 named_sharable_mutex(open_only_t, const wchar_t *name);
0102
0103 #endif
0104
0105
0106
0107
0108
0109
0110
0111 ~named_sharable_mutex();
0112
0113
0114
0115
0116
0117
0118
0119
0120
0121
0122
0123
0124
0125 void lock();
0126
0127
0128
0129
0130
0131
0132
0133
0134
0135
0136
0137
0138
0139 bool try_lock();
0140
0141
0142
0143
0144
0145
0146
0147
0148
0149
0150
0151
0152 template<class TimePoint>
0153 bool timed_lock(const TimePoint &abs_time);
0154
0155
0156
0157 template<class TimePoint> bool try_lock_until(const TimePoint &abs_time)
0158 { return this->timed_lock(abs_time); }
0159
0160
0161
0162 template<class Duration> bool try_lock_for(const Duration &dur)
0163 { return this->timed_lock(ipcdetail::duration_to_ustime(dur)); }
0164
0165
0166
0167
0168 void unlock();
0169
0170
0171
0172
0173
0174
0175
0176
0177
0178
0179
0180
0181
0182 void lock_sharable();
0183
0184
0185
0186 void lock_shared()
0187 { this->lock_sharable(); }
0188
0189
0190
0191
0192
0193
0194
0195
0196
0197
0198
0199
0200
0201 bool try_lock_sharable();
0202
0203
0204
0205 bool try_lock_shared()
0206 { return this->try_lock_sharable(); }
0207
0208
0209
0210
0211
0212
0213
0214
0215
0216
0217
0218
0219 template<class TimePoint>
0220 bool timed_lock_sharable(const TimePoint &abs_time);
0221
0222
0223
0224 template<class TimePoint> bool try_lock_shared_until(const TimePoint &abs_time)
0225 { return this->timed_lock_sharable(abs_time); }
0226
0227
0228
0229 template<class Duration> bool try_lock_shared_for(const Duration &dur)
0230 { return this->timed_lock_sharable(ipcdetail::duration_to_ustime(dur)); }
0231
0232
0233
0234
0235 void unlock_sharable();
0236
0237
0238
0239 void unlock_shared()
0240 { this->unlock_sharable(); }
0241
0242
0243
0244 static bool remove(const char *name);
0245
0246 #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0247
0248
0249
0250 static bool remove(const wchar_t *name);
0251
0252 #endif
0253
0254 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0255 private:
0256 friend class ipcdetail::interprocess_tester;
0257 void dont_close_on_destruction();
0258
0259 interprocess_sharable_mutex *mutex() const
0260 { return static_cast<interprocess_sharable_mutex*>(m_shmem.get_user_address()); }
0261
0262 typedef ipcdetail::managed_open_or_create_impl<shared_memory_object, 0, true, false> open_create_impl_t;
0263 open_create_impl_t m_shmem;
0264 typedef ipcdetail::named_creation_functor<interprocess_sharable_mutex> construct_func_t;
0265 #endif
0266 };
0267
0268 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0269
0270 inline named_sharable_mutex::~named_sharable_mutex()
0271 {}
0272
0273 inline named_sharable_mutex::named_sharable_mutex
0274 (create_only_t, const char *name, const permissions &perm)
0275 : m_shmem (create_only
0276 ,name
0277 ,sizeof(interprocess_sharable_mutex) +
0278 open_create_impl_t::ManagedOpenOrCreateUserOffset
0279 ,read_write
0280 ,0
0281 ,construct_func_t(ipcdetail::DoCreate)
0282 ,perm)
0283 {}
0284
0285 inline named_sharable_mutex::named_sharable_mutex
0286 (open_or_create_t, const char *name, const permissions &perm)
0287 : m_shmem (open_or_create
0288 ,name
0289 ,sizeof(interprocess_sharable_mutex) +
0290 open_create_impl_t::ManagedOpenOrCreateUserOffset
0291 ,read_write
0292 ,0
0293 ,construct_func_t(ipcdetail::DoOpenOrCreate)
0294 ,perm)
0295 {}
0296
0297 inline named_sharable_mutex::named_sharable_mutex
0298 (open_only_t, const char *name)
0299 : m_shmem (open_only
0300 ,name
0301 ,read_write
0302 ,0
0303 ,construct_func_t(ipcdetail::DoOpen))
0304 {}
0305
0306 #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0307
0308 inline named_sharable_mutex::named_sharable_mutex
0309 (create_only_t, const wchar_t *name, const permissions &perm)
0310 : m_shmem (create_only
0311 ,name
0312 ,sizeof(interprocess_sharable_mutex) +
0313 open_create_impl_t::ManagedOpenOrCreateUserOffset
0314 ,read_write
0315 ,0
0316 ,construct_func_t(ipcdetail::DoCreate)
0317 ,perm)
0318 {}
0319
0320 inline named_sharable_mutex::named_sharable_mutex
0321 (open_or_create_t, const wchar_t *name, const permissions &perm)
0322 : m_shmem (open_or_create
0323 ,name
0324 ,sizeof(interprocess_sharable_mutex) +
0325 open_create_impl_t::ManagedOpenOrCreateUserOffset
0326 ,read_write
0327 ,0
0328 ,construct_func_t(ipcdetail::DoOpenOrCreate)
0329 ,perm)
0330 {}
0331
0332 inline named_sharable_mutex::named_sharable_mutex
0333 (open_only_t, const wchar_t *name)
0334 : m_shmem (open_only
0335 ,name
0336 ,read_write
0337 ,0
0338 ,construct_func_t(ipcdetail::DoOpen))
0339 {}
0340
0341 #endif
0342
0343 inline void named_sharable_mutex::dont_close_on_destruction()
0344 { ipcdetail::interprocess_tester::dont_close_on_destruction(m_shmem); }
0345
0346 inline void named_sharable_mutex::lock()
0347 { this->mutex()->lock(); }
0348
0349 inline void named_sharable_mutex::unlock()
0350 { this->mutex()->unlock(); }
0351
0352 inline bool named_sharable_mutex::try_lock()
0353 { return this->mutex()->try_lock(); }
0354
0355 template<class TimePoint>
0356 inline bool named_sharable_mutex::timed_lock
0357 (const TimePoint &abs_time)
0358 { return this->mutex()->timed_lock(abs_time); }
0359
0360 inline void named_sharable_mutex::lock_sharable()
0361 { this->mutex()->lock_sharable(); }
0362
0363 inline void named_sharable_mutex::unlock_sharable()
0364 { this->mutex()->unlock_sharable(); }
0365
0366 inline bool named_sharable_mutex::try_lock_sharable()
0367 { return this->mutex()->try_lock_sharable(); }
0368
0369 template<class TimePoint>
0370 inline bool named_sharable_mutex::timed_lock_sharable
0371 (const TimePoint &abs_time)
0372 { return this->mutex()->timed_lock_sharable(abs_time); }
0373
0374 inline bool named_sharable_mutex::remove(const char *name)
0375 { return shared_memory_object::remove(name); }
0376
0377 #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0378
0379 inline bool named_sharable_mutex::remove(const wchar_t *name)
0380 { return shared_memory_object::remove(name); }
0381
0382 #endif
0383
0384 #endif
0385
0386 }
0387 }
0388
0389 #include <boost/interprocess/detail/config_end.hpp>
0390
0391 #endif