blob: c2f6f8abab9d33d27922df06f0bd2bb93966570f [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:261/*
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 04:47:3111#include "rtc_base/event.h"
henrike@webrtc.orgf0488722014-05-13 18:00:2612
13#if defined(WEBRTC_WIN)
14#include <windows.h>
15#elif defined(WEBRTC_POSIX)
Karl Wiberg98417032019-03-24 18:12:4016#include <errno.h>
henrike@webrtc.orgf0488722014-05-13 18:00:2617#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 08:31:2424#include "absl/types/optional.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3125#include "rtc_base/checks.h"
Sebastian Jansson7a603392019-03-20 15:50:3526#include "rtc_base/synchronization/yield_policy.h"
Karl Wiberg98417032019-03-24 18:12:4027#include "rtc_base/system/warn_current_thread_is_deadlocked.h"
Markus Handell1d5be492022-08-18 13:49:0928#include "rtc_base/time_utils.h"
tommi@webrtc.org4c0fd962015-02-09 10:23:2729
henrike@webrtc.orgf0488722014-05-13 18:00:2630namespace rtc {
31
Markus Handell1d5be492022-08-18 13:49:0932using ::webrtc::TimeDelta;
33
Niels Möllerc572ff32018-11-07 07:43:5034Event::Event() : Event(false, false) {}
35
henrike@webrtc.orgf0488722014-05-13 18:00:2636#if defined(WEBRTC_WIN)
37
tommi@webrtc.org4c0fd962015-02-09 10:23:2738Event::Event(bool manual_reset, bool initially_signaled) {
deadbeef37f5ecf2017-02-27 22:06:4139 event_handle_ = ::CreateEvent(nullptr, // Security attributes.
40 manual_reset, initially_signaled,
41 nullptr); // Name.
henrikg91d6ede2015-09-17 07:24:3442 RTC_CHECK(event_handle_);
henrike@webrtc.orgf0488722014-05-13 18:00:2643}
44
45Event::~Event() {
46 CloseHandle(event_handle_);
47}
48
49void Event::Set() {
50 SetEvent(event_handle_);
51}
52
53void Event::Reset() {
54 ResetEvent(event_handle_);
55}
56
Markus Handell1d5be492022-08-18 13:49:0957bool Event::Wait(TimeDelta give_up_after, TimeDelta /*warn_after*/) {
Sebastian Jansson7a603392019-03-20 15:50:3558 ScopedYieldPolicy::YieldExecution();
Markus Handell1d5be492022-08-18 13:49:0959 const DWORD ms =
60 give_up_after.IsPlusInfinity()
61 ? INFINITE
62 : give_up_after.RoundUpTo(webrtc::TimeDelta::Millis(1)).ms();
henrike@webrtc.orgf0488722014-05-13 18:00:2663 return (WaitForSingleObject(event_handle_, ms) == WAIT_OBJECT_0);
64}
65
66#elif defined(WEBRTC_POSIX)
67
Niels Möller2cf9c552018-05-22 12:11:5968// On MacOS, clock_gettime is available from version 10.12, and on
69// iOS, from version 10.0. So we can't use it yet.
70#if defined(WEBRTC_MAC) || defined(WEBRTC_IOS)
71#define USE_CLOCK_GETTIME 0
72#define USE_PTHREAD_COND_TIMEDWAIT_MONOTONIC_NP 0
73// On Android, pthread_condattr_setclock is available from version 21. By
74// default, we target a new enough version for 64-bit platforms but not for
75// 32-bit platforms. For older versions, use
76// pthread_cond_timedwait_monotonic_np.
77#elif defined(WEBRTC_ANDROID) && (__ANDROID_API__ < 21)
78#define USE_CLOCK_GETTIME 1
79#define USE_PTHREAD_COND_TIMEDWAIT_MONOTONIC_NP 1
80#else
81#define USE_CLOCK_GETTIME 1
82#define USE_PTHREAD_COND_TIMEDWAIT_MONOTONIC_NP 0
83#endif
84
henrike@webrtc.orgf0488722014-05-13 18:00:2685Event::Event(bool manual_reset, bool initially_signaled)
Yves Gerey665174f2018-06-19 13:03:0586 : is_manual_reset_(manual_reset), event_status_(initially_signaled) {
deadbeef37f5ecf2017-02-27 22:06:4187 RTC_CHECK(pthread_mutex_init(&event_mutex_, nullptr) == 0);
Niels Möller2cf9c552018-05-22 12:11:5988 pthread_condattr_t cond_attr;
89 RTC_CHECK(pthread_condattr_init(&cond_attr) == 0);
90#if USE_CLOCK_GETTIME && !USE_PTHREAD_COND_TIMEDWAIT_MONOTONIC_NP
91 RTC_CHECK(pthread_condattr_setclock(&cond_attr, CLOCK_MONOTONIC) == 0);
92#endif
93 RTC_CHECK(pthread_cond_init(&event_cond_, &cond_attr) == 0);
94 pthread_condattr_destroy(&cond_attr);
henrike@webrtc.orgf0488722014-05-13 18:00:2695}
96
97Event::~Event() {
98 pthread_mutex_destroy(&event_mutex_);
99 pthread_cond_destroy(&event_cond_);
100}
101
102void Event::Set() {
103 pthread_mutex_lock(&event_mutex_);
104 event_status_ = true;
105 pthread_cond_broadcast(&event_cond_);
106 pthread_mutex_unlock(&event_mutex_);
107}
108
109void Event::Reset() {
110 pthread_mutex_lock(&event_mutex_);
111 event_status_ = false;
112 pthread_mutex_unlock(&event_mutex_);
113}
114
Karl Wiberg98417032019-03-24 18:12:40115namespace {
Sebastian Jansson7a603392019-03-20 15:50:35116
Markus Handell1d5be492022-08-18 13:49:09117timespec GetTimespec(TimeDelta duration_from_now) {
Karl Wiberg98417032019-03-24 18:12:40118 timespec ts;
henrike@webrtc.orgf0488722014-05-13 18:00:26119
Karl Wiberg98417032019-03-24 18:12:40120 // Get the current time.
Niels Möller2cf9c552018-05-22 12:11:59121#if USE_CLOCK_GETTIME
Karl Wiberg98417032019-03-24 18:12:40122 clock_gettime(CLOCK_MONOTONIC, &ts);
Niels Möller2cf9c552018-05-22 12:11:59123#else
Karl Wiberg98417032019-03-24 18:12:40124 timeval tv;
125 gettimeofday(&tv, nullptr);
126 ts.tv_sec = tv.tv_sec;
Markus Handell1d5be492022-08-18 13:49:09127 ts.tv_nsec = tv.tv_usec * kNumNanosecsPerMicrosec;
Niels Möller2cf9c552018-05-22 12:11:59128#endif
henrike@webrtc.orgf0488722014-05-13 18:00:26129
Karl Wiberg98417032019-03-24 18:12:40130 // Add the specified number of milliseconds to it.
Markus Handell1d5be492022-08-18 13:49:09131 int64_t microsecs_from_now = duration_from_now.us();
132 ts.tv_sec += microsecs_from_now / kNumMicrosecsPerSec;
133 ts.tv_nsec +=
134 (microsecs_from_now % kNumMicrosecsPerSec) * kNumNanosecsPerMicrosec;
henrike@webrtc.orgf0488722014-05-13 18:00:26135
Karl Wiberg98417032019-03-24 18:12:40136 // Normalize.
Markus Handell1d5be492022-08-18 13:49:09137 if (ts.tv_nsec >= kNumNanosecsPerSec) {
Karl Wiberg98417032019-03-24 18:12:40138 ts.tv_sec++;
Markus Handell1d5be492022-08-18 13:49:09139 ts.tv_nsec -= kNumNanosecsPerSec;
Peter Boström7ddc9de2016-02-22 10:31:49140 }
henrike@webrtc.orgf0488722014-05-13 18:00:26141
Karl Wiberg98417032019-03-24 18:12:40142 return ts;
143}
144
145} // namespace
146
Markus Handell1d5be492022-08-18 13:49:09147bool Event::Wait(TimeDelta give_up_after, TimeDelta warn_after) {
Karl Wibergfc47c862019-04-11 08:31:24148 // Instant when we'll log a warning message (because we've been waiting so
149 // long it might be a bug), but not yet give up waiting. nullopt if we
150 // shouldn't log a warning.
151 const absl::optional<timespec> warn_ts =
Markus Handell1d5be492022-08-18 13:49:09152 warn_after >= give_up_after
Karl Wibergfc47c862019-04-11 08:31:24153 ? absl::nullopt
Markus Handell1d5be492022-08-18 13:49:09154 : absl::make_optional(GetTimespec(warn_after));
Karl Wibergfc47c862019-04-11 08:31:24155
156 // Instant when we'll stop waiting and return an error. nullopt if we should
157 // never give up.
158 const absl::optional<timespec> give_up_ts =
Markus Handell1d5be492022-08-18 13:49:09159 give_up_after.IsPlusInfinity()
Karl Wibergfc47c862019-04-11 08:31:24160 ? absl::nullopt
Markus Handell1d5be492022-08-18 13:49:09161 : absl::make_optional(GetTimespec(give_up_after));
Karl Wiberg98417032019-03-24 18:12:40162
163 ScopedYieldPolicy::YieldExecution();
Peter Boström7ddc9de2016-02-22 10:31:49164 pthread_mutex_lock(&event_mutex_);
Karl Wiberg98417032019-03-24 18:12:40165
Karl Wibergfc47c862019-04-11 08:31:24166 // Wait for `event_cond_` to trigger and `event_status_` to be set, with the
167 // given timeout (or without a timeout if none is given).
168 const auto wait = [&](const absl::optional<timespec> timeout_ts) {
169 int error = 0;
Karl Wiberg98417032019-03-24 18:12:40170 while (!event_status_ && error == 0) {
Karl Wibergfc47c862019-04-11 08:31:24171 if (timeout_ts == absl::nullopt) {
172 error = pthread_cond_wait(&event_cond_, &event_mutex_);
173 } else {
174#if USE_PTHREAD_COND_TIMEDWAIT_MONOTONIC_NP
175 error = pthread_cond_timedwait_monotonic_np(&event_cond_, &event_mutex_,
176 &*timeout_ts);
177#else
178 error =
179 pthread_cond_timedwait(&event_cond_, &event_mutex_, &*timeout_ts);
180#endif
181 }
182 }
183 return error;
184 };
185
186 int error;
187 if (warn_ts == absl::nullopt) {
188 error = wait(give_up_ts);
189 } else {
190 error = wait(warn_ts);
191 if (error == ETIMEDOUT) {
192 webrtc::WarnThatTheCurrentThreadIsProbablyDeadlocked();
193 error = wait(give_up_ts);
Karl Wiberg98417032019-03-24 18:12:40194 }
henrike@webrtc.orgf0488722014-05-13 18:00:26195 }
196
197 // NOTE(liulk): Exactly one thread will auto-reset this event. All
198 // the other threads will think it's unsignaled. This seems to be
199 // consistent with auto-reset events in WEBRTC_WIN
200 if (error == 0 && !is_manual_reset_)
201 event_status_ = false;
202
203 pthread_mutex_unlock(&event_mutex_);
204
205 return (error == 0);
206}
207
208#endif
209
210} // namespace rtc