blob: 6c9639b4fecc111a807eeaa18aef03ba71816baa [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"
tommi@webrtc.org4c0fd962015-02-09 10:23:27 +000024
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000025namespace rtc {
26
27#if defined(WEBRTC_WIN)
28
tommi@webrtc.org4c0fd962015-02-09 10:23:27 +000029Event::Event(bool manual_reset, bool initially_signaled) {
deadbeef37f5ecf2017-02-27 14:06:41 -080030 event_handle_ = ::CreateEvent(nullptr, // Security attributes.
31 manual_reset, initially_signaled,
32 nullptr); // Name.
henrikg91d6ede2015-09-17 00:24:34 -070033 RTC_CHECK(event_handle_);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000034}
35
36Event::~Event() {
37 CloseHandle(event_handle_);
38}
39
40void Event::Set() {
41 SetEvent(event_handle_);
42}
43
44void Event::Reset() {
45 ResetEvent(event_handle_);
46}
47
tommi@webrtc.org1a072f92015-02-10 12:27:48 +000048bool Event::Wait(int milliseconds) {
49 DWORD ms = (milliseconds == kForever) ? INFINITE : milliseconds;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000050 return (WaitForSingleObject(event_handle_, ms) == WAIT_OBJECT_0);
51}
52
53#elif defined(WEBRTC_POSIX)
54
Niels Möller2cf9c552018-05-22 14:11:59 +020055// 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.orgf0488722014-05-13 18:00:26 +000072Event::Event(bool manual_reset, bool initially_signaled)
Yves Gerey665174f2018-06-19 15:03:05 +020073 : is_manual_reset_(manual_reset), event_status_(initially_signaled) {
deadbeef37f5ecf2017-02-27 14:06:41 -080074 RTC_CHECK(pthread_mutex_init(&event_mutex_, nullptr) == 0);
Niels Möller2cf9c552018-05-22 14:11:59 +020075 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.orgf0488722014-05-13 18:00:26 +000082}
83
84Event::~Event() {
85 pthread_mutex_destroy(&event_mutex_);
86 pthread_cond_destroy(&event_cond_);
87}
88
89void 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
96void Event::Reset() {
97 pthread_mutex_lock(&event_mutex_);
98 event_status_ = false;
99 pthread_mutex_unlock(&event_mutex_);
100}
101
tommi@webrtc.org1a072f92015-02-10 12:27:48 +0000102bool Event::Wait(int milliseconds) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000103 int error = 0;
104
Peter Boström7ddc9de2016-02-22 11:31:49 +0100105 struct timespec ts;
tommi@webrtc.org1a072f92015-02-10 12:27:48 +0000106 if (milliseconds != kForever) {
Niels Möller2cf9c552018-05-22 14:11:59 +0200107#if USE_CLOCK_GETTIME
108 clock_gettime(CLOCK_MONOTONIC, &ts);
109#else
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000110 struct timeval tv;
deadbeef37f5ecf2017-02-27 14:06:41 -0800111 gettimeofday(&tv, nullptr);
Niels Möller2cf9c552018-05-22 14:11:59 +0200112 ts.tv_sec = tv.tv_sec;
113 ts.tv_nsec = tv.tv_usec * 1000;
114#endif
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000115
Niels Möller2cf9c552018-05-22 14:11:59 +0200116 ts.tv_sec += (milliseconds / 1000);
117 ts.tv_nsec += (milliseconds % 1000) * 1000000;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000118
119 // Handle overflow.
120 if (ts.tv_nsec >= 1000000000) {
121 ts.tv_sec++;
122 ts.tv_nsec -= 1000000000;
123 }
Peter Boström7ddc9de2016-02-22 11:31:49 +0100124 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000125
Peter Boström7ddc9de2016-02-22 11:31:49 +0100126 pthread_mutex_lock(&event_mutex_);
127 if (milliseconds != kForever) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000128 while (!event_status_ && error == 0) {
Niels Möller2cf9c552018-05-22 14:11:59 +0200129#if USE_PTHREAD_COND_TIMEDWAIT_MONOTONIC_NP
130 error =
131 pthread_cond_timedwait_monotonic_np(&event_cond_, &event_mutex_, &ts);
132#else
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000133 error = pthread_cond_timedwait(&event_cond_, &event_mutex_, &ts);
Niels Möller2cf9c552018-05-22 14:11:59 +0200134#endif
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000135 }
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