blob: 606d6ad49cdc8d0db555ea83d10252e8ecac517e [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
pbos@webrtc.org75025432015-02-06 08:32:32 +000013#include "webrtc/base/checks.h"
14
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000015#if defined(WEBRTC_WIN)
16#include <windows.h>
17#elif defined(WEBRTC_POSIX)
18#include <pthread.h>
19#include <sys/time.h>
20#include <time.h>
21#else
22#error "Must define either WEBRTC_WIN or WEBRTC_POSIX."
23#endif
24
25namespace rtc {
26
27#if defined(WEBRTC_WIN)
28
29Event::Event(bool manual_reset, bool initially_signaled)
30 : is_manual_reset_(manual_reset),
31 is_initially_signaled_(initially_signaled) {
32 event_handle_ = ::CreateEvent(NULL, // Security attributes.
33 is_manual_reset_,
34 is_initially_signaled_,
35 NULL); // Name.
pbos@webrtc.org75025432015-02-06 08:32:32 +000036 CHECK(event_handle_ != NULL);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000037}
38
39Event::~Event() {
40 CloseHandle(event_handle_);
41}
42
43void Event::Set() {
44 SetEvent(event_handle_);
45}
46
47void Event::Reset() {
48 ResetEvent(event_handle_);
49}
50
51bool Event::Wait(int cms) {
52 DWORD ms = (cms == kForever)? INFINITE : cms;
53 return (WaitForSingleObject(event_handle_, ms) == WAIT_OBJECT_0);
54}
55
56#elif defined(WEBRTC_POSIX)
57
58Event::Event(bool manual_reset, bool initially_signaled)
59 : is_manual_reset_(manual_reset),
60 event_status_(initially_signaled) {
pbos@webrtc.org75025432015-02-06 08:32:32 +000061 CHECK(pthread_mutex_init(&event_mutex_, NULL) == 0);
62 CHECK(pthread_cond_init(&event_cond_, NULL) == 0);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000063}
64
65Event::~Event() {
66 pthread_mutex_destroy(&event_mutex_);
67 pthread_cond_destroy(&event_cond_);
68}
69
70void Event::Set() {
71 pthread_mutex_lock(&event_mutex_);
72 event_status_ = true;
73 pthread_cond_broadcast(&event_cond_);
74 pthread_mutex_unlock(&event_mutex_);
75}
76
77void Event::Reset() {
78 pthread_mutex_lock(&event_mutex_);
79 event_status_ = false;
80 pthread_mutex_unlock(&event_mutex_);
81}
82
83bool Event::Wait(int cms) {
84 pthread_mutex_lock(&event_mutex_);
85 int error = 0;
86
87 if (cms != kForever) {
88 // Converting from seconds and microseconds (1e-6) plus
89 // milliseconds (1e-3) to seconds and nanoseconds (1e-9).
90
91 struct timespec ts;
92#if HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE
93 // Use relative time version, which tends to be more efficient for
94 // pthread implementations where provided (like on Android).
95 ts.tv_sec = cms / 1000;
96 ts.tv_nsec = (cms % 1000) * 1000000;
97#else
98 struct timeval tv;
99 gettimeofday(&tv, NULL);
100
101 ts.tv_sec = tv.tv_sec + (cms / 1000);
102 ts.tv_nsec = tv.tv_usec * 1000 + (cms % 1000) * 1000000;
103
104 // Handle overflow.
105 if (ts.tv_nsec >= 1000000000) {
106 ts.tv_sec++;
107 ts.tv_nsec -= 1000000000;
108 }
109#endif
110
111 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