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