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_SHM_NAMED_CONDITION_HPP
|
Chris@16
|
12 #define BOOST_INTERPROCESS_SHM_NAMED_CONDITION_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/static_assert.hpp>
|
Chris@16
|
25 #include <boost/interprocess/detail/type_traits.hpp>
|
Chris@16
|
26 #include <boost/interprocess/creation_tags.hpp>
|
Chris@16
|
27 #include <boost/interprocess/exceptions.hpp>
|
Chris@16
|
28 #include <boost/interprocess/shared_memory_object.hpp>
|
Chris@16
|
29 #include <boost/interprocess/sync/interprocess_condition.hpp>
|
Chris@16
|
30 #include <boost/interprocess/detail/managed_open_or_create_impl.hpp>
|
Chris@16
|
31 #include <boost/interprocess/detail/posix_time_types_wrk.hpp>
|
Chris@16
|
32 #include <boost/interprocess/sync/shm/named_creation_functor.hpp>
|
Chris@16
|
33 #include <boost/interprocess/sync/named_mutex.hpp>
|
Chris@16
|
34 #include <boost/interprocess/permissions.hpp>
|
Chris@16
|
35 #if defined (BOOST_INTERPROCESS_NAMED_MUTEX_USES_POSIX_SEMAPHORES)
|
Chris@16
|
36 #include <boost/interprocess/sync/interprocess_mutex.hpp>
|
Chris@16
|
37 #include <boost/interprocess/sync/scoped_lock.hpp>
|
Chris@16
|
38 #include <boost/interprocess/sync/detail/condition_any_algorithm.hpp>
|
Chris@16
|
39 #else
|
Chris@16
|
40 #include <boost/interprocess/sync/detail/locks.hpp>
|
Chris@16
|
41 #endif
|
Chris@16
|
42
|
Chris@16
|
43
|
Chris@16
|
44 //!\file
|
Chris@16
|
45 //!Describes process-shared variables interprocess_condition class
|
Chris@16
|
46
|
Chris@16
|
47 namespace boost {
|
Chris@16
|
48 namespace interprocess {
|
Chris@16
|
49 namespace ipcdetail {
|
Chris@16
|
50
|
Chris@101
|
51 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
|
Chris@16
|
52 class interprocess_tester;
|
Chris@101
|
53 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
|
Chris@16
|
54
|
Chris@16
|
55 //! A global condition variable that can be created by name.
|
Chris@16
|
56 //! This condition variable is designed to work with named_mutex and
|
Chris@16
|
57 //! can't be placed in shared memory or memory mapped files.
|
Chris@16
|
58 class shm_named_condition
|
Chris@16
|
59 {
|
Chris@101
|
60 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
|
Chris@16
|
61 //Non-copyable
|
Chris@16
|
62 shm_named_condition();
|
Chris@16
|
63 shm_named_condition(const shm_named_condition &);
|
Chris@16
|
64 shm_named_condition &operator=(const shm_named_condition &);
|
Chris@101
|
65 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
|
Chris@16
|
66 public:
|
Chris@16
|
67 //!Creates a global condition with a name.
|
Chris@16
|
68 //!If the condition can't be created throws interprocess_exception
|
Chris@16
|
69 shm_named_condition(create_only_t create_only, const char *name, const permissions &perm = permissions());
|
Chris@16
|
70
|
Chris@16
|
71 //!Opens or creates a global condition with a name.
|
Chris@16
|
72 //!If the condition is created, this call is equivalent to
|
Chris@16
|
73 //!shm_named_condition(create_only_t, ... )
|
Chris@16
|
74 //!If the condition is already created, this call is equivalent
|
Chris@16
|
75 //!shm_named_condition(open_only_t, ... )
|
Chris@16
|
76 //!Does not throw
|
Chris@16
|
77 shm_named_condition(open_or_create_t open_or_create, const char *name, const permissions &perm = permissions());
|
Chris@16
|
78
|
Chris@16
|
79 //!Opens a global condition with a name if that condition is previously
|
Chris@16
|
80 //!created. If it is not previously created this function throws
|
Chris@16
|
81 //!interprocess_exception.
|
Chris@16
|
82 shm_named_condition(open_only_t open_only, const char *name);
|
Chris@16
|
83
|
Chris@16
|
84 //!Destroys *this and indicates that the calling process is finished using
|
Chris@16
|
85 //!the resource. The destructor function will deallocate
|
Chris@16
|
86 //!any system resources allocated by the system for use by this process for
|
Chris@16
|
87 //!this resource. The resource can still be opened again calling
|
Chris@16
|
88 //!the open constructor overload. To erase the resource from the system
|
Chris@16
|
89 //!use remove().
|
Chris@16
|
90 ~shm_named_condition();
|
Chris@16
|
91
|
Chris@16
|
92 //!If there is a thread waiting on *this, change that
|
Chris@16
|
93 //!thread's state to ready. Otherwise there is no effect.*/
|
Chris@16
|
94 void notify_one();
|
Chris@16
|
95
|
Chris@16
|
96 //!Change the state of all threads waiting on *this to ready.
|
Chris@16
|
97 //!If there are no waiting threads, notify_all() has no effect.
|
Chris@16
|
98 void notify_all();
|
Chris@16
|
99
|
Chris@16
|
100 //!Releases the lock on the named_mutex object associated with lock, blocks
|
Chris@16
|
101 //!the current thread of execution until readied by a call to
|
Chris@16
|
102 //!this->notify_one() or this->notify_all(), and then reacquires the lock.
|
Chris@16
|
103 template <typename L>
|
Chris@16
|
104 void wait(L& lock);
|
Chris@16
|
105
|
Chris@16
|
106 //!The same as:
|
Chris@16
|
107 //!while (!pred()) wait(lock)
|
Chris@16
|
108 template <typename L, typename Pr>
|
Chris@16
|
109 void wait(L& lock, Pr pred);
|
Chris@16
|
110
|
Chris@16
|
111 //!Releases the lock on the named_mutex object associated with lock, blocks
|
Chris@16
|
112 //!the current thread of execution until readied by a call to
|
Chris@16
|
113 //!this->notify_one() or this->notify_all(), or until time abs_time is reached,
|
Chris@16
|
114 //!and then reacquires the lock.
|
Chris@16
|
115 //!Returns: false if time abs_time is reached, otherwise true.
|
Chris@16
|
116 template <typename L>
|
Chris@16
|
117 bool timed_wait(L& lock, const boost::posix_time::ptime &abs_time);
|
Chris@16
|
118
|
Chris@16
|
119 //!The same as: while (!pred()) {
|
Chris@16
|
120 //! if (!timed_wait(lock, abs_time)) return pred();
|
Chris@16
|
121 //! } return true;
|
Chris@16
|
122 template <typename L, typename Pr>
|
Chris@16
|
123 bool timed_wait(L& lock, const boost::posix_time::ptime &abs_time, Pr pred);
|
Chris@16
|
124
|
Chris@16
|
125 //!Erases a named condition from the system.
|
Chris@16
|
126 //!Returns false on error. Never throws.
|
Chris@16
|
127 static bool remove(const char *name);
|
Chris@16
|
128
|
Chris@101
|
129 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
|
Chris@16
|
130 private:
|
Chris@16
|
131
|
Chris@16
|
132 #if defined (BOOST_INTERPROCESS_NAMED_MUTEX_USES_POSIX_SEMAPHORES)
|
Chris@16
|
133 class internal_condition_members
|
Chris@16
|
134 {
|
Chris@16
|
135 public:
|
Chris@16
|
136 typedef interprocess_mutex mutex_type;
|
Chris@16
|
137 typedef interprocess_condition condvar_type;
|
Chris@101
|
138
|
Chris@16
|
139 condvar_type& get_condvar() { return m_cond; }
|
Chris@16
|
140 mutex_type& get_mutex() { return m_mtx; }
|
Chris@16
|
141
|
Chris@16
|
142 private:
|
Chris@16
|
143 mutex_type m_mtx;
|
Chris@16
|
144 condvar_type m_cond;
|
Chris@16
|
145 };
|
Chris@16
|
146
|
Chris@16
|
147 typedef ipcdetail::condition_any_wrapper<internal_condition_members> internal_condition;
|
Chris@16
|
148 #else //defined (BOOST_INTERPROCESS_NAMED_MUTEX_USES_POSIX_SEMAPHORES)
|
Chris@16
|
149 typedef interprocess_condition internal_condition;
|
Chris@16
|
150 #endif //defined (BOOST_INTERPROCESS_NAMED_MUTEX_USES_POSIX_SEMAPHORES)
|
Chris@16
|
151
|
Chris@16
|
152 internal_condition &internal_cond()
|
Chris@16
|
153 { return *static_cast<internal_condition*>(m_shmem.get_user_address()); }
|
Chris@16
|
154
|
Chris@16
|
155 friend class boost::interprocess::ipcdetail::interprocess_tester;
|
Chris@16
|
156 void dont_close_on_destruction();
|
Chris@16
|
157
|
Chris@16
|
158 typedef ipcdetail::managed_open_or_create_impl<shared_memory_object, 0, true, false> open_create_impl_t;
|
Chris@16
|
159 open_create_impl_t m_shmem;
|
Chris@16
|
160
|
Chris@16
|
161 template <class T, class Arg> friend class boost::interprocess::ipcdetail::named_creation_functor;
|
Chris@16
|
162 typedef boost::interprocess::ipcdetail::named_creation_functor<internal_condition> construct_func_t;
|
Chris@101
|
163 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
|
Chris@16
|
164 };
|
Chris@16
|
165
|
Chris@101
|
166 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
|
Chris@16
|
167
|
Chris@16
|
168 inline shm_named_condition::~shm_named_condition()
|
Chris@16
|
169 {}
|
Chris@16
|
170
|
Chris@16
|
171 inline shm_named_condition::shm_named_condition(create_only_t, const char *name, const permissions &perm)
|
Chris@16
|
172 : m_shmem (create_only
|
Chris@16
|
173 ,name
|
Chris@16
|
174 ,sizeof(internal_condition) +
|
Chris@16
|
175 open_create_impl_t::ManagedOpenOrCreateUserOffset
|
Chris@16
|
176 ,read_write
|
Chris@16
|
177 ,0
|
Chris@16
|
178 ,construct_func_t(DoCreate)
|
Chris@16
|
179 ,perm)
|
Chris@16
|
180 {}
|
Chris@16
|
181
|
Chris@16
|
182 inline shm_named_condition::shm_named_condition(open_or_create_t, const char *name, const permissions &perm)
|
Chris@16
|
183 : m_shmem (open_or_create
|
Chris@16
|
184 ,name
|
Chris@16
|
185 ,sizeof(internal_condition) +
|
Chris@16
|
186 open_create_impl_t::ManagedOpenOrCreateUserOffset
|
Chris@16
|
187 ,read_write
|
Chris@16
|
188 ,0
|
Chris@16
|
189 ,construct_func_t(DoOpenOrCreate)
|
Chris@16
|
190 ,perm)
|
Chris@16
|
191 {}
|
Chris@16
|
192
|
Chris@16
|
193 inline shm_named_condition::shm_named_condition(open_only_t, const char *name)
|
Chris@16
|
194 : m_shmem (open_only
|
Chris@16
|
195 ,name
|
Chris@16
|
196 ,read_write
|
Chris@16
|
197 ,0
|
Chris@16
|
198 ,construct_func_t(DoOpen))
|
Chris@16
|
199 {}
|
Chris@16
|
200
|
Chris@16
|
201 inline void shm_named_condition::dont_close_on_destruction()
|
Chris@16
|
202 { interprocess_tester::dont_close_on_destruction(m_shmem); }
|
Chris@16
|
203
|
Chris@16
|
204 inline void shm_named_condition::notify_one()
|
Chris@16
|
205 { this->internal_cond().notify_one(); }
|
Chris@16
|
206
|
Chris@16
|
207 inline void shm_named_condition::notify_all()
|
Chris@16
|
208 { this->internal_cond().notify_all(); }
|
Chris@16
|
209
|
Chris@16
|
210 template <typename L>
|
Chris@16
|
211 inline void shm_named_condition::wait(L& lock)
|
Chris@16
|
212 { this->internal_cond().wait(lock); }
|
Chris@16
|
213
|
Chris@16
|
214 template <typename L, typename Pr>
|
Chris@16
|
215 inline void shm_named_condition::wait(L& lock, Pr pred)
|
Chris@16
|
216 { this->internal_cond().wait(lock, pred); }
|
Chris@16
|
217
|
Chris@16
|
218 template <typename L>
|
Chris@16
|
219 inline bool shm_named_condition::timed_wait
|
Chris@16
|
220 (L& lock, const boost::posix_time::ptime &abs_time)
|
Chris@16
|
221 { return this->internal_cond().timed_wait(lock, abs_time); }
|
Chris@16
|
222
|
Chris@16
|
223 template <typename L, typename Pr>
|
Chris@16
|
224 inline bool shm_named_condition::timed_wait
|
Chris@16
|
225 (L& lock, const boost::posix_time::ptime &abs_time, Pr pred)
|
Chris@16
|
226 { return this->internal_cond().timed_wait(lock, abs_time, pred); }
|
Chris@16
|
227
|
Chris@16
|
228 inline bool shm_named_condition::remove(const char *name)
|
Chris@16
|
229 { return shared_memory_object::remove(name); }
|
Chris@16
|
230
|
Chris@101
|
231 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
|
Chris@16
|
232
|
Chris@16
|
233 } //namespace ipcdetail
|
Chris@16
|
234 } //namespace interprocess
|
Chris@16
|
235 } //namespace boost
|
Chris@16
|
236
|
Chris@16
|
237 #include <boost/interprocess/detail/config_end.hpp>
|
Chris@16
|
238
|
Chris@16
|
239 #endif // BOOST_INTERPROCESS_SHM_NAMED_CONDITION_HPP
|