• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //////////////////////////////////////////////////////////////////////////////
2 //
3 // (C) Copyright Ion Gaztanaga 2012-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_CONDITION_ANY_HPP
12 #define BOOST_INTERPROCESS_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 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
23 
24 #include <boost/interprocess/detail/config_begin.hpp>
25 #include <boost/interprocess/detail/workaround.hpp>
26 
27 #include <boost/interprocess/detail/posix_time_types_wrk.hpp>
28 #include <boost/interprocess/sync/interprocess_mutex.hpp>
29 #include <boost/interprocess/sync/interprocess_condition.hpp>
30 #include <boost/interprocess/exceptions.hpp>
31 #include <boost/interprocess/sync/detail/condition_any_algorithm.hpp>
32 
33 #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
34 
35 //!\file
36 //!Describes process-shared variables interprocess_condition_any class
37 
38 namespace boost {
39 
40 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
41 
42 namespace posix_time
43 {  class ptime;   }
44 
45 #endif   //#if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
46 
47 namespace interprocess {
48 
49 //!This class is a condition variable that can be placed in shared memory or
50 //!memory mapped files.
51 //!
52 //!The interprocess_condition_any class is a generalization of interprocess_condition.
53 //!Whereas interprocess_condition works only on Locks with mutex_type == interprocess_mutex
54 //!interprocess_condition_any can operate on any user-defined lock that meets the BasicLockable
55 //!requirements (lock()/unlock() member functions).
56 //!
57 //!Unlike std::condition_variable_any in C++11, it is NOT safe to invoke the destructor if all
58 //!threads have been only notified. It is required that they have exited their respective wait
59 //!functions.
60 class interprocess_condition_any
61 {
62    #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
63    //Non-copyable
64    interprocess_condition_any(const interprocess_condition_any &);
65    interprocess_condition_any &operator=(const interprocess_condition_any &);
66 
67    class members
68    {
69       public:
70       typedef interprocess_condition   condvar_type;
71       typedef interprocess_mutex       mutex_type;
72 
get_condvar()73       condvar_type &get_condvar() {  return m_cond;  }
get_mutex()74       mutex_type   &get_mutex()   {  return m_mut; }
75 
76       private:
77       condvar_type   m_cond;
78       mutex_type     m_mut;
79    };
80 
81    ipcdetail::condition_any_wrapper<members>   m_cond;
82 
83    #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
84    public:
85    //!Constructs a interprocess_condition_any. On error throws interprocess_exception.
interprocess_condition_any()86    interprocess_condition_any(){}
87 
88    //!Destroys *this
89    //!liberating system resources.
~interprocess_condition_any()90    ~interprocess_condition_any(){}
91 
92    //!If there is a thread waiting on *this, change that
93    //!thread's state to ready. Otherwise there is no effect.
notify_one()94    void notify_one()
95    {  m_cond.notify_one();  }
96 
97    //!Change the state of all threads waiting on *this to ready.
98    //!If there are no waiting threads, notify_all() has no effect.
notify_all()99    void notify_all()
100    {  m_cond.notify_all();  }
101 
102    //!Releases the lock on the interprocess_mutex object associated with lock, blocks
103    //!the current thread of execution until readied by a call to
104    //!this->notify_one() or this->notify_all(), and then reacquires the lock.
105    template <typename L>
wait(L & lock)106    void wait(L& lock)
107    {  m_cond.wait(lock);  }
108 
109    //!The same as:
110    //!while (!pred()) wait(lock)
111    template <typename L, typename Pr>
wait(L & lock,Pr pred)112    void wait(L& lock, Pr pred)
113    {  m_cond.wait(lock, pred);  }
114 
115    //!Releases the lock on the interprocess_mutex object associated with lock, blocks
116    //!the current thread of execution until readied by a call to
117    //!this->notify_one() or this->notify_all(), or until time abs_time is reached,
118    //!and then reacquires the lock.
119    //!Returns: false if time abs_time is reached, otherwise true.
120    template <typename L>
timed_wait(L & lock,const boost::posix_time::ptime & abs_time)121    bool timed_wait(L& lock, const boost::posix_time::ptime &abs_time)
122    {  return m_cond.timed_wait(lock, abs_time);  }
123 
124    //!The same as:   while (!pred()) {
125    //!                  if (!timed_wait(lock, abs_time)) return pred();
126    //!               } return true;
127    template <typename L, typename Pr>
timed_wait(L & lock,const boost::posix_time::ptime & abs_time,Pr pred)128    bool timed_wait(L& lock, const boost::posix_time::ptime &abs_time, Pr pred)
129    {  return m_cond.timed_wait(lock, abs_time, pred);  }
130 };
131 
132 }  //namespace interprocess
133 }  // namespace boost
134 
135 #include <boost/interprocess/detail/config_end.hpp>
136 
137 #endif // BOOST_INTERPROCESS_CONDITION_ANY_HPP
138