1 //////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Ion Gaztanaga 2011-2012. Distributed under the Boost
4 // Software License, Version 1.0. (See accompanying file
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 // See http://www.boost.org/libs/interprocess for documentation.
9 //////////////////////////////////////////////////////////////////////////////
10 #ifndef BOOST_INTERPROCESS_WINDOWS_NAMED_MUTEX_HPP
11 #define BOOST_INTERPROCESS_WINDOWS_NAMED_MUTEX_HPP
13 #ifndef BOOST_CONFIG_HPP
14 # include <boost/config.hpp>
17 #if defined(BOOST_HAS_PRAGMA_ONCE)
21 #include <boost/interprocess/detail/config_begin.hpp>
22 #include <boost/interprocess/detail/workaround.hpp>
23 #include <boost/interprocess/creation_tags.hpp>
24 #include <boost/interprocess/permissions.hpp>
25 #include <boost/interprocess/detail/interprocess_tester.hpp>
26 #include <boost/interprocess/detail/posix_time_types_wrk.hpp>
27 #include <boost/interprocess/sync/windows/sync_utils.hpp>
28 #include <boost/interprocess/sync/windows/named_sync.hpp>
29 #include <boost/interprocess/sync/windows/winapi_mutex_wrapper.hpp>
30 #include <boost/interprocess/errors.hpp>
31 #include <boost/interprocess/exceptions.hpp>
35 namespace interprocess {
40 class windows_named_mutex
42 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
45 windows_named_mutex();
46 windows_named_mutex(const windows_named_mutex &);
47 windows_named_mutex &operator=(const windows_named_mutex &);
48 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
51 windows_named_mutex(create_only_t, const char *name, const permissions &perm = permissions());
53 windows_named_mutex(open_or_create_t, const char *name, const permissions &perm = permissions());
55 windows_named_mutex(open_only_t, const char *name);
57 ~windows_named_mutex();
62 bool timed_lock(const boost::posix_time::ptime &abs_time);
64 static bool remove(const char *name);
66 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
68 friend class interprocess_tester;
69 void dont_close_on_destruction();
70 winapi_mutex_wrapper m_mtx_wrapper;
71 windows_named_sync m_named_sync;
73 class named_mut_callbacks : public windows_named_sync_interface
76 named_mut_callbacks(winapi_mutex_wrapper &mtx_wrapper)
77 : m_mtx_wrapper(mtx_wrapper)
80 virtual std::size_t get_data_size() const
83 virtual const void *buffer_with_init_data_to_file()
86 virtual const void *buffer_with_final_data_to_file()
89 virtual void *buffer_to_store_init_data_from_file()
92 virtual bool open(create_enum_t, const char *id_name)
94 std::string aux_str = "Global\\bipc.mut.";
98 mut_perm.set_unrestricted();
99 return m_mtx_wrapper.open_or_create(aux_str.c_str(), mut_perm);
104 m_mtx_wrapper.close();
107 virtual ~named_mut_callbacks()
111 winapi_mutex_wrapper& m_mtx_wrapper;
113 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
116 inline windows_named_mutex::~windows_named_mutex()
118 named_mut_callbacks callbacks(m_mtx_wrapper);
119 m_named_sync.close(callbacks);
122 inline void windows_named_mutex::dont_close_on_destruction()
125 inline windows_named_mutex::windows_named_mutex
126 (create_only_t, const char *name, const permissions &perm)
129 named_mut_callbacks callbacks(m_mtx_wrapper);
130 m_named_sync.open_or_create(DoCreate, name, perm, callbacks);
133 inline windows_named_mutex::windows_named_mutex
134 (open_or_create_t, const char *name, const permissions &perm)
137 named_mut_callbacks callbacks(m_mtx_wrapper);
138 m_named_sync.open_or_create(DoOpenOrCreate, name, perm, callbacks);
141 inline windows_named_mutex::windows_named_mutex(open_only_t, const char *name)
144 named_mut_callbacks callbacks(m_mtx_wrapper);
145 m_named_sync.open_or_create(DoOpen, name, permissions(), callbacks);
148 inline void windows_named_mutex::unlock()
150 m_mtx_wrapper.unlock();
153 inline void windows_named_mutex::lock()
155 m_mtx_wrapper.lock();
158 inline bool windows_named_mutex::try_lock()
160 return m_mtx_wrapper.try_lock();
163 inline bool windows_named_mutex::timed_lock(const boost::posix_time::ptime &abs_time)
165 return m_mtx_wrapper.timed_lock(abs_time);
168 inline bool windows_named_mutex::remove(const char *name)
170 return windows_named_sync::remove(name);
173 } //namespace ipcdetail {
174 } //namespace interprocess {
175 } //namespace boost {
177 #include <boost/interprocess/detail/config_end.hpp>
179 #endif //BOOST_INTERPROCESS_WINDOWS_NAMED_MUTEX_HPP