Imported Upstream version 1.57.0
[platform/upstream/boost.git] / boost / interprocess / sync / interprocess_condition.hpp
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_CONDITION_HPP
12 #define BOOST_INTERPROCESS_CONDITION_HPP
13
14 #if defined(_MSC_VER)
15 #  pragma once
16 #endif
17
18 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
19
20 #include <boost/interprocess/detail/config_begin.hpp>
21 #include <boost/interprocess/detail/workaround.hpp>
22
23 #include <boost/interprocess/detail/posix_time_types_wrk.hpp>
24 #include <boost/interprocess/sync/interprocess_mutex.hpp>
25 #include <boost/interprocess/sync/detail/locks.hpp>
26 #include <boost/interprocess/exceptions.hpp>
27 #include <boost/limits.hpp>
28 #include <boost/assert.hpp>
29
30 #if !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION) && defined(BOOST_INTERPROCESS_POSIX_PROCESS_SHARED)
31    #include <boost/interprocess/sync/posix/condition.hpp>
32    #define BOOST_INTERPROCESS_USE_POSIX
33 //Experimental...
34 #elif !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION) && defined (BOOST_INTERPROCESS_WINDOWS)
35    #include <boost/interprocess/sync/windows/condition.hpp>
36    #define BOOST_INTERPROCESS_USE_WINDOWS
37 #elif !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
38    #include <boost/interprocess/sync/spin/condition.hpp>
39    #define BOOST_INTERPROCESS_USE_GENERIC_EMULATION
40 #endif
41
42 #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
43
44 //!\file
45 //!Describes process-shared variables interprocess_condition class
46
47 namespace boost {
48
49 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
50
51 namespace posix_time
52 {  class ptime;   }
53
54 #endif   //#if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
55
56 namespace interprocess {
57
58 class named_condition;
59
60 //!This class is a condition variable that can be placed in shared memory or
61 //!memory mapped files.
62 //!Destroys the object of type std::condition_variable_any
63 //!
64 //!Unlike std::condition_variable in C++11, it is NOT safe to invoke the destructor if all
65 //!threads have been only notified. It is required that they have exited their respective wait
66 //!functions.
67 class interprocess_condition
68 {
69    #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
70    //Non-copyable
71    interprocess_condition(const interprocess_condition &);
72    interprocess_condition &operator=(const interprocess_condition &);
73    friend class named_condition;
74    #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
75
76    public:
77    //!Constructs a interprocess_condition. On error throws interprocess_exception.
78    interprocess_condition()
79    {}
80
81    //!Destroys *this
82    //!liberating system resources.
83    ~interprocess_condition()
84    {}
85
86    //!If there is a thread waiting on *this, change that
87    //!thread's state to ready. Otherwise there is no effect.
88    void notify_one()
89    {  m_condition.notify_one();  }
90
91    //!Change the state of all threads waiting on *this to ready.
92    //!If there are no waiting threads, notify_all() has no effect.
93    void notify_all()
94    {  m_condition.notify_all();  }
95
96    //!Releases the lock on the interprocess_mutex object associated with lock, blocks
97    //!the current thread of execution until readied by a call to
98    //!this->notify_one() or this->notify_all(), and then reacquires the lock.
99    template <typename L>
100    void wait(L& lock)
101    {
102       ipcdetail::internal_mutex_lock<L> internal_lock(lock);
103       m_condition.wait(internal_lock);
104    }
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       ipcdetail::internal_mutex_lock<L> internal_lock(lock);
112       m_condition.wait(internal_lock, pred);
113    }
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>
121    bool timed_wait(L& lock, const boost::posix_time::ptime &abs_time)
122    {
123       ipcdetail::internal_mutex_lock<L> internal_lock(lock);
124       return m_condition.timed_wait(internal_lock, abs_time);
125    }
126
127    //!The same as:   while (!pred()) {
128    //!                  if (!timed_wait(lock, abs_time)) return pred();
129    //!               } return true;
130    template <typename L, typename Pr>
131    bool timed_wait(L& lock, const boost::posix_time::ptime &abs_time, Pr pred)
132    {
133       ipcdetail::internal_mutex_lock<L> internal_lock(lock);
134       return m_condition.timed_wait(internal_lock, abs_time, pred);
135    }
136
137    #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
138
139    private:
140    #if defined (BOOST_INTERPROCESS_USE_GENERIC_EMULATION)
141       #undef BOOST_INTERPROCESS_USE_GENERIC_EMULATION
142       ipcdetail::spin_condition m_condition;
143    #elif defined(BOOST_INTERPROCESS_USE_POSIX)
144       #undef BOOST_INTERPROCESS_USE_POSIX
145       ipcdetail::posix_condition m_condition;
146    #elif defined(BOOST_INTERPROCESS_USE_WINDOWS)
147       #undef BOOST_INTERPROCESS_USE_WINDOWS
148       ipcdetail::windows_condition m_condition;
149    #else
150       #error "Unknown platform for interprocess_mutex"
151    #endif
152    #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
153 };
154
155 }  //namespace interprocess
156 }  // namespace boost
157
158 #include <boost/interprocess/detail/config_end.hpp>
159
160 #endif // BOOST_INTERPROCESS_CONDITION_HPP