Realistic 3D camera system
3D camera system components
posix_event.hpp
Go to the documentation of this file.
1 //
2 // detail/posix_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_POSIX_EVENT_HPP
12 #define ASIO_DETAIL_POSIX_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_HAS_PTHREADS)
21 
22 #include <pthread.h>
23 #include "asio/detail/assert.hpp"
25 
27 
28 namespace asio {
29 namespace detail {
30 
31 class posix_event
32  : private noncopyable
33 {
34 public:
35  // Constructor.
36  ASIO_DECL posix_event();
37 
38  // Destructor.
39  ~posix_event()
40  {
41  ::pthread_cond_destroy(&cond_);
42  }
43 
44  // Signal the event. (Retained for backward compatibility.)
45  template <typename Lock>
46  void signal(Lock& lock)
47  {
48  this->signal_all(lock);
49  }
50 
51  // Signal all waiters.
52  template <typename Lock>
53  void signal_all(Lock& lock)
54  {
55  ASIO_ASSERT(lock.locked());
56  (void)lock;
57  state_ |= 1;
58  ::pthread_cond_broadcast(&cond_); // Ignore EINVAL.
59  }
60 
61  // Unlock the mutex and signal one waiter.
62  template <typename Lock>
63  void unlock_and_signal_one(Lock& lock)
64  {
65  ASIO_ASSERT(lock.locked());
66  state_ |= 1;
67  bool have_waiters = (state_ > 1);
68  lock.unlock();
69  if (have_waiters)
70  ::pthread_cond_signal(&cond_); // Ignore EINVAL.
71  }
72 
73  // If there's a waiter, unlock the mutex and signal it.
74  template <typename Lock>
75  bool maybe_unlock_and_signal_one(Lock& lock)
76  {
77  ASIO_ASSERT(lock.locked());
78  state_ |= 1;
79  if (state_ > 1)
80  {
81  lock.unlock();
82  ::pthread_cond_signal(&cond_); // Ignore EINVAL.
83  return true;
84  }
85  return false;
86  }
87 
88  // Reset the event.
89  template <typename Lock>
90  void clear(Lock& lock)
91  {
92  ASIO_ASSERT(lock.locked());
93  (void)lock;
94  state_ &= ~std::size_t(1);
95  }
96 
97  // Wait for the event to become signalled.
98  template <typename Lock>
99  void wait(Lock& lock)
100  {
101  ASIO_ASSERT(lock.locked());
102  while ((state_ & 1) == 0)
103  {
104  state_ += 2;
105  ::pthread_cond_wait(&cond_, &lock.mutex().mutex_); // Ignore EINVAL.
106  state_ -= 2;
107  }
108  }
109 
110 private:
111  ::pthread_cond_t cond_;
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_HAS_PTHREADS)
125 
126 #endif // ASIO_DETAIL_POSIX_EVENT_HPP
#define ASIO_ASSERT(expr)
Definition: assert.hpp:29
STL namespace.
#define ASIO_DECL
Definition: config.hpp:43