blob: 7cd944329ce28a74ded430831e103ef7bc1bb08b [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.
tommi@webrtc.org4c0fd962015-02-09 10:23:27 +000034 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
49bool Event::Wait(int cms) {
tommi@webrtc.org4c0fd962015-02-09 10:23:27 +000050 DWORD ms = (cms == kForever) ? INFINITE : cms;
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) {
tommi@webrtc.org4c0fd962015-02-09 10:23:27 +000059 CHECK(pthread_mutex_init(&event_mutex_, NULL) == 0);
60 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
81bool Event::Wait(int cms) {
82 pthread_mutex_lock(&event_mutex_);
83 int error = 0;
84
85 if (cms != 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 = cms / 1000;
94 ts.tv_nsec = (cms % 1000) * 1000000;
95#else
96 struct timeval tv;
97 gettimeofday(&tv, NULL);
98
99 ts.tv_sec = tv.tv_sec + (cms / 1000);
100 ts.tv_nsec = tv.tv_usec * 1000 + (cms % 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