henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "rtc_base/event.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 12 | |
| 13 | #if defined(WEBRTC_WIN) |
| 14 | #include <windows.h> |
| 15 | #elif defined(WEBRTC_POSIX) |
Karl Wiberg | 9841703 | 2019-03-24 19:12:40 +0100 | [diff] [blame] | 16 | #include <errno.h> |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 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 | |
Karl Wiberg | fc47c86 | 2019-04-11 10:31:24 +0200 | [diff] [blame] | 24 | #include "absl/types/optional.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 25 | #include "rtc_base/checks.h" |
Sebastian Jansson | 7a60339 | 2019-03-20 16:50:35 +0100 | [diff] [blame] | 26 | #include "rtc_base/synchronization/yield_policy.h" |
Karl Wiberg | 9841703 | 2019-03-24 19:12:40 +0100 | [diff] [blame] | 27 | #include "rtc_base/system/warn_current_thread_is_deadlocked.h" |
tommi@webrtc.org | 4c0fd96 | 2015-02-09 10:23:27 +0000 | [diff] [blame] | 28 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 29 | namespace rtc { |
| 30 | |
Niels Möller | c572ff3 | 2018-11-07 08:43:50 +0100 | [diff] [blame] | 31 | Event::Event() : Event(false, false) {} |
| 32 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 33 | #if defined(WEBRTC_WIN) |
| 34 | |
tommi@webrtc.org | 4c0fd96 | 2015-02-09 10:23:27 +0000 | [diff] [blame] | 35 | Event::Event(bool manual_reset, bool initially_signaled) { |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 36 | event_handle_ = ::CreateEvent(nullptr, // Security attributes. |
| 37 | manual_reset, initially_signaled, |
| 38 | nullptr); // Name. |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 39 | RTC_CHECK(event_handle_); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | Event::~Event() { |
| 43 | CloseHandle(event_handle_); |
| 44 | } |
| 45 | |
| 46 | void Event::Set() { |
| 47 | SetEvent(event_handle_); |
| 48 | } |
| 49 | |
| 50 | void Event::Reset() { |
| 51 | ResetEvent(event_handle_); |
| 52 | } |
| 53 | |
Karl Wiberg | fc47c86 | 2019-04-11 10:31:24 +0200 | [diff] [blame] | 54 | bool Event::Wait(const int give_up_after_ms, int /*warn_after_ms*/) { |
Sebastian Jansson | 7a60339 | 2019-03-20 16:50:35 +0100 | [diff] [blame] | 55 | ScopedYieldPolicy::YieldExecution(); |
Karl Wiberg | fc47c86 | 2019-04-11 10:31:24 +0200 | [diff] [blame] | 56 | const DWORD ms = give_up_after_ms == kForever ? INFINITE : give_up_after_ms; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 57 | return (WaitForSingleObject(event_handle_, ms) == WAIT_OBJECT_0); |
| 58 | } |
| 59 | |
| 60 | #elif defined(WEBRTC_POSIX) |
| 61 | |
Niels Möller | 2cf9c55 | 2018-05-22 14:11:59 +0200 | [diff] [blame] | 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 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 79 | Event::Event(bool manual_reset, bool initially_signaled) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 80 | : is_manual_reset_(manual_reset), event_status_(initially_signaled) { |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 81 | RTC_CHECK(pthread_mutex_init(&event_mutex_, nullptr) == 0); |
Niels Möller | 2cf9c55 | 2018-05-22 14:11:59 +0200 | [diff] [blame] | 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); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | Event::~Event() { |
| 92 | pthread_mutex_destroy(&event_mutex_); |
| 93 | pthread_cond_destroy(&event_cond_); |
| 94 | } |
| 95 | |
| 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 | |
| 103 | void Event::Reset() { |
| 104 | pthread_mutex_lock(&event_mutex_); |
| 105 | event_status_ = false; |
| 106 | pthread_mutex_unlock(&event_mutex_); |
| 107 | } |
| 108 | |
Karl Wiberg | 9841703 | 2019-03-24 19:12:40 +0100 | [diff] [blame] | 109 | namespace { |
Sebastian Jansson | 7a60339 | 2019-03-20 16:50:35 +0100 | [diff] [blame] | 110 | |
Karl Wiberg | 9841703 | 2019-03-24 19:12:40 +0100 | [diff] [blame] | 111 | timespec GetTimespec(const int milliseconds_from_now) { |
| 112 | timespec ts; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 113 | |
Karl Wiberg | 9841703 | 2019-03-24 19:12:40 +0100 | [diff] [blame] | 114 | // Get the current time. |
Niels Möller | 2cf9c55 | 2018-05-22 14:11:59 +0200 | [diff] [blame] | 115 | #if USE_CLOCK_GETTIME |
Karl Wiberg | 9841703 | 2019-03-24 19:12:40 +0100 | [diff] [blame] | 116 | clock_gettime(CLOCK_MONOTONIC, &ts); |
Niels Möller | 2cf9c55 | 2018-05-22 14:11:59 +0200 | [diff] [blame] | 117 | #else |
Karl Wiberg | 9841703 | 2019-03-24 19:12:40 +0100 | [diff] [blame] | 118 | timeval tv; |
| 119 | gettimeofday(&tv, nullptr); |
| 120 | ts.tv_sec = tv.tv_sec; |
| 121 | ts.tv_nsec = tv.tv_usec * 1000; |
Niels Möller | 2cf9c55 | 2018-05-22 14:11:59 +0200 | [diff] [blame] | 122 | #endif |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 123 | |
Karl Wiberg | 9841703 | 2019-03-24 19:12:40 +0100 | [diff] [blame] | 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; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 127 | |
Karl Wiberg | 9841703 | 2019-03-24 19:12:40 +0100 | [diff] [blame] | 128 | // Normalize. |
| 129 | if (ts.tv_nsec >= 1000000000) { |
| 130 | ts.tv_sec++; |
| 131 | ts.tv_nsec -= 1000000000; |
Peter Boström | 7ddc9de | 2016-02-22 11:31:49 +0100 | [diff] [blame] | 132 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 133 | |
Karl Wiberg | 9841703 | 2019-03-24 19:12:40 +0100 | [diff] [blame] | 134 | return ts; |
| 135 | } |
| 136 | |
| 137 | } // namespace |
| 138 | |
Karl Wiberg | fc47c86 | 2019-04-11 10:31:24 +0200 | [diff] [blame] | 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)); |
Karl Wiberg | 9841703 | 2019-03-24 19:12:40 +0100 | [diff] [blame] | 155 | |
| 156 | ScopedYieldPolicy::YieldExecution(); |
Peter Boström | 7ddc9de | 2016-02-22 11:31:49 +0100 | [diff] [blame] | 157 | pthread_mutex_lock(&event_mutex_); |
Karl Wiberg | 9841703 | 2019-03-24 19:12:40 +0100 | [diff] [blame] | 158 | |
Karl Wiberg | fc47c86 | 2019-04-11 10:31:24 +0200 | [diff] [blame] | 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; |
Karl Wiberg | 9841703 | 2019-03-24 19:12:40 +0100 | [diff] [blame] | 163 | while (!event_status_ && error == 0) { |
Karl Wiberg | fc47c86 | 2019-04-11 10:31:24 +0200 | [diff] [blame] | 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); |
Karl Wiberg | 9841703 | 2019-03-24 19:12:40 +0100 | [diff] [blame] | 187 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 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 |