blob: 71dca4980d0acfcc4f35641d4b14fb9f09049f9a [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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 06:47:31 +020011#include "rtc_base/event.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000012
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 Bonadei92ea95e2017-09-15 06:47:31 +020023#include "rtc_base/checks.h"
Sebastian Jansson7a603392019-03-20 16:50:35 +010024#include "rtc_base/synchronization/yield_policy.h"
tommi@webrtc.org4c0fd962015-02-09 10:23:27 +000025
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000026namespace rtc {
27
Niels Möllerc572ff32018-11-07 08:43:50 +010028Event::Event() : Event(false, false) {}
29
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000030#if defined(WEBRTC_WIN)
31
tommi@webrtc.org4c0fd962015-02-09 10:23:27 +000032Event::Event(bool manual_reset, bool initially_signaled) {
deadbeef37f5ecf2017-02-27 14:06:41 -080033 event_handle_ = ::CreateEvent(nullptr, // Security attributes.
34 manual_reset, initially_signaled,
35 nullptr); // Name.
henrikg91d6ede2015-09-17 00:24:34 -070036 RTC_CHECK(event_handle_);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000037}
38
39Event::~Event() {
40 CloseHandle(event_handle_);
41}
42
43void Event::Set() {
44 SetEvent(event_handle_);
45}
46
47void Event::Reset() {
48 ResetEvent(event_handle_);
49}
50
tommi@webrtc.org1a072f92015-02-10 12:27:48 +000051bool Event::Wait(int milliseconds) {
Sebastian Jansson7a603392019-03-20 16:50:35 +010052 ScopedYieldPolicy::YieldExecution();
tommi@webrtc.org1a072f92015-02-10 12:27:48 +000053 DWORD ms = (milliseconds == kForever) ? INFINITE : milliseconds;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000054 return (WaitForSingleObject(event_handle_, ms) == WAIT_OBJECT_0);
55}
56
57#elif defined(WEBRTC_POSIX)
58
Niels Möller2cf9c552018-05-22 14:11:59 +020059// On MacOS, clock_gettime is available from version 10.12, and on
60// iOS, from version 10.0. So we can't use it yet.
61#if defined(WEBRTC_MAC) || defined(WEBRTC_IOS)
62#define USE_CLOCK_GETTIME 0
63#define USE_PTHREAD_COND_TIMEDWAIT_MONOTONIC_NP 0
64// On Android, pthread_condattr_setclock is available from version 21. By
65// default, we target a new enough version for 64-bit platforms but not for
66// 32-bit platforms. For older versions, use
67// pthread_cond_timedwait_monotonic_np.
68#elif defined(WEBRTC_ANDROID) && (__ANDROID_API__ < 21)
69#define USE_CLOCK_GETTIME 1
70#define USE_PTHREAD_COND_TIMEDWAIT_MONOTONIC_NP 1
71#else
72#define USE_CLOCK_GETTIME 1
73#define USE_PTHREAD_COND_TIMEDWAIT_MONOTONIC_NP 0
74#endif
75
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000076Event::Event(bool manual_reset, bool initially_signaled)
Yves Gerey665174f2018-06-19 15:03:05 +020077 : is_manual_reset_(manual_reset), event_status_(initially_signaled) {
deadbeef37f5ecf2017-02-27 14:06:41 -080078 RTC_CHECK(pthread_mutex_init(&event_mutex_, nullptr) == 0);
Niels Möller2cf9c552018-05-22 14:11:59 +020079 pthread_condattr_t cond_attr;
80 RTC_CHECK(pthread_condattr_init(&cond_attr) == 0);
81#if USE_CLOCK_GETTIME && !USE_PTHREAD_COND_TIMEDWAIT_MONOTONIC_NP
82 RTC_CHECK(pthread_condattr_setclock(&cond_attr, CLOCK_MONOTONIC) == 0);
83#endif
84 RTC_CHECK(pthread_cond_init(&event_cond_, &cond_attr) == 0);
85 pthread_condattr_destroy(&cond_attr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000086}
87
88Event::~Event() {
89 pthread_mutex_destroy(&event_mutex_);
90 pthread_cond_destroy(&event_cond_);
91}
92
93void Event::Set() {
94 pthread_mutex_lock(&event_mutex_);
95 event_status_ = true;
96 pthread_cond_broadcast(&event_cond_);
97 pthread_mutex_unlock(&event_mutex_);
98}
99
100void Event::Reset() {
101 pthread_mutex_lock(&event_mutex_);
102 event_status_ = false;
103 pthread_mutex_unlock(&event_mutex_);
104}
105
tommi@webrtc.org1a072f92015-02-10 12:27:48 +0000106bool Event::Wait(int milliseconds) {
Sebastian Jansson7a603392019-03-20 16:50:35 +0100107 ScopedYieldPolicy::YieldExecution();
108
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000109 int error = 0;
110
Peter Boström7ddc9de2016-02-22 11:31:49 +0100111 struct timespec ts;
tommi@webrtc.org1a072f92015-02-10 12:27:48 +0000112 if (milliseconds != kForever) {
Niels Möller2cf9c552018-05-22 14:11:59 +0200113#if USE_CLOCK_GETTIME
114 clock_gettime(CLOCK_MONOTONIC, &ts);
115#else
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000116 struct timeval tv;
deadbeef37f5ecf2017-02-27 14:06:41 -0800117 gettimeofday(&tv, nullptr);
Niels Möller2cf9c552018-05-22 14:11:59 +0200118 ts.tv_sec = tv.tv_sec;
119 ts.tv_nsec = tv.tv_usec * 1000;
120#endif
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000121
Niels Möller2cf9c552018-05-22 14:11:59 +0200122 ts.tv_sec += (milliseconds / 1000);
123 ts.tv_nsec += (milliseconds % 1000) * 1000000;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000124
125 // Handle overflow.
126 if (ts.tv_nsec >= 1000000000) {
127 ts.tv_sec++;
128 ts.tv_nsec -= 1000000000;
129 }
Peter Boström7ddc9de2016-02-22 11:31:49 +0100130 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000131
Peter Boström7ddc9de2016-02-22 11:31:49 +0100132 pthread_mutex_lock(&event_mutex_);
133 if (milliseconds != kForever) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000134 while (!event_status_ && error == 0) {
Niels Möller2cf9c552018-05-22 14:11:59 +0200135#if USE_PTHREAD_COND_TIMEDWAIT_MONOTONIC_NP
136 error =
137 pthread_cond_timedwait_monotonic_np(&event_cond_, &event_mutex_, &ts);
138#else
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000139 error = pthread_cond_timedwait(&event_cond_, &event_mutex_, &ts);
Niels Möller2cf9c552018-05-22 14:11:59 +0200140#endif
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000141 }
142 } else {
143 while (!event_status_ && error == 0)
144 error = pthread_cond_wait(&event_cond_, &event_mutex_);
145 }
146
147 // NOTE(liulk): Exactly one thread will auto-reset this event. All
148 // the other threads will think it's unsignaled. This seems to be
149 // consistent with auto-reset events in WEBRTC_WIN
150 if (error == 0 && !is_manual_reset_)
151 event_status_ = false;
152
153 pthread_mutex_unlock(&event_mutex_);
154
155 return (error == 0);
156}
157
158#endif
159
160} // namespace rtc