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_NAMED_RECURSIVE_MUTEX_HPP
12 #define BOOST_INTERPROCESS_NAMED_RECURSIVE_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/permissions.hpp>
27 #if !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION) && defined (BOOST_INTERPROCESS_WINDOWS)
28 #include <boost/interprocess/sync/windows/named_recursive_mutex.hpp>
29 #define BOOST_INTERPROCESS_NAMED_RECURSIVE_MUTEX_USE_WINAPI
31 #include <boost/interprocess/sync/shm/named_recursive_mutex.hpp>
35 //!Describes a named named_recursive_mutex class for inter-process synchronization
38 namespace interprocess {
40 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
41 namespace ipcdetail{ class interprocess_tester; }
42 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
44 //!A recursive mutex with a global name, so it can be found from different
45 //!processes. This mutex can't be placed in shared memory, and
46 //!each process should have it's own named_recursive_mutex.
47 class named_recursive_mutex
49 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
51 named_recursive_mutex();
52 named_recursive_mutex(const named_recursive_mutex &);
53 named_recursive_mutex &operator=(const named_recursive_mutex &);
54 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
57 //!Creates a global recursive_mutex with a name.
58 //!If the recursive_mutex can't be created throws interprocess_exception
59 named_recursive_mutex(create_only_t, const char *name, const permissions &perm = permissions());
61 //!Opens or creates a global recursive_mutex with a name.
62 //!If the recursive_mutex is created, this call is equivalent to
63 //!named_recursive_mutex(create_only_t, ... )
64 //!If the recursive_mutex is already created, this call is equivalent
65 //!named_recursive_mutex(open_only_t, ... )
67 named_recursive_mutex(open_or_create_t, const char *name, const permissions &perm = permissions());
69 //!Opens a global recursive_mutex with a name if that recursive_mutex is previously
70 //!created. If it is not previously created this function throws
71 //!interprocess_exception.
72 named_recursive_mutex(open_only_t, const char *name);
74 #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
76 //!Creates a global recursive_mutex with a name.
77 //!If the recursive_mutex can't be created throws interprocess_exception
79 //!Note: This function is only available on operating systems with
80 //! native wchar_t APIs (e.g. Windows).
81 named_recursive_mutex(create_only_t, const wchar_t *name, const permissions &perm = permissions());
83 //!Opens or creates a global recursive_mutex with a name.
84 //!If the recursive_mutex is created, this call is equivalent to
85 //!named_recursive_mutex(create_only_t, ... )
86 //!If the recursive_mutex is already created, this call is equivalent
87 //!named_recursive_mutex(open_only_t, ... )
90 //!Note: This function is only available on operating systems with
91 //! native wchar_t APIs (e.g. Windows).
92 named_recursive_mutex(open_or_create_t, const wchar_t *name, const permissions &perm = permissions());
94 //!Opens a global recursive_mutex with a name if that recursive_mutex is previously
95 //!created. If it is not previously created this function throws
96 //!interprocess_exception.
98 //!Note: This function is only available on operating systems with
99 //! native wchar_t APIs (e.g. Windows).
100 named_recursive_mutex(open_only_t, const wchar_t *name);
102 #endif //defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
104 //!Destroys *this and indicates that the calling process is finished using
105 //!the resource. The destructor function will deallocate
106 //!any system resources allocated by the system for use by this process for
107 //!this resource. The resource can still be opened again calling
108 //!the open constructor overload. To erase the resource from the system
110 ~named_recursive_mutex();
112 //!Unlocks a previously locked
113 //!named_recursive_mutex.
116 //!Locks named_recursive_mutex, sleeps when named_recursive_mutex is already locked.
117 //!Throws interprocess_exception if a severe error is found.
119 //!Note: A program shall not deadlock if the thread that has ownership calls
123 //!Tries to lock the named_recursive_mutex, returns false when named_recursive_mutex
124 //!is already locked, returns true when success.
125 //!Throws interprocess_exception if a severe error is found.
127 //!Note: A program shall not deadlock if the thread that has ownership calls
131 //!Tries to lock the named_recursive_mutex until time abs_time,
132 //!Returns false when timeout expires, returns true when locks.
133 //!Throws interprocess_exception if a severe error is found
135 //!Note: A program shall not deadlock if the thread that has ownership calls
137 template<class TimePoint>
138 bool timed_lock(const TimePoint &abs_time);
140 //!Same as `timed_lock`, but this function is modeled after the
141 //!standard library interface.
142 template<class TimePoint> bool try_lock_until(const TimePoint &abs_time)
143 { return this->timed_lock(abs_time); }
145 //!Same as `timed_lock`, but this function is modeled after the
146 //!standard library interface.
147 template<class Duration> bool try_lock_for(const Duration &dur)
148 { return this->timed_lock(ipcdetail::duration_to_ustime(dur)); }
150 //!Erases a named recursive mutex
152 static bool remove(const char *name);
154 #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
155 //!Erases a named recursive mutex
158 //!Note: This function is only available on operating systems with
159 //! native wchar_t APIs (e.g. Windows).
160 static bool remove(const wchar_t *name);
161 #endif //defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
163 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
165 friend class ipcdetail::interprocess_tester;
166 void dont_close_on_destruction();
168 #if defined(BOOST_INTERPROCESS_NAMED_RECURSIVE_MUTEX_USE_WINAPI)
169 typedef ipcdetail::winapi_named_recursive_mutex impl_t;
171 typedef ipcdetail::shm_named_recursive_mutex impl_t;
175 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
178 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
180 inline named_recursive_mutex::~named_recursive_mutex()
183 inline void named_recursive_mutex::dont_close_on_destruction()
184 { ipcdetail::interprocess_tester::dont_close_on_destruction(m_mut); }
186 inline named_recursive_mutex::named_recursive_mutex(create_only_t, const char *name, const permissions &perm)
187 : m_mut (create_only, name, perm)
190 inline named_recursive_mutex::named_recursive_mutex(open_or_create_t, const char *name, const permissions &perm)
191 : m_mut (open_or_create, name, perm)
194 inline named_recursive_mutex::named_recursive_mutex(open_only_t, const char *name)
195 : m_mut (open_only, name)
198 #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
200 inline named_recursive_mutex::named_recursive_mutex(create_only_t, const wchar_t *name, const permissions &perm)
201 : m_mut (create_only, name, perm)
204 inline named_recursive_mutex::named_recursive_mutex(open_or_create_t, const wchar_t *name, const permissions &perm)
205 : m_mut (open_or_create, name, perm)
208 inline named_recursive_mutex::named_recursive_mutex(open_only_t, const wchar_t *name)
209 : m_mut (open_only, name)
212 #endif //defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
214 inline void named_recursive_mutex::lock()
217 inline void named_recursive_mutex::unlock()
220 inline bool named_recursive_mutex::try_lock()
221 { return m_mut.try_lock(); }
223 template<class TimePoint>
224 inline bool named_recursive_mutex::timed_lock(const TimePoint &abs_time)
225 { return m_mut.timed_lock(abs_time); }
227 inline bool named_recursive_mutex::remove(const char *name)
228 { return impl_t::remove(name); }
230 #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
232 inline bool named_recursive_mutex::remove(const wchar_t *name)
233 { return impl_t::remove(name); }
235 #endif //defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
237 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
239 } //namespace interprocess {
240 } //namespace boost {
242 #include <boost/interprocess/detail/config_end.hpp>
244 #endif //BOOST_INTERPROCESS_NAMED_RECURSIVE_MUTEX_HPP