2 // detail/posix_event.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2016 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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)
11 #ifndef BOOST_ASIO_DETAIL_POSIX_EVENT_HPP
12 #define BOOST_ASIO_DETAIL_POSIX_EVENT_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/config.hpp>
20 #if defined(BOOST_ASIO_HAS_PTHREADS)
23 #include <boost/asio/detail/assert.hpp>
24 #include <boost/asio/detail/noncopyable.hpp>
26 #include <boost/asio/detail/push_options.hpp>
37 BOOST_ASIO_DECL posix_event();
42 ::pthread_cond_destroy(&cond_);
45 // Signal the event. (Retained for backward compatibility.)
46 template <typename Lock>
47 void signal(Lock& lock)
49 this->signal_all(lock);
52 // Signal all waiters.
53 template <typename Lock>
54 void signal_all(Lock& lock)
56 BOOST_ASIO_ASSERT(lock.locked());
59 ::pthread_cond_broadcast(&cond_); // Ignore EINVAL.
62 // Unlock the mutex and signal one waiter.
63 template <typename Lock>
64 void unlock_and_signal_one(Lock& lock)
66 BOOST_ASIO_ASSERT(lock.locked());
68 bool have_waiters = (state_ > 1);
71 ::pthread_cond_signal(&cond_); // Ignore EINVAL.
74 // If there's a waiter, unlock the mutex and signal it.
75 template <typename Lock>
76 bool maybe_unlock_and_signal_one(Lock& lock)
78 BOOST_ASIO_ASSERT(lock.locked());
83 ::pthread_cond_signal(&cond_); // Ignore EINVAL.
90 template <typename Lock>
91 void clear(Lock& lock)
93 BOOST_ASIO_ASSERT(lock.locked());
95 state_ &= ~std::size_t(1);
98 // Wait for the event to become signalled.
99 template <typename Lock>
100 void wait(Lock& lock)
102 BOOST_ASIO_ASSERT(lock.locked());
103 while ((state_ & 1) == 0)
106 ::pthread_cond_wait(&cond_, &lock.mutex().mutex_); // Ignore EINVAL.
112 ::pthread_cond_t cond_;
116 } // namespace detail
120 #include <boost/asio/detail/pop_options.hpp>
122 #if defined(BOOST_ASIO_HEADER_ONLY)
123 # include <boost/asio/detail/impl/posix_event.ipp>
124 #endif // defined(BOOST_ASIO_HEADER_ONLY)
126 #endif // defined(BOOST_ASIO_HAS_PTHREADS)
128 #endif // BOOST_ASIO_DETAIL_POSIX_EVENT_HPP