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) |
| 16 | #include <pthread.h> |
| 17 | #include <sys/time.h> |
| 18 | #include <time.h> |
| 19 | #else |
| 20 | #error "Must define either WEBRTC_WIN or WEBRTC_POSIX." |
| 21 | #endif |
| 22 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 23 | #include "rtc_base/checks.h" |
tommi@webrtc.org | 4c0fd96 | 2015-02-09 10:23:27 +0000 | [diff] [blame] | 24 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 25 | namespace rtc { |
| 26 | |
| 27 | #if defined(WEBRTC_WIN) |
| 28 | |
tommi@webrtc.org | 4c0fd96 | 2015-02-09 10:23:27 +0000 | [diff] [blame] | 29 | Event::Event(bool manual_reset, bool initially_signaled) { |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 30 | event_handle_ = ::CreateEvent(nullptr, // Security attributes. |
| 31 | manual_reset, initially_signaled, |
| 32 | nullptr); // Name. |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 33 | RTC_CHECK(event_handle_); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | Event::~Event() { |
| 37 | CloseHandle(event_handle_); |
| 38 | } |
| 39 | |
| 40 | void Event::Set() { |
| 41 | SetEvent(event_handle_); |
| 42 | } |
| 43 | |
| 44 | void Event::Reset() { |
| 45 | ResetEvent(event_handle_); |
| 46 | } |
| 47 | |
tommi@webrtc.org | 1a072f9 | 2015-02-10 12:27:48 +0000 | [diff] [blame] | 48 | bool Event::Wait(int milliseconds) { |
| 49 | DWORD ms = (milliseconds == kForever) ? INFINITE : milliseconds; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 50 | return (WaitForSingleObject(event_handle_, ms) == WAIT_OBJECT_0); |
| 51 | } |
| 52 | |
| 53 | #elif defined(WEBRTC_POSIX) |
| 54 | |
Niels Möller | 2cf9c55 | 2018-05-22 14:11:59 +0200 | [diff] [blame] | 55 | // On MacOS, clock_gettime is available from version 10.12, and on |
| 56 | // iOS, from version 10.0. So we can't use it yet. |
| 57 | #if defined(WEBRTC_MAC) || defined(WEBRTC_IOS) |
| 58 | #define USE_CLOCK_GETTIME 0 |
| 59 | #define USE_PTHREAD_COND_TIMEDWAIT_MONOTONIC_NP 0 |
| 60 | // On Android, pthread_condattr_setclock is available from version 21. By |
| 61 | // default, we target a new enough version for 64-bit platforms but not for |
| 62 | // 32-bit platforms. For older versions, use |
| 63 | // pthread_cond_timedwait_monotonic_np. |
| 64 | #elif defined(WEBRTC_ANDROID) && (__ANDROID_API__ < 21) |
| 65 | #define USE_CLOCK_GETTIME 1 |
| 66 | #define USE_PTHREAD_COND_TIMEDWAIT_MONOTONIC_NP 1 |
| 67 | #else |
| 68 | #define USE_CLOCK_GETTIME 1 |
| 69 | #define USE_PTHREAD_COND_TIMEDWAIT_MONOTONIC_NP 0 |
| 70 | #endif |
| 71 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 72 | Event::Event(bool manual_reset, bool initially_signaled) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 73 | : is_manual_reset_(manual_reset), event_status_(initially_signaled) { |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 74 | RTC_CHECK(pthread_mutex_init(&event_mutex_, nullptr) == 0); |
Niels Möller | 2cf9c55 | 2018-05-22 14:11:59 +0200 | [diff] [blame] | 75 | pthread_condattr_t cond_attr; |
| 76 | RTC_CHECK(pthread_condattr_init(&cond_attr) == 0); |
| 77 | #if USE_CLOCK_GETTIME && !USE_PTHREAD_COND_TIMEDWAIT_MONOTONIC_NP |
| 78 | RTC_CHECK(pthread_condattr_setclock(&cond_attr, CLOCK_MONOTONIC) == 0); |
| 79 | #endif |
| 80 | RTC_CHECK(pthread_cond_init(&event_cond_, &cond_attr) == 0); |
| 81 | pthread_condattr_destroy(&cond_attr); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | Event::~Event() { |
| 85 | pthread_mutex_destroy(&event_mutex_); |
| 86 | pthread_cond_destroy(&event_cond_); |
| 87 | } |
| 88 | |
| 89 | void Event::Set() { |
| 90 | pthread_mutex_lock(&event_mutex_); |
| 91 | event_status_ = true; |
| 92 | pthread_cond_broadcast(&event_cond_); |
| 93 | pthread_mutex_unlock(&event_mutex_); |
| 94 | } |
| 95 | |
| 96 | void Event::Reset() { |
| 97 | pthread_mutex_lock(&event_mutex_); |
| 98 | event_status_ = false; |
| 99 | pthread_mutex_unlock(&event_mutex_); |
| 100 | } |
| 101 | |
tommi@webrtc.org | 1a072f9 | 2015-02-10 12:27:48 +0000 | [diff] [blame] | 102 | bool Event::Wait(int milliseconds) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 103 | int error = 0; |
| 104 | |
Peter Boström | 7ddc9de | 2016-02-22 11:31:49 +0100 | [diff] [blame] | 105 | struct timespec ts; |
tommi@webrtc.org | 1a072f9 | 2015-02-10 12:27:48 +0000 | [diff] [blame] | 106 | if (milliseconds != kForever) { |
Niels Möller | 2cf9c55 | 2018-05-22 14:11:59 +0200 | [diff] [blame] | 107 | #if USE_CLOCK_GETTIME |
| 108 | clock_gettime(CLOCK_MONOTONIC, &ts); |
| 109 | #else |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 110 | struct timeval tv; |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 111 | gettimeofday(&tv, nullptr); |
Niels Möller | 2cf9c55 | 2018-05-22 14:11:59 +0200 | [diff] [blame] | 112 | ts.tv_sec = tv.tv_sec; |
| 113 | ts.tv_nsec = tv.tv_usec * 1000; |
| 114 | #endif |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 115 | |
Niels Möller | 2cf9c55 | 2018-05-22 14:11:59 +0200 | [diff] [blame] | 116 | ts.tv_sec += (milliseconds / 1000); |
| 117 | ts.tv_nsec += (milliseconds % 1000) * 1000000; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 118 | |
| 119 | // Handle overflow. |
| 120 | if (ts.tv_nsec >= 1000000000) { |
| 121 | ts.tv_sec++; |
| 122 | ts.tv_nsec -= 1000000000; |
| 123 | } |
Peter Boström | 7ddc9de | 2016-02-22 11:31:49 +0100 | [diff] [blame] | 124 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 125 | |
Peter Boström | 7ddc9de | 2016-02-22 11:31:49 +0100 | [diff] [blame] | 126 | pthread_mutex_lock(&event_mutex_); |
| 127 | if (milliseconds != kForever) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 128 | while (!event_status_ && error == 0) { |
Niels Möller | 2cf9c55 | 2018-05-22 14:11:59 +0200 | [diff] [blame] | 129 | #if USE_PTHREAD_COND_TIMEDWAIT_MONOTONIC_NP |
| 130 | error = |
| 131 | pthread_cond_timedwait_monotonic_np(&event_cond_, &event_mutex_, &ts); |
| 132 | #else |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 133 | error = pthread_cond_timedwait(&event_cond_, &event_mutex_, &ts); |
Niels Möller | 2cf9c55 | 2018-05-22 14:11:59 +0200 | [diff] [blame] | 134 | #endif |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 135 | } |
| 136 | } else { |
| 137 | while (!event_status_ && error == 0) |
| 138 | error = pthread_cond_wait(&event_cond_, &event_mutex_); |
| 139 | } |
| 140 | |
| 141 | // NOTE(liulk): Exactly one thread will auto-reset this event. All |
| 142 | // the other threads will think it's unsignaled. This seems to be |
| 143 | // consistent with auto-reset events in WEBRTC_WIN |
| 144 | if (error == 0 && !is_manual_reset_) |
| 145 | event_status_ = false; |
| 146 | |
| 147 | pthread_mutex_unlock(&event_mutex_); |
| 148 | |
| 149 | return (error == 0); |
| 150 | } |
| 151 | |
| 152 | #endif |
| 153 | |
| 154 | } // namespace rtc |