File indexing completed on 2026-04-09 08:07:32
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #ifndef BOOST_INTERPROCESS_SHM_NAMED_MUTEX_HPP
0012 #define BOOST_INTERPROCESS_SHM_NAMED_MUTEX_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 #include <boost/interprocess/creation_tags.hpp>
0026 #include <boost/interprocess/exceptions.hpp>
0027 #include <boost/interprocess/detail/interprocess_tester.hpp>
0028 #include <boost/interprocess/permissions.hpp>
0029
0030 #include <boost/interprocess/shared_memory_object.hpp>
0031 #include <boost/interprocess/sync/interprocess_mutex.hpp>
0032 #include <boost/interprocess/detail/managed_open_or_create_impl.hpp>
0033 #include <boost/interprocess/sync/shm/named_creation_functor.hpp>
0034 #include <boost/interprocess/timed_utils.hpp>
0035
0036
0037
0038
0039 namespace boost {
0040 namespace interprocess {
0041 namespace ipcdetail {
0042
0043 class named_condition;
0044
0045
0046
0047
0048 class shm_named_mutex
0049 {
0050 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0051
0052
0053 shm_named_mutex();
0054 shm_named_mutex(const shm_named_mutex &);
0055 shm_named_mutex &operator=(const shm_named_mutex &);
0056 friend class named_condition;
0057 #endif
0058
0059 public:
0060
0061
0062 shm_named_mutex(create_only_t, const char *name, const permissions &perm = permissions());
0063
0064
0065
0066
0067
0068
0069
0070 shm_named_mutex(open_or_create_t, const char *name, const permissions &perm = permissions());
0071
0072
0073
0074
0075 shm_named_mutex(open_only_t, const char *name);
0076
0077 #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0078
0079
0080
0081 shm_named_mutex(create_only_t, const wchar_t *name, const permissions &perm = permissions());
0082
0083
0084
0085
0086
0087
0088
0089 shm_named_mutex(open_or_create_t, const wchar_t *name, const permissions &perm = permissions());
0090
0091
0092
0093
0094 shm_named_mutex(open_only_t, const wchar_t *name);
0095
0096 #endif
0097
0098
0099
0100
0101
0102
0103
0104 ~shm_named_mutex();
0105
0106
0107
0108 void unlock();
0109
0110
0111
0112 void lock();
0113
0114
0115
0116
0117 bool try_lock();
0118
0119
0120
0121
0122 template<class TimePoint>
0123 bool timed_lock(const TimePoint &abs_time);
0124
0125 template<class TimePoint> bool try_lock_until(const TimePoint &abs_time)
0126 { return this->timed_lock(abs_time); }
0127
0128 template<class Duration> bool try_lock_for(const Duration &dur)
0129 { return this->timed_lock(duration_to_ustime(dur)); }
0130
0131
0132
0133 static bool remove(const char *name);
0134
0135 #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0136
0137
0138
0139 static bool remove(const wchar_t *name);
0140
0141 #endif
0142
0143 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0144 typedef interprocess_mutex internal_mutex_type;
0145 interprocess_mutex &internal_mutex()
0146 { return *static_cast<interprocess_mutex*>(m_shmem.get_user_address()); }
0147
0148 private:
0149 friend class ipcdetail::interprocess_tester;
0150 void dont_close_on_destruction();
0151 typedef ipcdetail::managed_open_or_create_impl<shared_memory_object, 0, true, false> open_create_impl_t;
0152 open_create_impl_t m_shmem;
0153 typedef ipcdetail::named_creation_functor<interprocess_mutex> construct_func_t;
0154 #endif
0155 };
0156
0157 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0158
0159 inline void shm_named_mutex::dont_close_on_destruction()
0160 { ipcdetail::interprocess_tester::dont_close_on_destruction(m_shmem); }
0161
0162 inline shm_named_mutex::~shm_named_mutex()
0163 {}
0164
0165 inline shm_named_mutex::shm_named_mutex(create_only_t, const char *name, const permissions &perm)
0166 : m_shmem (create_only_t()
0167 ,name
0168 ,sizeof(interprocess_mutex) +
0169 open_create_impl_t::ManagedOpenOrCreateUserOffset
0170 ,read_write
0171 ,0
0172 ,construct_func_t(ipcdetail::DoCreate)
0173 ,perm)
0174 {}
0175
0176 inline shm_named_mutex::shm_named_mutex(open_or_create_t, const char *name, const permissions &perm)
0177 : m_shmem (open_or_create_t()
0178 ,name
0179 ,sizeof(interprocess_mutex) +
0180 open_create_impl_t::ManagedOpenOrCreateUserOffset
0181 ,read_write
0182 ,0
0183 ,construct_func_t(ipcdetail::DoOpenOrCreate)
0184 ,perm)
0185 {}
0186
0187 inline shm_named_mutex::shm_named_mutex(open_only_t, const char *name)
0188 : m_shmem (open_only_t()
0189 ,name
0190 ,read_write
0191 ,0
0192 ,construct_func_t(ipcdetail::DoOpen))
0193 {}
0194
0195 #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0196
0197 inline shm_named_mutex::shm_named_mutex(create_only_t, const wchar_t *name, const permissions &perm)
0198 : m_shmem (create_only_t()
0199 ,name
0200 ,sizeof(interprocess_mutex) +
0201 open_create_impl_t::ManagedOpenOrCreateUserOffset
0202 ,read_write
0203 ,0
0204 ,construct_func_t(ipcdetail::DoCreate)
0205 ,perm)
0206 {}
0207
0208 inline shm_named_mutex::shm_named_mutex(open_or_create_t, const wchar_t *name, const permissions &perm)
0209 : m_shmem (open_or_create_t()
0210 ,name
0211 ,sizeof(interprocess_mutex) +
0212 open_create_impl_t::ManagedOpenOrCreateUserOffset
0213 ,read_write
0214 ,0
0215 ,construct_func_t(ipcdetail::DoOpenOrCreate)
0216 ,perm)
0217 {}
0218
0219 inline shm_named_mutex::shm_named_mutex(open_only_t, const wchar_t *name)
0220 : m_shmem (open_only_t()
0221 ,name
0222 ,read_write
0223 ,0
0224 ,construct_func_t(ipcdetail::DoOpen))
0225 {}
0226
0227 #endif
0228
0229 inline void shm_named_mutex::lock()
0230 { this->internal_mutex().lock(); }
0231
0232 inline void shm_named_mutex::unlock()
0233 { this->internal_mutex().unlock(); }
0234
0235 inline bool shm_named_mutex::try_lock()
0236 { return this->internal_mutex().try_lock(); }
0237
0238 template<class TimePoint>
0239 inline bool shm_named_mutex::timed_lock(const TimePoint &abs_time)
0240 { return this->internal_mutex().timed_lock(abs_time); }
0241
0242 inline bool shm_named_mutex::remove(const char *name)
0243 { return shared_memory_object::remove(name); }
0244
0245 #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0246
0247 inline bool shm_named_mutex::remove(const wchar_t *name)
0248 { return shared_memory_object::remove(name); }
0249
0250 #endif
0251
0252 #endif
0253
0254 }
0255 }
0256 }
0257
0258 #include <boost/interprocess/detail/config_end.hpp>
0259
0260 #endif