Realistic 3D camera system
3D camera system components
win_event.hpp
Go to the documentation of this file.
1 //
2 // detail/win_event.hpp
3 // ~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2015 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_WIN_EVENT_HPP
12 #define ASIO_DETAIL_WIN_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 
20 #if defined(ASIO_WINDOWS)
21 
22 #include "asio/detail/assert.hpp"
25 
27 
28 namespace asio {
29 namespace detail {
30 
31 class win_event
32  : private noncopyable
33 {
34 public:
35  // Constructor.
36  ASIO_DECL win_event();
37 
38  // Destructor.
39  ASIO_DECL ~win_event();
40 
41  // Signal the event. (Retained for backward compatibility.)
42  template <typename Lock>
43  void signal(Lock& lock)
44  {
45  this->signal_all(lock);
46  }
47 
48  // Signal all waiters.
49  template <typename Lock>
50  void signal_all(Lock& lock)
51  {
52  ASIO_ASSERT(lock.locked());
53  (void)lock;
54  state_ |= 1;
55  ::SetEvent(events_[0]);
56  }
57 
58  // Unlock the mutex and signal one waiter.
59  template <typename Lock>
60  void unlock_and_signal_one(Lock& lock)
61  {
62  ASIO_ASSERT(lock.locked());
63  state_ |= 1;
64  bool have_waiters = (state_ > 1);
65  lock.unlock();
66  if (have_waiters)
67  ::SetEvent(events_[1]);
68  }
69 
70  // If there's a waiter, unlock the mutex and signal it.
71  template <typename Lock>
72  bool maybe_unlock_and_signal_one(Lock& lock)
73  {
74  ASIO_ASSERT(lock.locked());
75  state_ |= 1;
76  if (state_ > 1)
77  {
78  lock.unlock();
79  ::SetEvent(events_[1]);
80  return true;
81  }
82  return false;
83  }
84 
85  // Reset the event.
86  template <typename Lock>
87  void clear(Lock& lock)
88  {
89  ASIO_ASSERT(lock.locked());
90  (void)lock;
91  ::ResetEvent(events_[0]);
92  state_ &= ~std::size_t(1);
93  }
94 
95  // Wait for the event to become signalled.
96  template <typename Lock>
97  void wait(Lock& lock)
98  {
99  ASIO_ASSERT(lock.locked());
100  while ((state_ & 1) == 0)
101  {
102  state_ += 2;
103  lock.unlock();
104  ::WaitForMultipleObjects(2, events_, false, INFINITE);
105  lock.lock();
106  state_ -= 2;
107  }
108  }
109 
110 private:
111  HANDLE events_[2];
112  std::size_t state_;
113 };
114 
115 } // namespace detail
116 } // namespace asio
117 
119 
120 #if defined(ASIO_HEADER_ONLY)
122 #endif // defined(ASIO_HEADER_ONLY)
123 
124 #endif // defined(ASIO_WINDOWS)
125 
126 #endif // ASIO_DETAIL_WIN_EVENT_HPP
#define ASIO_ASSERT(expr)
Definition: assert.hpp:29
STL namespace.
#define ASIO_DECL
Definition: config.hpp:43