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