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