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_MUTEX_HPP
12 #define BOOST_INTERPROCESS_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/detail/posix_time_types_wrk.hpp>
28 #include <boost/interprocess/permissions.hpp>
30 #if defined(BOOST_INTERPROCESS_NAMED_MUTEX_USES_POSIX_SEMAPHORES)
31 #include <boost/interprocess/sync/posix/named_mutex.hpp>
32 #define BOOST_INTERPROCESS_USE_POSIX_SEMAPHORES
33 #elif !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION) && defined (BOOST_INTERPROCESS_WINDOWS)
34 #include <boost/interprocess/sync/windows/named_mutex.hpp>
35 #define BOOST_INTERPROCESS_USE_WINDOWS
37 #include <boost/interprocess/sync/shm/named_mutex.hpp>
41 //!Describes a named mutex class for inter-process synchronization
44 namespace interprocess {
46 class named_condition;
48 //!A mutex with a global name, so it can be found from different
49 //!processes. This mutex can't be placed in shared memory, and
50 //!each process should have it's own named_mutex.
53 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
57 named_mutex(const named_mutex &);
58 named_mutex &operator=(const named_mutex &);
59 friend class named_condition;
60 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
63 //!Creates a global mutex with a name.
64 //!Throws interprocess_exception on error.
65 named_mutex(create_only_t create_only, const char *name, const permissions &perm = permissions());
67 //!Opens or creates a global mutex with a name.
68 //!If the mutex is created, this call is equivalent to
69 //!named_mutex(create_only_t, ... )
70 //!If the mutex is already created, this call is equivalent
71 //!named_mutex(open_only_t, ... )
73 named_mutex(open_or_create_t open_or_create, const char *name, const permissions &perm = permissions());
75 //!Opens a global mutex with a name if that mutex is previously
76 //!created. If it is not previously created this function throws
77 //!interprocess_exception.
78 named_mutex(open_only_t open_only, const char *name);
80 //!Destroys *this and indicates that the calling process is finished using
81 //!the resource. The destructor function will deallocate
82 //!any system resources allocated by the system for use by this process for
83 //!this resource. The resource can still be opened again calling
84 //!the open constructor overload. To erase the resource from the system
88 //!Unlocks a previously locked
92 //!Locks the mutex, sleeps when the mutex is already locked.
93 //!Throws interprocess_exception if a severe error is found
96 //!Tries to lock the mutex, returns false when the mutex
97 //!is already locked, returns true when success.
98 //!Throws interprocess_exception if a severe error is found
101 //!Tries to lock the the mutex until time abs_time,
102 //!Returns false when timeout expires, returns true when locks.
103 //!Throws interprocess_exception if a severe error is found
104 bool timed_lock(const boost::posix_time::ptime &abs_time);
106 //!Erases a named mutex from the system.
107 //!Returns false on error. Never throws.
108 static bool remove(const char *name);
110 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
112 friend class ipcdetail::interprocess_tester;
113 void dont_close_on_destruction();
116 #if defined(BOOST_INTERPROCESS_USE_POSIX_SEMAPHORES)
117 typedef ipcdetail::posix_named_mutex internal_mutex_type;
118 #undef BOOST_INTERPROCESS_USE_POSIX_SEMAPHORES
119 #elif defined(BOOST_INTERPROCESS_USE_WINDOWS)
120 typedef ipcdetail::windows_named_mutex internal_mutex_type;
121 #undef BOOST_INTERPROCESS_USE_WINDOWS
123 typedef ipcdetail::shm_named_mutex internal_mutex_type;
125 internal_mutex_type &internal_mutex()
128 internal_mutex_type m_mut;
130 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
133 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
135 inline named_mutex::named_mutex(create_only_t, const char *name, const permissions &perm)
136 : m_mut(create_only_t(), name, perm)
139 inline named_mutex::named_mutex(open_or_create_t, const char *name, const permissions &perm)
140 : m_mut(open_or_create_t(), name, perm)
143 inline named_mutex::named_mutex(open_only_t, const char *name)
144 : m_mut(open_only_t(), name)
147 inline void named_mutex::dont_close_on_destruction()
148 { ipcdetail::interprocess_tester::dont_close_on_destruction(m_mut); }
150 inline named_mutex::~named_mutex()
153 inline void named_mutex::lock()
156 inline void named_mutex::unlock()
159 inline bool named_mutex::try_lock()
160 { return m_mut.try_lock(); }
162 inline bool named_mutex::timed_lock(const boost::posix_time::ptime &abs_time)
163 { return m_mut.timed_lock(abs_time); }
165 inline bool named_mutex::remove(const char *name)
166 { return internal_mutex_type::remove(name); }
168 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
170 } //namespace interprocess {
171 } //namespace boost {
173 #include <boost/interprocess/detail/config_end.hpp>
175 #endif //BOOST_INTERPROCESS_NAMED_MUTEX_HPP