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