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