blob: 9e38159b6601e5527bc4090a73c9f067373bfc3c [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)
73 : is_manual_reset_(manual_reset),
74 event_status_(initially_signaled) {
deadbeef37f5ecf2017-02-27 14:06:41 -080075 RTC_CHECK(pthread_mutex_init(&event_mutex_, nullptr) == 0);
Niels Möller2cf9c552018-05-22 14:11:59 +020076 pthread_condattr_t cond_attr;
77 RTC_CHECK(pthread_condattr_init(&cond_attr) == 0);
78#if USE_CLOCK_GETTIME && !USE_PTHREAD_COND_TIMEDWAIT_MONOTONIC_NP
79 RTC_CHECK(pthread_condattr_setclock(&cond_attr, CLOCK_MONOTONIC) == 0);
80#endif
81 RTC_CHECK(pthread_cond_init(&event_cond_, &cond_attr) == 0);
82 pthread_condattr_destroy(&cond_attr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000083}
84
85Event::~Event() {
86 pthread_mutex_destroy(&event_mutex_);
87 pthread_cond_destroy(&event_cond_);
88}
89
90void Event::Set() {
91 pthread_mutex_lock(&event_mutex_);
92 event_status_ = true;
93 pthread_cond_broadcast(&event_cond_);
94 pthread_mutex_unlock(&event_mutex_);
95}
96
97void Event::Reset() {
98 pthread_mutex_lock(&event_mutex_);
99 event_status_ = false;
100 pthread_mutex_unlock(&event_mutex_);
101}
102
tommi@webrtc.org1a072f92015-02-10 12:27:48 +0000103bool Event::Wait(int milliseconds) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000104 int error = 0;
105
Peter Boström7ddc9de2016-02-22 11:31:49 +0100106 struct timespec ts;
tommi@webrtc.org1a072f92015-02-10 12:27:48 +0000107 if (milliseconds != kForever) {
Niels Möller2cf9c552018-05-22 14:11:59 +0200108#if USE_CLOCK_GETTIME
109 clock_gettime(CLOCK_MONOTONIC, &ts);
110#else
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000111 struct timeval tv;
deadbeef37f5ecf2017-02-27 14:06:41 -0800112 gettimeofday(&tv, nullptr);
Niels Möller2cf9c552018-05-22 14:11:59 +0200113 ts.tv_sec = tv.tv_sec;
114 ts.tv_nsec = tv.tv_usec * 1000;
115#endif
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000116
Niels Möller2cf9c552018-05-22 14:11:59 +0200117 ts.tv_sec += (milliseconds / 1000);
118 ts.tv_nsec += (milliseconds % 1000) * 1000000;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000119
120 // Handle overflow.
121 if (ts.tv_nsec >= 1000000000) {
122 ts.tv_sec++;
123 ts.tv_nsec -= 1000000000;
124 }
Peter Boström7ddc9de2016-02-22 11:31:49 +0100125 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000126
Peter Boström7ddc9de2016-02-22 11:31:49 +0100127 pthread_mutex_lock(&event_mutex_);
128 if (milliseconds != kForever) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000129 while (!event_status_ && error == 0) {
Niels Möller2cf9c552018-05-22 14:11:59 +0200130#if USE_PTHREAD_COND_TIMEDWAIT_MONOTONIC_NP
131 error =
132 pthread_cond_timedwait_monotonic_np(&event_cond_, &event_mutex_, &ts);
133#else
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000134 error = pthread_cond_timedwait(&event_cond_, &event_mutex_, &ts);
Niels Möller2cf9c552018-05-22 14:11:59 +0200135#endif
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000136 }
137 } else {
138 while (!event_status_ && error == 0)
139 error = pthread_cond_wait(&event_cond_, &event_mutex_);
140 }
141
142 // NOTE(liulk): Exactly one thread will auto-reset this event. All
143 // the other threads will think it's unsignaled. This seems to be
144 // consistent with auto-reset events in WEBRTC_WIN
145 if (error == 0 && !is_manual_reset_)
146 event_status_ = false;
147
148 pthread_mutex_unlock(&event_mutex_);
149
150 return (error == 0);
151}
152
153#endif
154
155} // namespace rtc