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