File indexing completed on 2025-07-11 08:13:56
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #ifndef BOOST_INTERPROCESS_WINDOWS_NAMED_MUTEX_HPP
0011 #define BOOST_INTERPROCESS_WINDOWS_NAMED_MUTEX_HPP
0012
0013 #ifndef BOOST_CONFIG_HPP
0014 # include <boost/config.hpp>
0015 #endif
0016 #
0017 #if defined(BOOST_HAS_PRAGMA_ONCE)
0018 # pragma once
0019 #endif
0020
0021 #include <boost/interprocess/detail/config_begin.hpp>
0022 #include <boost/interprocess/detail/workaround.hpp>
0023 #include <boost/interprocess/creation_tags.hpp>
0024 #include <boost/interprocess/permissions.hpp>
0025 #include <boost/interprocess/detail/interprocess_tester.hpp>
0026 #include <boost/interprocess/sync/windows/sync_utils.hpp>
0027 #include <boost/interprocess/sync/windows/named_sync.hpp>
0028 #include <boost/interprocess/sync/windows/winapi_mutex_wrapper.hpp>
0029 #include <boost/interprocess/errors.hpp>
0030 #include <boost/interprocess/exceptions.hpp>
0031 #include <boost/interprocess/timed_utils.hpp>
0032 #include <limits>
0033
0034 namespace boost {
0035 namespace interprocess {
0036 namespace ipcdetail {
0037
0038
0039
0040 class winapi_named_mutex
0041 {
0042 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0043
0044
0045 winapi_named_mutex();
0046 winapi_named_mutex(const winapi_named_mutex &);
0047 winapi_named_mutex &operator=(const winapi_named_mutex &);
0048 #endif
0049
0050 public:
0051 winapi_named_mutex(create_only_t, const char *name, const permissions &perm = permissions());
0052
0053 winapi_named_mutex(open_or_create_t, const char *name, const permissions &perm = permissions());
0054
0055 winapi_named_mutex(open_only_t, const char *name);
0056
0057 winapi_named_mutex(create_only_t, const wchar_t *name, const permissions &perm = permissions());
0058
0059 winapi_named_mutex(open_or_create_t, const wchar_t *name, const permissions &perm = permissions());
0060
0061 winapi_named_mutex(open_only_t, const wchar_t *name);
0062
0063 ~winapi_named_mutex();
0064
0065 void unlock();
0066 void lock();
0067 bool try_lock();
0068 template<class TimePoint> bool timed_lock(const TimePoint &abs_time);
0069
0070 template<class TimePoint> bool try_lock_until(const TimePoint &abs_time)
0071 { return this->timed_lock(abs_time); }
0072
0073 template<class Duration> bool try_lock_for(const Duration &dur)
0074 { return this->timed_lock(duration_to_ustime(dur)); }
0075
0076 static bool remove(const char *name);
0077
0078 static bool remove(const wchar_t *name);
0079
0080 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0081 private:
0082 friend class interprocess_tester;
0083 void dont_close_on_destruction();
0084 winapi_mutex_wrapper m_mtx_wrapper;
0085 windows_named_sync m_named_sync;
0086
0087 class named_mut_callbacks : public windows_named_sync_interface
0088 {
0089 public:
0090 named_mut_callbacks(winapi_mutex_wrapper &mtx_wrapper)
0091 : m_mtx_wrapper(mtx_wrapper)
0092 {}
0093
0094 virtual std::size_t get_data_size() const BOOST_OVERRIDE
0095 { return 0u; }
0096
0097 virtual const void *buffer_with_init_data_to_file() BOOST_OVERRIDE
0098 { return 0; }
0099
0100 virtual const void *buffer_with_final_data_to_file() BOOST_OVERRIDE
0101 { return 0; }
0102
0103 virtual void *buffer_to_store_init_data_from_file() BOOST_OVERRIDE
0104 { return 0; }
0105
0106 virtual bool open(create_enum_t, const char *id_name) BOOST_OVERRIDE
0107 {
0108 std::string aux_str = "Global\\bipc.mut.";
0109 aux_str += id_name;
0110
0111 permissions mut_perm;
0112 mut_perm.set_unrestricted();
0113 return m_mtx_wrapper.open_or_create(aux_str.c_str(), mut_perm);
0114 }
0115
0116 virtual bool open(create_enum_t, const wchar_t *id_name) BOOST_OVERRIDE
0117 {
0118 std::wstring aux_str = L"Global\\bipc.mut.";
0119 aux_str += id_name;
0120
0121 permissions mut_perm;
0122 mut_perm.set_unrestricted();
0123 return m_mtx_wrapper.open_or_create(aux_str.c_str(), mut_perm);
0124 }
0125
0126 virtual void close() BOOST_OVERRIDE
0127 {
0128 m_mtx_wrapper.close();
0129 }
0130
0131 virtual ~named_mut_callbacks() BOOST_OVERRIDE
0132 {}
0133
0134 private:
0135 winapi_mutex_wrapper& m_mtx_wrapper;
0136 };
0137 #endif
0138 };
0139
0140 inline winapi_named_mutex::~winapi_named_mutex()
0141 {
0142 named_mut_callbacks callbacks(m_mtx_wrapper);
0143 m_named_sync.close(callbacks);
0144 }
0145
0146 inline void winapi_named_mutex::dont_close_on_destruction()
0147 {}
0148
0149 inline winapi_named_mutex::winapi_named_mutex
0150 (create_only_t, const char *name, const permissions &perm)
0151 : m_mtx_wrapper()
0152 {
0153 named_mut_callbacks callbacks(m_mtx_wrapper);
0154 m_named_sync.open_or_create(DoCreate, name, perm, callbacks);
0155 }
0156
0157 inline winapi_named_mutex::winapi_named_mutex
0158 (open_or_create_t, const char *name, const permissions &perm)
0159 : m_mtx_wrapper()
0160 {
0161 named_mut_callbacks callbacks(m_mtx_wrapper);
0162 m_named_sync.open_or_create(DoOpenOrCreate, name, perm, callbacks);
0163 }
0164
0165 inline winapi_named_mutex::winapi_named_mutex(open_only_t, const char *name)
0166 : m_mtx_wrapper()
0167 {
0168 named_mut_callbacks callbacks(m_mtx_wrapper);
0169 m_named_sync.open_or_create(DoOpen, name, permissions(), callbacks);
0170 }
0171
0172 inline winapi_named_mutex::winapi_named_mutex
0173 (create_only_t, const wchar_t *name, const permissions &perm)
0174 : m_mtx_wrapper()
0175 {
0176 named_mut_callbacks callbacks(m_mtx_wrapper);
0177 m_named_sync.open_or_create(DoCreate, name, perm, callbacks);
0178 }
0179
0180 inline winapi_named_mutex::winapi_named_mutex
0181 (open_or_create_t, const wchar_t *name, const permissions &perm)
0182 : m_mtx_wrapper()
0183 {
0184 named_mut_callbacks callbacks(m_mtx_wrapper);
0185 m_named_sync.open_or_create(DoOpenOrCreate, name, perm, callbacks);
0186 }
0187
0188 inline winapi_named_mutex::winapi_named_mutex(open_only_t, const wchar_t *name)
0189 : m_mtx_wrapper()
0190 {
0191 named_mut_callbacks callbacks(m_mtx_wrapper);
0192 m_named_sync.open_or_create(DoOpen, name, permissions(), callbacks);
0193 }
0194
0195 inline void winapi_named_mutex::unlock()
0196 {
0197 m_mtx_wrapper.unlock();
0198 }
0199
0200 inline void winapi_named_mutex::lock()
0201 {
0202 m_mtx_wrapper.lock();
0203 }
0204
0205 inline bool winapi_named_mutex::try_lock()
0206 {
0207 return m_mtx_wrapper.try_lock();
0208 }
0209
0210 template<class TimePoint>
0211 inline bool winapi_named_mutex::timed_lock(const TimePoint &abs_time)
0212 {
0213 return m_mtx_wrapper.timed_lock(abs_time);
0214 }
0215
0216 inline bool winapi_named_mutex::remove(const char *name)
0217 {
0218 return windows_named_sync::remove(name);
0219 }
0220
0221 inline bool winapi_named_mutex::remove(const wchar_t *name)
0222 {
0223 return windows_named_sync::remove(name);
0224 }
0225
0226 }
0227 }
0228 }
0229
0230 #include <boost/interprocess/detail/config_end.hpp>
0231
0232 #endif