blob: 0e267dc1ac31a0743613650233229a0e6a12e6d0 [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
11#include "webrtc/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
tommi@webrtc.org4c0fd962015-02-09 10:23:27 +000023#include "webrtc/base/checks.h"
24
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) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000030 event_handle_ = ::CreateEvent(NULL, // Security attributes.
tommi@webrtc.org4c0fd962015-02-09 10:23:27 +000031 manual_reset,
32 initially_signaled,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000033 NULL); // Name.
henrikg91d6ede2015-09-17 00:24:34 -070034 RTC_CHECK(event_handle_);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000035}
36
37Event::~Event() {
38 CloseHandle(event_handle_);
39}
40
41void Event::Set() {
42 SetEvent(event_handle_);
43}
44
45void Event::Reset() {
46 ResetEvent(event_handle_);
47}
48
tommi@webrtc.org1a072f92015-02-10 12:27:48 +000049bool Event::Wait(int milliseconds) {
50 DWORD ms = (milliseconds == kForever) ? INFINITE : milliseconds;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000051 return (WaitForSingleObject(event_handle_, ms) == WAIT_OBJECT_0);
52}
53
54#elif defined(WEBRTC_POSIX)
55
56Event::Event(bool manual_reset, bool initially_signaled)
57 : is_manual_reset_(manual_reset),
58 event_status_(initially_signaled) {
henrikg91d6ede2015-09-17 00:24:34 -070059 RTC_CHECK(pthread_mutex_init(&event_mutex_, NULL) == 0);
60 RTC_CHECK(pthread_cond_init(&event_cond_, NULL) == 0);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000061}
62
63Event::~Event() {
64 pthread_mutex_destroy(&event_mutex_);
65 pthread_cond_destroy(&event_cond_);
66}
67
68void Event::Set() {
69 pthread_mutex_lock(&event_mutex_);
70 event_status_ = true;
71 pthread_cond_broadcast(&event_cond_);
72 pthread_mutex_unlock(&event_mutex_);
73}
74
75void Event::Reset() {
76 pthread_mutex_lock(&event_mutex_);
77 event_status_ = false;
78 pthread_mutex_unlock(&event_mutex_);
79}
80
tommi@webrtc.org1a072f92015-02-10 12:27:48 +000081bool Event::Wait(int milliseconds) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000082 int error = 0;
83
Peter Boström7ddc9de2016-02-22 11:31:49 +010084 struct timespec ts;
tommi@webrtc.org1a072f92015-02-10 12:27:48 +000085 if (milliseconds != kForever) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000086 // Converting from seconds and microseconds (1e-6) plus
87 // milliseconds (1e-3) to seconds and nanoseconds (1e-9).
88
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000089#if HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE
90 // Use relative time version, which tends to be more efficient for
91 // pthread implementations where provided (like on Android).
tommi@webrtc.org1a072f92015-02-10 12:27:48 +000092 ts.tv_sec = milliseconds / 1000;
93 ts.tv_nsec = (milliseconds % 1000) * 1000000;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000094#else
95 struct timeval tv;
96 gettimeofday(&tv, NULL);
97
tommi@webrtc.org1a072f92015-02-10 12:27:48 +000098 ts.tv_sec = tv.tv_sec + (milliseconds / 1000);
99 ts.tv_nsec = tv.tv_usec * 1000 + (milliseconds % 1000) * 1000000;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000100
101 // Handle overflow.
102 if (ts.tv_nsec >= 1000000000) {
103 ts.tv_sec++;
104 ts.tv_nsec -= 1000000000;
105 }
106#endif
Peter Boström7ddc9de2016-02-22 11:31:49 +0100107 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000108
Peter Boström7ddc9de2016-02-22 11:31:49 +0100109 pthread_mutex_lock(&event_mutex_);
110 if (milliseconds != kForever) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000111 while (!event_status_ && error == 0) {
112#if HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE
113 error = pthread_cond_timedwait_relative_np(
114 &event_cond_, &event_mutex_, &ts);
115#else
116 error = pthread_cond_timedwait(&event_cond_, &event_mutex_, &ts);
117#endif
118 }
119 } else {
120 while (!event_status_ && error == 0)
121 error = pthread_cond_wait(&event_cond_, &event_mutex_);
122 }
123
124 // NOTE(liulk): Exactly one thread will auto-reset this event. All
125 // the other threads will think it's unsignaled. This seems to be
126 // consistent with auto-reset events in WEBRTC_WIN
127 if (error == 0 && !is_manual_reset_)
128 event_status_ = false;
129
130 pthread_mutex_unlock(&event_mutex_);
131
132 return (error == 0);
133}
134
135#endif
136
137} // namespace rtc