1 //////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Ion Gaztanaga 2005-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 //////////////////////////////////////////////////////////////////////////////
11 #ifndef BOOST_INTERPROCESS_SHM_NAMED_MUTEX_HPP
12 #define BOOST_INTERPROCESS_SHM_NAMED_MUTEX_HPP
14 #ifndef BOOST_CONFIG_HPP
15 # include <boost/config.hpp>
18 #if defined(BOOST_HAS_PRAGMA_ONCE)
22 #include <boost/interprocess/detail/config_begin.hpp>
23 #include <boost/interprocess/detail/workaround.hpp>
24 #include <boost/interprocess/creation_tags.hpp>
25 #include <boost/interprocess/exceptions.hpp>
26 #include <boost/interprocess/detail/interprocess_tester.hpp>
27 #include <boost/interprocess/permissions.hpp>
29 #include <boost/interprocess/shared_memory_object.hpp>
30 #include <boost/interprocess/sync/interprocess_mutex.hpp>
31 #include <boost/interprocess/detail/managed_open_or_create_impl.hpp>
32 #include <boost/interprocess/sync/shm/named_creation_functor.hpp>
35 //!Describes a named mutex class for inter-process synchronization
38 namespace interprocess {
41 class named_condition;
43 //!A mutex with a global name, so it can be found from different
44 //!processes. This mutex can't be placed in shared memory, and
45 //!each process should have it's own named mutex.
48 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
52 shm_named_mutex(const shm_named_mutex &);
53 shm_named_mutex &operator=(const shm_named_mutex &);
54 friend class named_condition;
55 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
58 //!Creates a global interprocess_mutex with a name.
59 //!Throws interprocess_exception on error.
60 shm_named_mutex(create_only_t, const char *name, const permissions &perm = permissions());
62 //!Opens or creates a global mutex with a name.
63 //!If the mutex is created, this call is equivalent to
64 //!shm_named_mutex(create_only_t, ... )
65 //!If the mutex is already created, this call is equivalent
66 //!shm_named_mutex(open_only_t, ... )
68 shm_named_mutex(open_or_create_t, const char *name, const permissions &perm = permissions());
70 //!Opens a global mutex with a name if that mutex is previously
71 //!created. If it is not previously created this function throws
72 //!interprocess_exception.
73 shm_named_mutex(open_only_t, const char *name);
75 #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
77 //!Creates a global interprocess_mutex with a name.
78 //!Throws interprocess_exception on error.
79 shm_named_mutex(create_only_t, const wchar_t *name, const permissions &perm = permissions());
81 //!Opens or creates a global mutex with a name.
82 //!If the mutex is created, this call is equivalent to
83 //!shm_named_mutex(create_only_t, ... )
84 //!If the mutex is already created, this call is equivalent
85 //!shm_named_mutex(open_only_t, ... )
87 shm_named_mutex(open_or_create_t, const wchar_t *name, const permissions &perm = permissions());
89 //!Opens a global mutex with a name if that mutex is previously
90 //!created. If it is not previously created this function throws
91 //!interprocess_exception.
92 shm_named_mutex(open_only_t, const wchar_t *name);
94 #endif //defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
96 //!Destroys *this and indicates that the calling process is finished using
97 //!the resource. The destructor function will deallocate
98 //!any system resources allocated by the system for use by this process for
99 //!this resource. The resource can still be opened again calling
100 //!the open constructor overload. To erase the resource from the system
104 //!Unlocks a previously locked
105 //!interprocess_mutex.
108 //!Locks interprocess_mutex, sleeps when interprocess_mutex is already locked.
109 //!Throws interprocess_exception if a severe error is found
112 //!Tries to lock the interprocess_mutex, returns false when interprocess_mutex
113 //!is already locked, returns true when success.
114 //!Throws interprocess_exception if a severe error is found
117 //!Tries to lock the interprocess_mutex until time abs_time,
118 //!Returns false when timeout expires, returns true when locks.
119 //!Throws interprocess_exception if a severe error is found
120 template<class TimePoint>
121 bool timed_lock(const TimePoint &abs_time);
123 template<class TimePoint> bool try_lock_until(const TimePoint &abs_time)
124 { return this->timed_lock(abs_time); }
126 template<class Duration> bool try_lock_for(const Duration &dur)
127 { return this->timed_lock(duration_to_ustime(dur)); }
129 //!Erases a named mutex from the system.
130 //!Returns false on error. Never throws.
131 static bool remove(const char *name);
133 #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
135 //!Erases a named mutex from the system.
136 //!Returns false on error. Never throws.
137 static bool remove(const wchar_t *name);
141 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
142 typedef interprocess_mutex internal_mutex_type;
143 interprocess_mutex &internal_mutex()
144 { return *static_cast<interprocess_mutex*>(m_shmem.get_user_address()); }
147 friend class ipcdetail::interprocess_tester;
148 void dont_close_on_destruction();
149 typedef ipcdetail::managed_open_or_create_impl<shared_memory_object, 0, true, false> open_create_impl_t;
150 open_create_impl_t m_shmem;
151 typedef ipcdetail::named_creation_functor<interprocess_mutex> construct_func_t;
152 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
155 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
157 inline void shm_named_mutex::dont_close_on_destruction()
158 { ipcdetail::interprocess_tester::dont_close_on_destruction(m_shmem); }
160 inline shm_named_mutex::~shm_named_mutex()
163 inline shm_named_mutex::shm_named_mutex(create_only_t, const char *name, const permissions &perm)
164 : m_shmem (create_only_t()
166 ,sizeof(interprocess_mutex) +
167 open_create_impl_t::ManagedOpenOrCreateUserOffset
170 ,construct_func_t(ipcdetail::DoCreate)
174 inline shm_named_mutex::shm_named_mutex(open_or_create_t, const char *name, const permissions &perm)
175 : m_shmem (open_or_create_t()
177 ,sizeof(interprocess_mutex) +
178 open_create_impl_t::ManagedOpenOrCreateUserOffset
181 ,construct_func_t(ipcdetail::DoOpenOrCreate)
185 inline shm_named_mutex::shm_named_mutex(open_only_t, const char *name)
186 : m_shmem (open_only_t()
190 ,construct_func_t(ipcdetail::DoOpen))
193 #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
195 inline shm_named_mutex::shm_named_mutex(create_only_t, const wchar_t *name, const permissions &perm)
196 : m_shmem (create_only_t()
198 ,sizeof(interprocess_mutex) +
199 open_create_impl_t::ManagedOpenOrCreateUserOffset
202 ,construct_func_t(ipcdetail::DoCreate)
206 inline shm_named_mutex::shm_named_mutex(open_or_create_t, const wchar_t *name, const permissions &perm)
207 : m_shmem (open_or_create_t()
209 ,sizeof(interprocess_mutex) +
210 open_create_impl_t::ManagedOpenOrCreateUserOffset
213 ,construct_func_t(ipcdetail::DoOpenOrCreate)
217 inline shm_named_mutex::shm_named_mutex(open_only_t, const wchar_t *name)
218 : m_shmem (open_only_t()
222 ,construct_func_t(ipcdetail::DoOpen))
225 #endif //defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
227 inline void shm_named_mutex::lock()
228 { this->internal_mutex().lock(); }
230 inline void shm_named_mutex::unlock()
231 { this->internal_mutex().unlock(); }
233 inline bool shm_named_mutex::try_lock()
234 { return this->internal_mutex().try_lock(); }
236 template<class TimePoint>
237 inline bool shm_named_mutex::timed_lock(const TimePoint &abs_time)
238 { return this->internal_mutex().timed_lock(abs_time); }
240 inline bool shm_named_mutex::remove(const char *name)
241 { return shared_memory_object::remove(name); }
243 #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
245 inline bool shm_named_mutex::remove(const wchar_t *name)
246 { return shared_memory_object::remove(name); }
248 #endif //defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
250 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
252 } //namespace ipcdetail {
253 } //namespace interprocess {
254 } //namespace boost {
256 #include <boost/interprocess/detail/config_end.hpp>
258 #endif //BOOST_INTERPROCESS_SHM_NAMED_MUTEX_HPP