Clementine
conditionally_enabled_event.hpp
1 //
2 // detail/conditionally_enabled_event.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2020 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 ASIO_DETAIL_CONDITIONALLY_ENABLED_EVENT_HPP
12 #define ASIO_DETAIL_CONDITIONALLY_ENABLED_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 "asio/detail/config.hpp"
19 #include "asio/detail/conditionally_enabled_mutex.hpp"
20 #include "asio/detail/event.hpp"
21 #include "asio/detail/noncopyable.hpp"
22 #include "asio/detail/null_event.hpp"
23 #include "asio/detail/scoped_lock.hpp"
24 
25 #include "asio/detail/push_options.hpp"
26 
27 namespace asio {
28 namespace detail {
29 
30 // Mutex adapter used to conditionally enable or disable locking.
32  : private noncopyable
33 {
34 public:
35  // Constructor.
37  {
38  }
39 
40  // Destructor.
42  {
43  }
44 
45  // Signal the event. (Retained for backward compatibility.)
47  {
48  if (lock.mutex_.enabled_)
49  event_.signal(lock);
50  }
51 
52  // Signal all waiters.
53  void signal_all(conditionally_enabled_mutex::scoped_lock& lock)
54  {
55  if (lock.mutex_.enabled_)
56  event_.signal_all(lock);
57  }
58 
59  // Unlock the mutex and signal one waiter.
60  void unlock_and_signal_one(
62  {
63  if (lock.mutex_.enabled_)
64  event_.unlock_and_signal_one(lock);
65  }
66 
67  // Unlock the mutex and signal one waiter who may destroy us.
68  void unlock_and_signal_one_for_destruction(
70  {
71  if (lock.mutex_.enabled_)
72  event_.unlock_and_signal_one(lock);
73  }
74 
75  // If there's a waiter, unlock the mutex and signal it.
76  bool maybe_unlock_and_signal_one(
78  {
79  if (lock.mutex_.enabled_)
80  return event_.maybe_unlock_and_signal_one(lock);
81  else
82  return false;
83  }
84 
85  // Reset the event.
87  {
88  if (lock.mutex_.enabled_)
89  event_.clear(lock);
90  }
91 
92  // Wait for the event to become signalled.
94  {
95  if (lock.mutex_.enabled_)
96  event_.wait(lock);
97  else
98  null_event().wait(lock);
99  }
100 
101  // Timed wait for the event to become signalled.
102  bool wait_for_usec(
104  {
105  if (lock.mutex_.enabled_)
106  return event_.wait_for_usec(lock, usec);
107  else
108  return null_event().wait_for_usec(lock, usec);
109  }
110 
111 private:
112  asio::detail::event event_;
113 };
114 
115 } // namespace detail
116 } // namespace asio
117 
118 #include "asio/detail/pop_options.hpp"
119 
120 #endif // ASIO_DETAIL_CONDITIONALLY_ENABLED_EVENT_HPP
Definition: conditionally_enabled_event.hpp:31
Definition: noncopyable.hpp:25
Definition: chrono.h:284
Definition: null_event.hpp:26
Definition: conditionally_enabled_mutex.hpp:34
Definition: any_io_executor.hpp:28