Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(8)

Side by Side Diff: webrtc/base/event.cc

Issue 2877023002: Move webrtc/{base => rtc_base} (Closed)
Patch Set: update presubmit.py and DEPS include rules Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « webrtc/base/event.h ('k') | webrtc/base/event_tracer.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
29 Event::Event(bool manual_reset, bool initially_signaled) {
30 event_handle_ = ::CreateEvent(nullptr, // Security attributes.
31 manual_reset, initially_signaled,
32 nullptr); // Name.
33 RTC_CHECK(event_handle_);
34 }
35
36 Event::~Event() {
37 CloseHandle(event_handle_);
38 }
39
40 void Event::Set() {
41 SetEvent(event_handle_);
42 }
43
44 void Event::Reset() {
45 ResetEvent(event_handle_);
46 }
47
48 bool Event::Wait(int milliseconds) {
49 DWORD ms = (milliseconds == kForever) ? INFINITE : milliseconds;
50 return (WaitForSingleObject(event_handle_, ms) == WAIT_OBJECT_0);
51 }
52
53 #elif defined(WEBRTC_POSIX)
54
55 Event::Event(bool manual_reset, bool initially_signaled)
56 : is_manual_reset_(manual_reset),
57 event_status_(initially_signaled) {
58 RTC_CHECK(pthread_mutex_init(&event_mutex_, nullptr) == 0);
59 RTC_CHECK(pthread_cond_init(&event_cond_, nullptr) == 0);
60 }
61
62 Event::~Event() {
63 pthread_mutex_destroy(&event_mutex_);
64 pthread_cond_destroy(&event_cond_);
65 }
66
67 void Event::Set() {
68 pthread_mutex_lock(&event_mutex_);
69 event_status_ = true;
70 pthread_cond_broadcast(&event_cond_);
71 pthread_mutex_unlock(&event_mutex_);
72 }
73
74 void Event::Reset() {
75 pthread_mutex_lock(&event_mutex_);
76 event_status_ = false;
77 pthread_mutex_unlock(&event_mutex_);
78 }
79
80 bool Event::Wait(int milliseconds) {
81 int error = 0;
82
83 struct timespec ts;
84 if (milliseconds != kForever) {
85 // Converting from seconds and microseconds (1e-6) plus
86 // milliseconds (1e-3) to seconds and nanoseconds (1e-9).
87
88 #ifdef HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE
89 // Use relative time version, which tends to be more efficient for
90 // pthread implementations where provided (like on Android).
91 ts.tv_sec = milliseconds / 1000;
92 ts.tv_nsec = (milliseconds % 1000) * 1000000;
93 #else
94 struct timeval tv;
95 gettimeofday(&tv, nullptr);
96
97 ts.tv_sec = tv.tv_sec + (milliseconds / 1000);
98 ts.tv_nsec = tv.tv_usec * 1000 + (milliseconds % 1000) * 1000000;
99
100 // Handle overflow.
101 if (ts.tv_nsec >= 1000000000) {
102 ts.tv_sec++;
103 ts.tv_nsec -= 1000000000;
104 }
105 #endif
106 }
107
108 pthread_mutex_lock(&event_mutex_);
109 if (milliseconds != kForever) {
110 while (!event_status_ && error == 0) {
111 #ifdef HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE
112 error = pthread_cond_timedwait_relative_np(
113 &event_cond_, &event_mutex_, &ts);
114 #else
115 error = pthread_cond_timedwait(&event_cond_, &event_mutex_, &ts);
116 #endif
117 }
118 } else {
119 while (!event_status_ && error == 0)
120 error = pthread_cond_wait(&event_cond_, &event_mutex_);
121 }
122
123 // NOTE(liulk): Exactly one thread will auto-reset this event. All
124 // the other threads will think it's unsignaled. This seems to be
125 // consistent with auto-reset events in WEBRTC_WIN
126 if (error == 0 && !is_manual_reset_)
127 event_status_ = false;
128
129 pthread_mutex_unlock(&event_mutex_);
130
131 return (error == 0);
132 }
133
134 #endif
135
136 } // namespace rtc
OLDNEW
« no previous file with comments | « webrtc/base/event.h ('k') | webrtc/base/event_tracer.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698