blob: 42c22a29ccb0968a837deee9db6bddec1e4e0ab2 [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
Niels Möllerc572ff32018-11-07 08:43:50 +010027Event::Event() : Event(false, false) {}
28
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000029#if defined(WEBRTC_WIN)
30
tommi@webrtc.org4c0fd962015-02-09 10:23:27 +000031Event::Event(bool manual_reset, bool initially_signaled) {
deadbeef37f5ecf2017-02-27 14:06:41 -080032 event_handle_ = ::CreateEvent(nullptr, // Security attributes.
33 manual_reset, initially_signaled,
34 nullptr); // Name.
henrikg91d6ede2015-09-17 00:24:34 -070035 RTC_CHECK(event_handle_);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000036}
37
38Event::~Event() {
39 CloseHandle(event_handle_);
40}
41
42void Event::Set() {
43 SetEvent(event_handle_);
44}
45
46void Event::Reset() {
47 ResetEvent(event_handle_);
48}
49
tommi@webrtc.org1a072f92015-02-10 12:27:48 +000050bool Event::Wait(int milliseconds) {
51 DWORD ms = (milliseconds == kForever) ? INFINITE : milliseconds;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000052 return (WaitForSingleObject(event_handle_, ms) == WAIT_OBJECT_0);
53}
54
55#elif defined(WEBRTC_POSIX)
56
Niels Möller2cf9c552018-05-22 14:11:59 +020057// 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
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000074Event::Event(bool manual_reset, bool initially_signaled)
Yves Gerey665174f2018-06-19 15:03:05 +020075 : is_manual_reset_(manual_reset), event_status_(initially_signaled) {
deadbeef37f5ecf2017-02-27 14:06:41 -080076 RTC_CHECK(pthread_mutex_init(&event_mutex_, nullptr) == 0);
Niels Möller2cf9c552018-05-22 14:11:59 +020077 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);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000084}
85
86Event::~Event() {
87 pthread_mutex_destroy(&event_mutex_);
88 pthread_cond_destroy(&event_cond_);
89}
90
91void 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
98void Event::Reset() {
99 pthread_mutex_lock(&event_mutex_);
100 event_status_ = false;
101 pthread_mutex_unlock(&event_mutex_);
102}
103
tommi@webrtc.org1a072f92015-02-10 12:27:48 +0000104bool Event::Wait(int milliseconds) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000105 int error = 0;
106
Peter Boström7ddc9de2016-02-22 11:31:49 +0100107 struct timespec ts;
tommi@webrtc.org1a072f92015-02-10 12:27:48 +0000108 if (milliseconds != kForever) {
Niels Möller2cf9c552018-05-22 14:11:59 +0200109#if USE_CLOCK_GETTIME
110 clock_gettime(CLOCK_MONOTONIC, &ts);
111#else
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000112 struct timeval tv;
deadbeef37f5ecf2017-02-27 14:06:41 -0800113 gettimeofday(&tv, nullptr);
Niels Möller2cf9c552018-05-22 14:11:59 +0200114 ts.tv_sec = tv.tv_sec;
115 ts.tv_nsec = tv.tv_usec * 1000;
116#endif
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000117
Niels Möller2cf9c552018-05-22 14:11:59 +0200118 ts.tv_sec += (milliseconds / 1000);
119 ts.tv_nsec += (milliseconds % 1000) * 1000000;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000120
121 // Handle overflow.
122 if (ts.tv_nsec >= 1000000000) {
123 ts.tv_sec++;
124 ts.tv_nsec -= 1000000000;
125 }
Peter Boström7ddc9de2016-02-22 11:31:49 +0100126 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000127
Peter Boström7ddc9de2016-02-22 11:31:49 +0100128 pthread_mutex_lock(&event_mutex_);
129 if (milliseconds != kForever) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000130 while (!event_status_ && error == 0) {
Niels Möller2cf9c552018-05-22 14:11:59 +0200131#if USE_PTHREAD_COND_TIMEDWAIT_MONOTONIC_NP
132 error =
133 pthread_cond_timedwait_monotonic_np(&event_cond_, &event_mutex_, &ts);
134#else
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000135 error = pthread_cond_timedwait(&event_cond_, &event_mutex_, &ts);
Niels Möller2cf9c552018-05-22 14:11:59 +0200136#endif
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000137 }
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