Imported Upstream version 1.57.0
[platform/upstream/boost.git] / boost / interprocess / sync / windows / 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_DETAIL_WINDOWS_CONDITION_HPP
12 #define BOOST_INTERPROCESS_DETAIL_WINDOWS_CONDITION_HPP
13
14 #if defined(_MSC_VER)
15 #  pragma once
16 #endif
17
18 #include <boost/interprocess/detail/config_begin.hpp>
19 #include <boost/interprocess/detail/workaround.hpp>
20 #include <boost/interprocess/detail/posix_time_types_wrk.hpp>
21
22 #include <boost/interprocess/sync/interprocess_mutex.hpp>
23 #include <boost/interprocess/sync/scoped_lock.hpp>
24 #include <boost/interprocess/exceptions.hpp>
25 #include <boost/interprocess/sync/windows/semaphore.hpp>
26 #include <boost/interprocess/sync/windows/mutex.hpp>
27 #include <boost/interprocess/sync/detail/condition_algorithm_8a.hpp>
28
29
30 namespace boost {
31 namespace interprocess {
32 namespace ipcdetail {
33
34 class windows_condition
35 {
36    windows_condition(const windows_condition &);
37    windows_condition &operator=(const windows_condition &);
38
39    public:
40    windows_condition()
41       : m_condition_data()
42    {}
43
44    ~windows_condition()
45    {
46       //Notify all waiting threads
47       //to allow POSIX semantics on condition destruction
48       this->notify_all();
49    }
50
51    void notify_one()
52    {  m_condition_data.notify_one();   }
53
54    void notify_all()
55    {  m_condition_data.notify_all();   }
56
57    template <typename L>
58    bool timed_wait(L& lock, const boost::posix_time::ptime &abs_time)
59    {  return m_condition_data.timed_wait(lock, abs_time);   }
60
61    template <typename L, typename Pr>
62    bool timed_wait(L& lock, const boost::posix_time::ptime &abs_time, Pr pred)
63    {  return m_condition_data.timed_wait(lock, abs_time, pred);   }
64
65    template <typename L>
66    void wait(L& lock)
67    {  m_condition_data.wait(lock);   }
68
69    template <typename L, typename Pr>
70    void wait(L& lock, Pr pred)
71    {  m_condition_data.wait(lock, pred);   }
72
73    private:
74
75    struct condition_data
76    {
77       typedef boost::int32_t     integer_type;
78       typedef windows_semaphore  semaphore_type;
79       typedef windows_mutex      mutex_type;
80
81       condition_data()
82          : m_nwaiters_blocked(0)
83          , m_nwaiters_gone(0)
84          , m_nwaiters_to_unblock(0)
85          , m_sem_block_queue(0)
86          , m_sem_block_lock(1)
87          , m_mtx_unblock_lock()
88       {}
89
90       integer_type    &get_nwaiters_blocked()
91       {  return m_nwaiters_blocked;  }
92
93       integer_type    &get_nwaiters_gone()
94       {  return m_nwaiters_gone;  }
95
96       integer_type    &get_nwaiters_to_unblock()
97       {  return m_nwaiters_to_unblock;  }
98
99       semaphore_type  &get_sem_block_queue()
100       {  return m_sem_block_queue;  }
101
102       semaphore_type  &get_sem_block_lock()
103       {  return m_sem_block_lock;  }
104
105       mutex_type      &get_mtx_unblock_lock()
106       {  return m_mtx_unblock_lock;  }
107
108       boost::int32_t    m_nwaiters_blocked;
109       boost::int32_t    m_nwaiters_gone;
110       boost::int32_t    m_nwaiters_to_unblock;
111       windows_semaphore m_sem_block_queue;
112       windows_semaphore m_sem_block_lock;
113       windows_mutex     m_mtx_unblock_lock;
114    };
115
116    ipcdetail::condition_8a_wrapper<condition_data> m_condition_data;
117 };
118
119 }  //namespace ipcdetail
120 }  //namespace interprocess
121 }  //namespace boost
122
123 #include <boost/interprocess/detail/config_end.hpp>
124
125 #endif   //BOOST_INTERPROCESS_DETAIL_WINDOWS_CONDITION_HPP