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