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