1 /*
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
11 #include "rtc_base/event.h"
12
13 #if defined(WEBRTC_WIN)
14 #include <windows.h>
15 #elif defined(WEBRTC_POSIX)
16 #include <errno.h>
17 #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
24 #include "absl/types/optional.h"
25 #include "rtc_base/checks.h"
26 #include "rtc_base/synchronization/yield_policy.h"
27 #include "rtc_base/system/warn_current_thread_is_deadlocked.h"
28
29 namespace rtc {
30
Event()31 Event::Event() : Event(false, false) {}
32
33 #if defined(WEBRTC_WIN)
34
Event(bool manual_reset,bool initially_signaled)35 Event::Event(bool manual_reset, bool initially_signaled) {
36 event_handle_ = ::CreateEvent(nullptr, // Security attributes.
37 manual_reset, initially_signaled,
38 nullptr); // Name.
39 RTC_CHECK(event_handle_);
40 }
41
~Event()42 Event::~Event() {
43 CloseHandle(event_handle_);
44 }
45
Set()46 void Event::Set() {
47 SetEvent(event_handle_);
48 }
49
Reset()50 void Event::Reset() {
51 ResetEvent(event_handle_);
52 }
53
Wait(const int give_up_after_ms,int)54 bool Event::Wait(const int give_up_after_ms, int /*warn_after_ms*/) {
55 ScopedYieldPolicy::YieldExecution();
56 const DWORD ms = give_up_after_ms == kForever ? INFINITE : give_up_after_ms;
57 return (WaitForSingleObject(event_handle_, ms) == WAIT_OBJECT_0);
58 }
59
60 #elif defined(WEBRTC_POSIX)
61
62 // 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
Event(bool manual_reset,bool initially_signaled)79 Event::Event(bool manual_reset, bool initially_signaled)
80 : is_manual_reset_(manual_reset), event_status_(initially_signaled) {
81 RTC_CHECK(pthread_mutex_init(&event_mutex_, nullptr) == 0);
82 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);
89 }
90
~Event()91 Event::~Event() {
92 pthread_mutex_destroy(&event_mutex_);
93 pthread_cond_destroy(&event_cond_);
94 }
95
Set()96 void 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
Reset()103 void Event::Reset() {
104 pthread_mutex_lock(&event_mutex_);
105 event_status_ = false;
106 pthread_mutex_unlock(&event_mutex_);
107 }
108
109 namespace {
110
GetTimespec(const int milliseconds_from_now)111 timespec GetTimespec(const int milliseconds_from_now) {
112 timespec ts;
113
114 // Get the current time.
115 #if USE_CLOCK_GETTIME
116 clock_gettime(CLOCK_MONOTONIC, &ts);
117 #else
118 timeval tv;
119 gettimeofday(&tv, nullptr);
120 ts.tv_sec = tv.tv_sec;
121 ts.tv_nsec = tv.tv_usec * 1000;
122 #endif
123
124 // 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;
127
128 // Normalize.
129 if (ts.tv_nsec >= 1000000000) {
130 ts.tv_sec++;
131 ts.tv_nsec -= 1000000000;
132 }
133
134 return ts;
135 }
136
137 } // namespace
138
Wait(const int give_up_after_ms,const int warn_after_ms)139 bool 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));
155
156 ScopedYieldPolicy::YieldExecution();
157 pthread_mutex_lock(&event_mutex_);
158
159 // 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;
163 while (!event_status_ && error == 0) {
164 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);
187 }
188 }
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
204