blob: 67c87462057576fcea6df90957c0056be86d284f [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "rtc_base/event.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000012
13#if defined(WEBRTC_WIN)
14#include <windows.h>
15#elif defined(WEBRTC_POSIX)
Karl Wiberg98417032019-03-24 19:12:40 +010016#include <errno.h>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000017#include <pthread.h>
18#include <sys/time.h>
19#include <time.h>
20#else
21#error "Must define either WEBRTC_WIN or WEBRTC_POSIX."
22#endif
23
Karl Wibergfc47c862019-04-11 10:31:24 +020024#include "absl/types/optional.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "rtc_base/checks.h"
Sebastian Jansson7a603392019-03-20 16:50:35 +010026#include "rtc_base/synchronization/yield_policy.h"
Karl Wiberg98417032019-03-24 19:12:40 +010027#include "rtc_base/system/warn_current_thread_is_deadlocked.h"
tommi@webrtc.org4c0fd962015-02-09 10:23:27 +000028
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000029namespace rtc {
30
Niels Möllerc572ff32018-11-07 08:43:50 +010031Event::Event() : Event(false, false) {}
32
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000033#if defined(WEBRTC_WIN)
34
tommi@webrtc.org4c0fd962015-02-09 10:23:27 +000035Event::Event(bool manual_reset, bool initially_signaled) {
deadbeef37f5ecf2017-02-27 14:06:41 -080036 event_handle_ = ::CreateEvent(nullptr, // Security attributes.
37 manual_reset, initially_signaled,
38 nullptr); // Name.
henrikg91d6ede2015-09-17 00:24:34 -070039 RTC_CHECK(event_handle_);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000040}
41
42Event::~Event() {
43 CloseHandle(event_handle_);
44}
45
46void Event::Set() {
47 SetEvent(event_handle_);
48}
49
50void Event::Reset() {
51 ResetEvent(event_handle_);
52}
53
Karl Wibergfc47c862019-04-11 10:31:24 +020054bool Event::Wait(const int give_up_after_ms, int /*warn_after_ms*/) {
Sebastian Jansson7a603392019-03-20 16:50:35 +010055 ScopedYieldPolicy::YieldExecution();
Karl Wibergfc47c862019-04-11 10:31:24 +020056 const DWORD ms = give_up_after_ms == kForever ? INFINITE : give_up_after_ms;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000057 return (WaitForSingleObject(event_handle_, ms) == WAIT_OBJECT_0);
58}
59
60#elif defined(WEBRTC_POSIX)
61
Niels Möller2cf9c552018-05-22 14:11:59 +020062// On MacOS, clock_gettime is available from version 10.12, and on
63// iOS, from version 10.0. So we can't use it yet.
64#if defined(WEBRTC_MAC) || defined(WEBRTC_IOS)
65#define USE_CLOCK_GETTIME 0
66#define USE_PTHREAD_COND_TIMEDWAIT_MONOTONIC_NP 0
67// On Android, pthread_condattr_setclock is available from version 21. By
68// default, we target a new enough version for 64-bit platforms but not for
69// 32-bit platforms. For older versions, use
70// pthread_cond_timedwait_monotonic_np.
71#elif defined(WEBRTC_ANDROID) && (__ANDROID_API__ < 21)
72#define USE_CLOCK_GETTIME 1
73#define USE_PTHREAD_COND_TIMEDWAIT_MONOTONIC_NP 1
74#else
75#define USE_CLOCK_GETTIME 1
76#define USE_PTHREAD_COND_TIMEDWAIT_MONOTONIC_NP 0
77#endif
78
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000079Event::Event(bool manual_reset, bool initially_signaled)
Yves Gerey665174f2018-06-19 15:03:05 +020080 : is_manual_reset_(manual_reset), event_status_(initially_signaled) {
deadbeef37f5ecf2017-02-27 14:06:41 -080081 RTC_CHECK(pthread_mutex_init(&event_mutex_, nullptr) == 0);
Niels Möller2cf9c552018-05-22 14:11:59 +020082 pthread_condattr_t cond_attr;
83 RTC_CHECK(pthread_condattr_init(&cond_attr) == 0);
84#if USE_CLOCK_GETTIME && !USE_PTHREAD_COND_TIMEDWAIT_MONOTONIC_NP
85 RTC_CHECK(pthread_condattr_setclock(&cond_attr, CLOCK_MONOTONIC) == 0);
86#endif
87 RTC_CHECK(pthread_cond_init(&event_cond_, &cond_attr) == 0);
88 pthread_condattr_destroy(&cond_attr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000089}
90
91Event::~Event() {
92 pthread_mutex_destroy(&event_mutex_);
93 pthread_cond_destroy(&event_cond_);
94}
95
96void Event::Set() {
97 pthread_mutex_lock(&event_mutex_);
98 event_status_ = true;
99 pthread_cond_broadcast(&event_cond_);
100 pthread_mutex_unlock(&event_mutex_);
101}
102
103void Event::Reset() {
104 pthread_mutex_lock(&event_mutex_);
105 event_status_ = false;
106 pthread_mutex_unlock(&event_mutex_);
107}
108
Karl Wiberg98417032019-03-24 19:12:40 +0100109namespace {
Sebastian Jansson7a603392019-03-20 16:50:35 +0100110
Karl Wiberg98417032019-03-24 19:12:40 +0100111timespec GetTimespec(const int milliseconds_from_now) {
112 timespec ts;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000113
Karl Wiberg98417032019-03-24 19:12:40 +0100114 // Get the current time.
Niels Möller2cf9c552018-05-22 14:11:59 +0200115#if USE_CLOCK_GETTIME
Karl Wiberg98417032019-03-24 19:12:40 +0100116 clock_gettime(CLOCK_MONOTONIC, &ts);
Niels Möller2cf9c552018-05-22 14:11:59 +0200117#else
Karl Wiberg98417032019-03-24 19:12:40 +0100118 timeval tv;
119 gettimeofday(&tv, nullptr);
120 ts.tv_sec = tv.tv_sec;
121 ts.tv_nsec = tv.tv_usec * 1000;
Niels Möller2cf9c552018-05-22 14:11:59 +0200122#endif
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000123
Karl Wiberg98417032019-03-24 19:12:40 +0100124 // Add the specified number of milliseconds to it.
125 ts.tv_sec += (milliseconds_from_now / 1000);
126 ts.tv_nsec += (milliseconds_from_now % 1000) * 1000000;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000127
Karl Wiberg98417032019-03-24 19:12:40 +0100128 // Normalize.
129 if (ts.tv_nsec >= 1000000000) {
130 ts.tv_sec++;
131 ts.tv_nsec -= 1000000000;
Peter Boström7ddc9de2016-02-22 11:31:49 +0100132 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000133
Karl Wiberg98417032019-03-24 19:12:40 +0100134 return ts;
135}
136
137} // namespace
138
Karl Wibergfc47c862019-04-11 10:31:24 +0200139bool Event::Wait(const int give_up_after_ms, const int warn_after_ms) {
140 // Instant when we'll log a warning message (because we've been waiting so
141 // long it might be a bug), but not yet give up waiting. nullopt if we
142 // shouldn't log a warning.
143 const absl::optional<timespec> warn_ts =
144 warn_after_ms == kForever ||
145 (give_up_after_ms != kForever && warn_after_ms > give_up_after_ms)
146 ? absl::nullopt
147 : absl::make_optional(GetTimespec(warn_after_ms));
148
149 // Instant when we'll stop waiting and return an error. nullopt if we should
150 // never give up.
151 const absl::optional<timespec> give_up_ts =
152 give_up_after_ms == kForever
153 ? absl::nullopt
154 : absl::make_optional(GetTimespec(give_up_after_ms));
Karl Wiberg98417032019-03-24 19:12:40 +0100155
156 ScopedYieldPolicy::YieldExecution();
Peter Boström7ddc9de2016-02-22 11:31:49 +0100157 pthread_mutex_lock(&event_mutex_);
Karl Wiberg98417032019-03-24 19:12:40 +0100158
Karl Wibergfc47c862019-04-11 10:31:24 +0200159 // Wait for `event_cond_` to trigger and `event_status_` to be set, with the
160 // given timeout (or without a timeout if none is given).
161 const auto wait = [&](const absl::optional<timespec> timeout_ts) {
162 int error = 0;
Karl Wiberg98417032019-03-24 19:12:40 +0100163 while (!event_status_ && error == 0) {
Karl Wibergfc47c862019-04-11 10:31:24 +0200164 if (timeout_ts == absl::nullopt) {
165 error = pthread_cond_wait(&event_cond_, &event_mutex_);
166 } else {
167#if USE_PTHREAD_COND_TIMEDWAIT_MONOTONIC_NP
168 error = pthread_cond_timedwait_monotonic_np(&event_cond_, &event_mutex_,
169 &*timeout_ts);
170#else
171 error =
172 pthread_cond_timedwait(&event_cond_, &event_mutex_, &*timeout_ts);
173#endif
174 }
175 }
176 return error;
177 };
178
179 int error;
180 if (warn_ts == absl::nullopt) {
181 error = wait(give_up_ts);
182 } else {
183 error = wait(warn_ts);
184 if (error == ETIMEDOUT) {
185 webrtc::WarnThatTheCurrentThreadIsProbablyDeadlocked();
186 error = wait(give_up_ts);
Karl Wiberg98417032019-03-24 19:12:40 +0100187 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000188 }
189
190 // NOTE(liulk): Exactly one thread will auto-reset this event. All
191 // the other threads will think it's unsignaled. This seems to be
192 // consistent with auto-reset events in WEBRTC_WIN
193 if (error == 0 && !is_manual_reset_)
194 event_status_ = false;
195
196 pthread_mutex_unlock(&event_mutex_);
197
198 return (error == 0);
199}
200
201#endif
202
203} // namespace rtc