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