2 // detail/posix_event.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2018 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.
111 // Timed wait for the event to become signalled.
112 template <typename Lock>
113 bool wait_for_usec(Lock& lock, long usec)
115 BOOST_ASIO_ASSERT(lock.locked());
116 if ((state_ & 1) == 0)
120 #if (defined(__MACH__) && defined(__APPLE__)) \
121 || (defined(__ANDROID__) && (__ANDROID_API__ < 21) \
122 && defined(HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE))
123 ts.tv_sec = usec / 1000000;
124 ts.tv_nsec = (usec % 1000000) * 1000;
125 ::pthread_cond_timedwait_relative_np(
126 &cond_, &lock.mutex().mutex_, &ts); // Ignore EINVAL.
127 #else // (defined(__MACH__) && defined(__APPLE__))
128 // || (defined(__ANDROID__) && (__ANDROID_API__ < 21)
129 // && defined(HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE))
130 if (::clock_gettime(CLOCK_MONOTONIC, &ts) == 0)
132 ts.tv_sec += usec / 1000000;
133 ts.tv_nsec = (usec % 1000000) * 1000;
134 ts.tv_sec += ts.tv_nsec / 1000000000;
135 ts.tv_nsec = ts.tv_nsec % 1000000000;
136 ::pthread_cond_timedwait(&cond_,
137 &lock.mutex().mutex_, &ts); // Ignore EINVAL.
139 #endif // (defined(__MACH__) && defined(__APPLE__))
140 // || (defined(__ANDROID__) && (__ANDROID_API__ < 21)
141 // && defined(HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE))
144 return (state_ & 1) != 0;
148 ::pthread_cond_t cond_;
152 } // namespace detail
156 #include <boost/asio/detail/pop_options.hpp>
158 #if defined(BOOST_ASIO_HEADER_ONLY)
159 # include <boost/asio/detail/impl/posix_event.ipp>
160 #endif // defined(BOOST_ASIO_HEADER_ONLY)
162 #endif // defined(BOOST_ASIO_HAS_PTHREADS)
164 #endif // BOOST_ASIO_DETAIL_POSIX_EVENT_HPP