1 //////////////////////////////////////////////////////////////////////////////
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)
7 // See http://www.boost.org/libs/interprocess for documentation.
9 //////////////////////////////////////////////////////////////////////////////
11 #ifndef BOOST_INTERPROCESS_SHM_NAMED_CONDITION_ANY_HPP
12 #define BOOST_INTERPROCESS_SHM_NAMED_CONDITION_ANY_HPP
14 #ifndef BOOST_CONFIG_HPP
15 # include <boost/config.hpp>
18 #if defined(BOOST_HAS_PRAGMA_ONCE)
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>
40 //!Describes process-shared variables interprocess_condition class
43 namespace interprocess {
46 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
47 class interprocess_tester;
48 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
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
55 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
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
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
67 ,sizeof(internal_condition) +
68 open_create_impl_t::ManagedOpenOrCreateUserOffset
71 ,construct_func_t(DoCreate)
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, ... )
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
84 ,sizeof(internal_condition) +
85 open_create_impl_t::ManagedOpenOrCreateUserOffset
88 ,construct_func_t(DoOpenOrCreate)
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)
100 ,construct_func_t(DoOpen))
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
109 ~shm_named_condition_any()
112 //!If there is a thread waiting on *this, change that
113 //!thread's state to ready. Otherwise there is no effect.*/
115 { m_cond.notify_one(); }
117 //!Change the state of all threads waiting on *this to ready.
118 //!If there are no waiting threads, notify_all() has no effect.
120 { m_cond.notify_all(); }
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>
127 { m_cond.wait(lock); }
130 //!while (!pred()) wait(lock)
131 template <typename L, typename Pr>
132 void wait(L& lock, Pr pred)
133 { m_cond.wait(lock, pred); }
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); }
144 //!The same as: while (!pred()) {
145 //! if (!timed_wait(lock, abs_time)) return pred();
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); }
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); }
156 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
159 class internal_condition_members
162 typedef interprocess_mutex mutex_type;
163 typedef interprocess_condition condvar_type;
165 condvar_type& get_condvar() { return m_cond; }
166 mutex_type& get_mutex() { return m_mtx; }
173 typedef ipcdetail::condition_any_wrapper<internal_condition_members> internal_condition;
175 internal_condition m_cond;
177 friend class boost::interprocess::ipcdetail::interprocess_tester;
178 void dont_close_on_destruction()
179 { interprocess_tester::dont_close_on_destruction(m_shmem); }
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;
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
189 } //namespace ipcdetail
190 } //namespace interprocess
193 #include <boost/interprocess/detail/config_end.hpp>
195 #endif // BOOST_INTERPROCESS_SHM_NAMED_CONDITION_ANY_HPP