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_SHARABLE_MUTEX_HPP
12 #define BOOST_INTERPROCESS_NAMED_SHARABLE_MUTEX_HPP
18 #include <boost/interprocess/detail/config_begin.hpp>
19 #include <boost/interprocess/detail/workaround.hpp>
20 #include <boost/interprocess/creation_tags.hpp>
21 #include <boost/interprocess/exceptions.hpp>
22 #include <boost/interprocess/shared_memory_object.hpp>
23 #include <boost/interprocess/detail/managed_open_or_create_impl.hpp>
24 #include <boost/interprocess/sync/interprocess_sharable_mutex.hpp>
25 #include <boost/interprocess/detail/posix_time_types_wrk.hpp>
26 #include <boost/interprocess/sync/shm/named_creation_functor.hpp>
27 #include <boost/interprocess/permissions.hpp>
30 //!Describes a named sharable mutex class for inter-process synchronization
33 namespace interprocess {
35 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
36 namespace ipcdetail{ class interprocess_tester; }
37 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
39 class named_condition;
41 //!A sharable mutex with a global name, so it can be found from different
42 //!processes. This mutex can't be placed in shared memory, and
43 //!each process should have it's own named sharable mutex.
44 class named_sharable_mutex
46 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
48 named_sharable_mutex();
49 named_sharable_mutex(const named_sharable_mutex &);
50 named_sharable_mutex &operator=(const named_sharable_mutex &);
51 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
54 //!Creates a global sharable mutex with a name.
55 //!If the sharable mutex can't be created throws interprocess_exception
56 named_sharable_mutex(create_only_t create_only, const char *name, const permissions &perm = permissions());
58 //!Opens or creates a global sharable mutex with a name.
59 //!If the sharable mutex is created, this call is equivalent to
60 //!named_sharable_mutex(create_only_t, ...)
61 //!If the sharable mutex is already created, this call is equivalent to
62 //!named_sharable_mutex(open_only_t, ... ).
63 named_sharable_mutex(open_or_create_t open_or_create, const char *name, const permissions &perm = permissions());
65 //!Opens a global sharable mutex with a name if that sharable mutex
67 //!created. If it is not previously created this function throws
68 //!interprocess_exception.
69 named_sharable_mutex(open_only_t open_only, const char *name);
71 //!Destroys *this and indicates that the calling process is finished using
72 //!the resource. The destructor function will deallocate
73 //!any system resources allocated by the system for use by this process for
74 //!this resource. The resource can still be opened again calling
75 //!the open constructor overload. To erase the resource from the system
77 ~named_sharable_mutex();
81 //!Effects: The calling thread tries to obtain exclusive ownership of the mutex,
82 //! and if another thread has exclusive or sharable ownership of
83 //! the mutex, it waits until it can obtain the ownership.
84 //!Throws: interprocess_exception on error.
87 //!Effects: The calling thread tries to acquire exclusive ownership of the mutex
88 //! without waiting. If no other thread has exclusive or sharable
89 //! ownership of the mutex this succeeds.
90 //!Returns: If it can acquire exclusive ownership immediately returns true.
91 //! If it has to wait, returns false.
92 //!Throws: interprocess_exception on error.
95 //!Effects: The calling thread tries to acquire exclusive ownership of the mutex
96 //! waiting if necessary until no other thread has exclusive, or sharable
97 //! ownership of the mutex or abs_time is reached.
98 //!Returns: If acquires exclusive ownership, returns true. Otherwise returns false.
99 //!Throws: interprocess_exception on error.
100 bool timed_lock(const boost::posix_time::ptime &abs_time);
102 //!Precondition: The thread must have exclusive ownership of the mutex.
103 //!Effects: The calling thread releases the exclusive ownership of the mutex.
104 //!Throws: An exception derived from interprocess_exception on error.
109 //!Effects: The calling thread tries to obtain sharable ownership of the mutex,
110 //! and if another thread has exclusive ownership of the mutex,
111 //! waits until it can obtain the ownership.
112 //!Throws: interprocess_exception on error.
113 void lock_sharable();
115 //!Effects: The calling thread tries to acquire sharable ownership of the mutex
116 //! without waiting. If no other thread has exclusive ownership
117 //! of the mutex this succeeds.
118 //!Returns: If it can acquire sharable ownership immediately returns true. If it
119 //! has to wait, returns false.
120 //!Throws: interprocess_exception on error.
121 bool try_lock_sharable();
123 //!Effects: The calling thread tries to acquire sharable ownership of the mutex
124 //! waiting if necessary until no other thread has exclusive
125 //! ownership of the mutex or abs_time is reached.
126 //!Returns: If acquires sharable ownership, returns true. Otherwise returns false.
127 //!Throws: interprocess_exception on error.
128 bool timed_lock_sharable(const boost::posix_time::ptime &abs_time);
130 //!Precondition: The thread must have sharable ownership of the mutex.
131 //!Effects: The calling thread releases the sharable ownership of the mutex.
132 //!Throws: An exception derived from interprocess_exception on error.
133 void unlock_sharable();
135 //!Erases a named sharable mutex from the system.
136 //!Returns false on error. Never throws.
137 static bool remove(const char *name);
139 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
141 friend class ipcdetail::interprocess_tester;
142 void dont_close_on_destruction();
144 interprocess_sharable_mutex *mutex() const
145 { return static_cast<interprocess_sharable_mutex*>(m_shmem.get_user_address()); }
147 typedef ipcdetail::managed_open_or_create_impl<shared_memory_object, 0, true, false> open_create_impl_t;
148 open_create_impl_t m_shmem;
149 typedef ipcdetail::named_creation_functor<interprocess_sharable_mutex> construct_func_t;
150 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
153 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
155 inline named_sharable_mutex::~named_sharable_mutex()
158 inline named_sharable_mutex::named_sharable_mutex
159 (create_only_t, const char *name, const permissions &perm)
160 : m_shmem (create_only
162 ,sizeof(interprocess_sharable_mutex) +
163 open_create_impl_t::ManagedOpenOrCreateUserOffset
166 ,construct_func_t(ipcdetail::DoCreate)
170 inline named_sharable_mutex::named_sharable_mutex
171 (open_or_create_t, const char *name, const permissions &perm)
172 : m_shmem (open_or_create
174 ,sizeof(interprocess_sharable_mutex) +
175 open_create_impl_t::ManagedOpenOrCreateUserOffset
178 ,construct_func_t(ipcdetail::DoOpenOrCreate)
182 inline named_sharable_mutex::named_sharable_mutex
183 (open_only_t, const char *name)
188 ,construct_func_t(ipcdetail::DoOpen))
191 inline void named_sharable_mutex::dont_close_on_destruction()
192 { ipcdetail::interprocess_tester::dont_close_on_destruction(m_shmem); }
194 inline void named_sharable_mutex::lock()
195 { this->mutex()->lock(); }
197 inline void named_sharable_mutex::unlock()
198 { this->mutex()->unlock(); }
200 inline bool named_sharable_mutex::try_lock()
201 { return this->mutex()->try_lock(); }
203 inline bool named_sharable_mutex::timed_lock
204 (const boost::posix_time::ptime &abs_time)
205 { return this->mutex()->timed_lock(abs_time); }
207 inline void named_sharable_mutex::lock_sharable()
208 { this->mutex()->lock_sharable(); }
210 inline void named_sharable_mutex::unlock_sharable()
211 { this->mutex()->unlock_sharable(); }
213 inline bool named_sharable_mutex::try_lock_sharable()
214 { return this->mutex()->try_lock_sharable(); }
216 inline bool named_sharable_mutex::timed_lock_sharable
217 (const boost::posix_time::ptime &abs_time)
218 { return this->mutex()->timed_lock_sharable(abs_time); }
220 inline bool named_sharable_mutex::remove(const char *name)
221 { return shared_memory_object::remove(name); }
223 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
225 } //namespace interprocess {
226 } //namespace boost {
228 #include <boost/interprocess/detail/config_end.hpp>
230 #endif //BOOST_INTERPROCESS_NAMED_SHARABLE_MUTEX_HPP