Imported Upstream version 1.64.0
[platform/upstream/boost.git] / boost / asio / detail / std_event.hpp
1 //
2 // detail/std_event.hpp
3 // ~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2017 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6 //
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 //
10
11 #ifndef BOOST_ASIO_DETAIL_STD_EVENT_HPP
12 #define BOOST_ASIO_DETAIL_STD_EVENT_HPP
13
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
15 # pragma once
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
17
18 #include <boost/asio/detail/config.hpp>
19
20 #if defined(BOOST_ASIO_HAS_STD_MUTEX_AND_CONDVAR)
21
22 #include <chrono>
23 #include <condition_variable>
24 #include <boost/asio/detail/assert.hpp>
25 #include <boost/asio/detail/noncopyable.hpp>
26
27 #include <boost/asio/detail/push_options.hpp>
28
29 namespace boost {
30 namespace asio {
31 namespace detail {
32
33 class std_event
34   : private noncopyable
35 {
36 public:
37   // Constructor.
38   std_event()
39     : state_(0)
40   {
41   }
42
43   // Destructor.
44   ~std_event()
45   {
46   }
47
48   // Signal the event. (Retained for backward compatibility.)
49   template <typename Lock>
50   void signal(Lock& lock)
51   {
52     this->signal_all(lock);
53   }
54
55   // Signal all waiters.
56   template <typename Lock>
57   void signal_all(Lock& lock)
58   {
59     BOOST_ASIO_ASSERT(lock.locked());
60     (void)lock;
61     state_ |= 1;
62     cond_.notify_all();
63   }
64
65   // Unlock the mutex and signal one waiter.
66   template <typename Lock>
67   void unlock_and_signal_one(Lock& lock)
68   {
69     BOOST_ASIO_ASSERT(lock.locked());
70     state_ |= 1;
71     bool have_waiters = (state_ > 1);
72     lock.unlock();
73     if (have_waiters)
74       cond_.notify_one();
75   }
76
77   // If there's a waiter, unlock the mutex and signal it.
78   template <typename Lock>
79   bool maybe_unlock_and_signal_one(Lock& lock)
80   {
81     BOOST_ASIO_ASSERT(lock.locked());
82     state_ |= 1;
83     if (state_ > 1)
84     {
85       lock.unlock();
86       cond_.notify_one();
87       return true;
88     }
89     return false;
90   }
91
92   // Reset the event.
93   template <typename Lock>
94   void clear(Lock& lock)
95   {
96     BOOST_ASIO_ASSERT(lock.locked());
97     (void)lock;
98     state_ &= ~std::size_t(1);
99   }
100
101   // Wait for the event to become signalled.
102   template <typename Lock>
103   void wait(Lock& lock)
104   {
105     BOOST_ASIO_ASSERT(lock.locked());
106     unique_lock_adapter u_lock(lock);
107     while ((state_ & 1) == 0)
108     {
109       waiter w(state_);
110       cond_.wait(u_lock.unique_lock_);
111     }
112   }
113
114   // Timed wait for the event to become signalled.
115   template <typename Lock>
116   bool wait_for_usec(Lock& lock, long usec)
117   {
118     BOOST_ASIO_ASSERT(lock.locked());
119     unique_lock_adapter u_lock(lock);
120     if ((state_ & 1) == 0)
121     {
122       waiter w(state_);
123       cond_.wait_for(u_lock.unique_lock_, std::chrono::microseconds(usec));
124     }
125     return (state_ & 1) != 0;
126   }
127
128 private:
129   // Helper class to temporarily adapt a scoped_lock into a unique_lock so that
130   // it can be passed to std::condition_variable::wait().
131   struct unique_lock_adapter
132   {
133     template <typename Lock>
134     explicit unique_lock_adapter(Lock& lock)
135       : unique_lock_(lock.mutex().mutex_, std::adopt_lock)
136     {
137     }
138
139     ~unique_lock_adapter()
140     {
141       unique_lock_.release();
142     }
143
144     std::unique_lock<std::mutex> unique_lock_;
145   };
146
147   // Helper to increment and decrement the state to track outstanding waiters.
148   class waiter
149   {
150   public:
151     explicit waiter(std::size_t& state)
152       : state_(state)
153     {
154       state_ += 2;
155     }
156
157     ~waiter()
158     {
159       state_ -= 2;
160     }
161
162   private:
163     std::size_t& state_;
164   };
165
166   std::condition_variable cond_;
167   std::size_t state_;
168 };
169
170 } // namespace detail
171 } // namespace asio
172 } // namespace boost
173
174 #include <boost/asio/detail/pop_options.hpp>
175
176 #endif // defined(BOOST_ASIO_HAS_STD_MUTEX_AND_CONDVAR)
177
178 #endif // BOOST_ASIO_DETAIL_STD_EVENT_HPP