1 /*
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
23 #include "webrtc/base/checks.h"
24
25 namespace rtc {
26
27 #if defined(WEBRTC_WIN)
28
Event(bool manual_reset,bool initially_signaled)29 Event::Event(bool manual_reset, bool initially_signaled) {
30 event_handle_ = ::CreateEvent(NULL, // Security attributes.
31 manual_reset,
32 initially_signaled,
33 NULL); // Name.
34 RTC_CHECK(event_handle_);
35 }
36
~Event()37 Event::~Event() {
38 CloseHandle(event_handle_);
39 }
40
Set()41 void Event::Set() {
42 SetEvent(event_handle_);
43 }
44
Reset()45 void Event::Reset() {
46 ResetEvent(event_handle_);
47 }
48
Wait(int milliseconds)49 bool Event::Wait(int milliseconds) {
50 DWORD ms = (milliseconds == kForever) ? INFINITE : milliseconds;
51 return (WaitForSingleObject(event_handle_, ms) == WAIT_OBJECT_0);
52 }
53
54 #elif defined(WEBRTC_POSIX)
55
56 Event::Event(bool manual_reset, bool initially_signaled)
57 : is_manual_reset_(manual_reset),
58 event_status_(initially_signaled) {
59 RTC_CHECK(pthread_mutex_init(&event_mutex_, NULL) == 0);
60 RTC_CHECK(pthread_cond_init(&event_cond_, NULL) == 0);
61 }
62
63 Event::~Event() {
64 pthread_mutex_destroy(&event_mutex_);
65 pthread_cond_destroy(&event_cond_);
66 }
67
68 void 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
75 void Event::Reset() {
76 pthread_mutex_lock(&event_mutex_);
77 event_status_ = false;
78 pthread_mutex_unlock(&event_mutex_);
79 }
80
81 bool Event::Wait(int milliseconds) {
82 pthread_mutex_lock(&event_mutex_);
83 int error = 0;
84
85 if (milliseconds != kForever) {
86 // Converting from seconds and microseconds (1e-6) plus
87 // milliseconds (1e-3) to seconds and nanoseconds (1e-9).
88
89 struct timespec ts;
90 #if HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE
91 // Use relative time version, which tends to be more efficient for
92 // pthread implementations where provided (like on Android).
93 ts.tv_sec = milliseconds / 1000;
94 ts.tv_nsec = (milliseconds % 1000) * 1000000;
95 #else
96 struct timeval tv;
97 gettimeofday(&tv, NULL);
98
99 ts.tv_sec = tv.tv_sec + (milliseconds / 1000);
100 ts.tv_nsec = tv.tv_usec * 1000 + (milliseconds % 1000) * 1000000;
101
102 // Handle overflow.
103 if (ts.tv_nsec >= 1000000000) {
104 ts.tv_sec++;
105 ts.tv_nsec -= 1000000000;
106 }
107 #endif
108
109 while (!event_status_ && error == 0) {
110 #if HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE
111 error = pthread_cond_timedwait_relative_np(
112 &event_cond_, &event_mutex_, &ts);
113 #else
114 error = pthread_cond_timedwait(&event_cond_, &event_mutex_, &ts);
115 #endif
116 }
117 } else {
118 while (!event_status_ && error == 0)
119 error = pthread_cond_wait(&event_cond_, &event_mutex_);
120 }
121
122 // NOTE(liulk): Exactly one thread will auto-reset this event. All
123 // the other threads will think it's unsignaled. This seems to be
124 // consistent with auto-reset events in WEBRTC_WIN
125 if (error == 0 && !is_manual_reset_)
126 event_status_ = false;
127
128 pthread_mutex_unlock(&event_mutex_);
129
130 return (error == 0);
131 }
132
133 #endif
134
135 } // namespace rtc
136