Chris@16
|
1 //////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
2 //
|
Chris@16
|
3 // (C) Copyright Ion Gaztanaga 2005-2012. Distributed under the Boost
|
Chris@16
|
4 // Software License, Version 1.0. (See accompanying file
|
Chris@16
|
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
6 //
|
Chris@16
|
7 // See http://www.boost.org/libs/interprocess for documentation.
|
Chris@16
|
8 //
|
Chris@16
|
9 //////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
10
|
Chris@16
|
11 #ifndef BOOST_INTERPROCESS_NAMED_SHARABLE_MUTEX_HPP
|
Chris@16
|
12 #define BOOST_INTERPROCESS_NAMED_SHARABLE_MUTEX_HPP
|
Chris@16
|
13
|
Chris@101
|
14 #ifndef BOOST_CONFIG_HPP
|
Chris@101
|
15 # include <boost/config.hpp>
|
Chris@101
|
16 #endif
|
Chris@101
|
17 #
|
Chris@101
|
18 #if defined(BOOST_HAS_PRAGMA_ONCE)
|
Chris@16
|
19 # pragma once
|
Chris@16
|
20 #endif
|
Chris@16
|
21
|
Chris@16
|
22 #include <boost/interprocess/detail/config_begin.hpp>
|
Chris@16
|
23 #include <boost/interprocess/detail/workaround.hpp>
|
Chris@16
|
24 #include <boost/interprocess/creation_tags.hpp>
|
Chris@16
|
25 #include <boost/interprocess/exceptions.hpp>
|
Chris@16
|
26 #include <boost/interprocess/shared_memory_object.hpp>
|
Chris@16
|
27 #include <boost/interprocess/detail/managed_open_or_create_impl.hpp>
|
Chris@16
|
28 #include <boost/interprocess/sync/interprocess_sharable_mutex.hpp>
|
Chris@16
|
29 #include <boost/interprocess/detail/posix_time_types_wrk.hpp>
|
Chris@16
|
30 #include <boost/interprocess/sync/shm/named_creation_functor.hpp>
|
Chris@16
|
31 #include <boost/interprocess/permissions.hpp>
|
Chris@16
|
32
|
Chris@16
|
33 //!\file
|
Chris@16
|
34 //!Describes a named sharable mutex class for inter-process synchronization
|
Chris@16
|
35
|
Chris@16
|
36 namespace boost {
|
Chris@16
|
37 namespace interprocess {
|
Chris@16
|
38
|
Chris@101
|
39 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
|
Chris@16
|
40 namespace ipcdetail{ class interprocess_tester; }
|
Chris@101
|
41 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
|
Chris@16
|
42
|
Chris@16
|
43 class named_condition;
|
Chris@16
|
44
|
Chris@16
|
45 //!A sharable mutex with a global name, so it can be found from different
|
Chris@16
|
46 //!processes. This mutex can't be placed in shared memory, and
|
Chris@16
|
47 //!each process should have it's own named sharable mutex.
|
Chris@16
|
48 class named_sharable_mutex
|
Chris@16
|
49 {
|
Chris@101
|
50 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
|
Chris@16
|
51 //Non-copyable
|
Chris@16
|
52 named_sharable_mutex();
|
Chris@16
|
53 named_sharable_mutex(const named_sharable_mutex &);
|
Chris@16
|
54 named_sharable_mutex &operator=(const named_sharable_mutex &);
|
Chris@101
|
55 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
|
Chris@16
|
56 public:
|
Chris@16
|
57
|
Chris@16
|
58 //!Creates a global sharable mutex with a name.
|
Chris@16
|
59 //!If the sharable mutex can't be created throws interprocess_exception
|
Chris@16
|
60 named_sharable_mutex(create_only_t create_only, const char *name, const permissions &perm = permissions());
|
Chris@16
|
61
|
Chris@16
|
62 //!Opens or creates a global sharable mutex with a name.
|
Chris@16
|
63 //!If the sharable mutex is created, this call is equivalent to
|
Chris@16
|
64 //!named_sharable_mutex(create_only_t, ...)
|
Chris@16
|
65 //!If the sharable mutex is already created, this call is equivalent to
|
Chris@16
|
66 //!named_sharable_mutex(open_only_t, ... ).
|
Chris@16
|
67 named_sharable_mutex(open_or_create_t open_or_create, const char *name, const permissions &perm = permissions());
|
Chris@16
|
68
|
Chris@16
|
69 //!Opens a global sharable mutex with a name if that sharable mutex
|
Chris@16
|
70 //!is previously.
|
Chris@16
|
71 //!created. If it is not previously created this function throws
|
Chris@16
|
72 //!interprocess_exception.
|
Chris@16
|
73 named_sharable_mutex(open_only_t open_only, const char *name);
|
Chris@16
|
74
|
Chris@16
|
75 //!Destroys *this and indicates that the calling process is finished using
|
Chris@16
|
76 //!the resource. The destructor function will deallocate
|
Chris@16
|
77 //!any system resources allocated by the system for use by this process for
|
Chris@16
|
78 //!this resource. The resource can still be opened again calling
|
Chris@16
|
79 //!the open constructor overload. To erase the resource from the system
|
Chris@16
|
80 //!use remove().
|
Chris@16
|
81 ~named_sharable_mutex();
|
Chris@16
|
82
|
Chris@16
|
83 //Exclusive locking
|
Chris@16
|
84
|
Chris@16
|
85 //!Effects: The calling thread tries to obtain exclusive ownership of the mutex,
|
Chris@16
|
86 //! and if another thread has exclusive or sharable ownership of
|
Chris@16
|
87 //! the mutex, it waits until it can obtain the ownership.
|
Chris@16
|
88 //!Throws: interprocess_exception on error.
|
Chris@16
|
89 void lock();
|
Chris@16
|
90
|
Chris@16
|
91 //!Effects: The calling thread tries to acquire exclusive ownership of the mutex
|
Chris@16
|
92 //! without waiting. If no other thread has exclusive or sharable
|
Chris@16
|
93 //! ownership of the mutex this succeeds.
|
Chris@16
|
94 //!Returns: If it can acquire exclusive ownership immediately returns true.
|
Chris@16
|
95 //! If it has to wait, returns false.
|
Chris@16
|
96 //!Throws: interprocess_exception on error.
|
Chris@16
|
97 bool try_lock();
|
Chris@16
|
98
|
Chris@16
|
99 //!Effects: The calling thread tries to acquire exclusive ownership of the mutex
|
Chris@16
|
100 //! waiting if necessary until no other thread has exclusive, or sharable
|
Chris@16
|
101 //! ownership of the mutex or abs_time is reached.
|
Chris@16
|
102 //!Returns: If acquires exclusive ownership, returns true. Otherwise returns false.
|
Chris@16
|
103 //!Throws: interprocess_exception on error.
|
Chris@16
|
104 bool timed_lock(const boost::posix_time::ptime &abs_time);
|
Chris@16
|
105
|
Chris@16
|
106 //!Precondition: The thread must have exclusive ownership of the mutex.
|
Chris@16
|
107 //!Effects: The calling thread releases the exclusive ownership of the mutex.
|
Chris@16
|
108 //!Throws: An exception derived from interprocess_exception on error.
|
Chris@16
|
109 void unlock();
|
Chris@16
|
110
|
Chris@16
|
111 //Sharable locking
|
Chris@16
|
112
|
Chris@16
|
113 //!Effects: The calling thread tries to obtain sharable ownership of the mutex,
|
Chris@16
|
114 //! and if another thread has exclusive ownership of the mutex,
|
Chris@16
|
115 //! waits until it can obtain the ownership.
|
Chris@16
|
116 //!Throws: interprocess_exception on error.
|
Chris@16
|
117 void lock_sharable();
|
Chris@16
|
118
|
Chris@16
|
119 //!Effects: The calling thread tries to acquire sharable ownership of the mutex
|
Chris@16
|
120 //! without waiting. If no other thread has exclusive ownership
|
Chris@16
|
121 //! of the mutex this succeeds.
|
Chris@16
|
122 //!Returns: If it can acquire sharable ownership immediately returns true. If it
|
Chris@16
|
123 //! has to wait, returns false.
|
Chris@16
|
124 //!Throws: interprocess_exception on error.
|
Chris@16
|
125 bool try_lock_sharable();
|
Chris@16
|
126
|
Chris@16
|
127 //!Effects: The calling thread tries to acquire sharable ownership of the mutex
|
Chris@16
|
128 //! waiting if necessary until no other thread has exclusive
|
Chris@16
|
129 //! ownership of the mutex or abs_time is reached.
|
Chris@16
|
130 //!Returns: If acquires sharable ownership, returns true. Otherwise returns false.
|
Chris@16
|
131 //!Throws: interprocess_exception on error.
|
Chris@16
|
132 bool timed_lock_sharable(const boost::posix_time::ptime &abs_time);
|
Chris@16
|
133
|
Chris@16
|
134 //!Precondition: The thread must have sharable ownership of the mutex.
|
Chris@16
|
135 //!Effects: The calling thread releases the sharable ownership of the mutex.
|
Chris@16
|
136 //!Throws: An exception derived from interprocess_exception on error.
|
Chris@16
|
137 void unlock_sharable();
|
Chris@16
|
138
|
Chris@16
|
139 //!Erases a named sharable mutex from the system.
|
Chris@16
|
140 //!Returns false on error. Never throws.
|
Chris@16
|
141 static bool remove(const char *name);
|
Chris@16
|
142
|
Chris@101
|
143 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
|
Chris@16
|
144 private:
|
Chris@16
|
145 friend class ipcdetail::interprocess_tester;
|
Chris@16
|
146 void dont_close_on_destruction();
|
Chris@16
|
147
|
Chris@16
|
148 interprocess_sharable_mutex *mutex() const
|
Chris@16
|
149 { return static_cast<interprocess_sharable_mutex*>(m_shmem.get_user_address()); }
|
Chris@16
|
150
|
Chris@16
|
151 typedef ipcdetail::managed_open_or_create_impl<shared_memory_object, 0, true, false> open_create_impl_t;
|
Chris@16
|
152 open_create_impl_t m_shmem;
|
Chris@16
|
153 typedef ipcdetail::named_creation_functor<interprocess_sharable_mutex> construct_func_t;
|
Chris@101
|
154 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
|
Chris@16
|
155 };
|
Chris@16
|
156
|
Chris@101
|
157 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
|
Chris@16
|
158
|
Chris@16
|
159 inline named_sharable_mutex::~named_sharable_mutex()
|
Chris@16
|
160 {}
|
Chris@16
|
161
|
Chris@16
|
162 inline named_sharable_mutex::named_sharable_mutex
|
Chris@16
|
163 (create_only_t, const char *name, const permissions &perm)
|
Chris@16
|
164 : m_shmem (create_only
|
Chris@16
|
165 ,name
|
Chris@16
|
166 ,sizeof(interprocess_sharable_mutex) +
|
Chris@16
|
167 open_create_impl_t::ManagedOpenOrCreateUserOffset
|
Chris@16
|
168 ,read_write
|
Chris@16
|
169 ,0
|
Chris@16
|
170 ,construct_func_t(ipcdetail::DoCreate)
|
Chris@16
|
171 ,perm)
|
Chris@16
|
172 {}
|
Chris@16
|
173
|
Chris@16
|
174 inline named_sharable_mutex::named_sharable_mutex
|
Chris@16
|
175 (open_or_create_t, const char *name, const permissions &perm)
|
Chris@16
|
176 : m_shmem (open_or_create
|
Chris@16
|
177 ,name
|
Chris@16
|
178 ,sizeof(interprocess_sharable_mutex) +
|
Chris@16
|
179 open_create_impl_t::ManagedOpenOrCreateUserOffset
|
Chris@16
|
180 ,read_write
|
Chris@16
|
181 ,0
|
Chris@16
|
182 ,construct_func_t(ipcdetail::DoOpenOrCreate)
|
Chris@16
|
183 ,perm)
|
Chris@16
|
184 {}
|
Chris@16
|
185
|
Chris@16
|
186 inline named_sharable_mutex::named_sharable_mutex
|
Chris@16
|
187 (open_only_t, const char *name)
|
Chris@16
|
188 : m_shmem (open_only
|
Chris@16
|
189 ,name
|
Chris@16
|
190 ,read_write
|
Chris@16
|
191 ,0
|
Chris@16
|
192 ,construct_func_t(ipcdetail::DoOpen))
|
Chris@16
|
193 {}
|
Chris@16
|
194
|
Chris@16
|
195 inline void named_sharable_mutex::dont_close_on_destruction()
|
Chris@16
|
196 { ipcdetail::interprocess_tester::dont_close_on_destruction(m_shmem); }
|
Chris@16
|
197
|
Chris@16
|
198 inline void named_sharable_mutex::lock()
|
Chris@16
|
199 { this->mutex()->lock(); }
|
Chris@16
|
200
|
Chris@16
|
201 inline void named_sharable_mutex::unlock()
|
Chris@16
|
202 { this->mutex()->unlock(); }
|
Chris@16
|
203
|
Chris@16
|
204 inline bool named_sharable_mutex::try_lock()
|
Chris@16
|
205 { return this->mutex()->try_lock(); }
|
Chris@16
|
206
|
Chris@16
|
207 inline bool named_sharable_mutex::timed_lock
|
Chris@16
|
208 (const boost::posix_time::ptime &abs_time)
|
Chris@16
|
209 { return this->mutex()->timed_lock(abs_time); }
|
Chris@16
|
210
|
Chris@16
|
211 inline void named_sharable_mutex::lock_sharable()
|
Chris@16
|
212 { this->mutex()->lock_sharable(); }
|
Chris@16
|
213
|
Chris@16
|
214 inline void named_sharable_mutex::unlock_sharable()
|
Chris@16
|
215 { this->mutex()->unlock_sharable(); }
|
Chris@16
|
216
|
Chris@16
|
217 inline bool named_sharable_mutex::try_lock_sharable()
|
Chris@16
|
218 { return this->mutex()->try_lock_sharable(); }
|
Chris@16
|
219
|
Chris@16
|
220 inline bool named_sharable_mutex::timed_lock_sharable
|
Chris@16
|
221 (const boost::posix_time::ptime &abs_time)
|
Chris@16
|
222 { return this->mutex()->timed_lock_sharable(abs_time); }
|
Chris@16
|
223
|
Chris@16
|
224 inline bool named_sharable_mutex::remove(const char *name)
|
Chris@16
|
225 { return shared_memory_object::remove(name); }
|
Chris@16
|
226
|
Chris@101
|
227 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
|
Chris@16
|
228
|
Chris@16
|
229 } //namespace interprocess {
|
Chris@16
|
230 } //namespace boost {
|
Chris@16
|
231
|
Chris@16
|
232 #include <boost/interprocess/detail/config_end.hpp>
|
Chris@16
|
233
|
Chris@16
|
234 #endif //BOOST_INTERPROCESS_NAMED_SHARABLE_MUTEX_HPP
|