blob: a9af20863194c02de5a9234b20966f60089c24ca [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:261/*
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:2723#include "webrtc/base/checks.h"
24
henrike@webrtc.orgf0488722014-05-13 18:00:2625namespace rtc {
26
27#if defined(WEBRTC_WIN)
28
tommi@webrtc.org4c0fd962015-02-09 10:23:2729Event::Event(bool manual_reset, bool initially_signaled) {
henrike@webrtc.orgf0488722014-05-13 18:00:2630 event_handle_ = ::CreateEvent(NULL, // Security attributes.
tommi@webrtc.org4c0fd962015-02-09 10:23:2731 manual_reset,
32 initially_signaled,
henrike@webrtc.orgf0488722014-05-13 18:00:2633 NULL); // Name.
henrikg91d6ede2015-09-17 07:24:3434 RTC_CHECK(event_handle_);
henrike@webrtc.orgf0488722014-05-13 18:00:2635}
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:4849bool Event::Wait(int milliseconds) {
50 DWORD ms = (milliseconds == kForever) ? INFINITE : milliseconds;
henrike@webrtc.orgf0488722014-05-13 18:00:2651 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 07:24:3459 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:2661}
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:4881bool Event::Wait(int milliseconds) {
henrike@webrtc.orgf0488722014-05-13 18:00:2682 pthread_mutex_lock(&event_mutex_);
83 int error = 0;
84
tommi@webrtc.org1a072f92015-02-10 12:27:4885 if (milliseconds != kForever) {
henrike@webrtc.orgf0488722014-05-13 18:00:2686 // 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).
tommi@webrtc.org1a072f92015-02-10 12:27:4893 ts.tv_sec = milliseconds / 1000;
94 ts.tv_nsec = (milliseconds % 1000) * 1000000;
henrike@webrtc.orgf0488722014-05-13 18:00:2695#else
96 struct timeval tv;
97 gettimeofday(&tv, NULL);
98
tommi@webrtc.org1a072f92015-02-10 12:27:4899 ts.tv_sec = tv.tv_sec + (milliseconds / 1000);
100 ts.tv_nsec = tv.tv_usec * 1000 + (milliseconds % 1000) * 1000000;
henrike@webrtc.orgf0488722014-05-13 18:00:26101
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