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

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

Issue 1716563003: Reduce the scope of rtc::Event::Wait() locking. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Created 4 years, 10 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 | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 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 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 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
72 pthread_mutex_unlock(&event_mutex_); 72 pthread_mutex_unlock(&event_mutex_);
73 } 73 }
74 74
75 void Event::Reset() { 75 void Event::Reset() {
76 pthread_mutex_lock(&event_mutex_); 76 pthread_mutex_lock(&event_mutex_);
77 event_status_ = false; 77 event_status_ = false;
78 pthread_mutex_unlock(&event_mutex_); 78 pthread_mutex_unlock(&event_mutex_);
79 } 79 }
80 80
81 bool Event::Wait(int milliseconds) { 81 bool Event::Wait(int milliseconds) {
82 pthread_mutex_lock(&event_mutex_);
83 int error = 0; 82 int error = 0;
84 83
84 struct timespec ts;
85 if (milliseconds != kForever) { 85 if (milliseconds != kForever) {
86 // Converting from seconds and microseconds (1e-6) plus 86 // Converting from seconds and microseconds (1e-6) plus
87 // milliseconds (1e-3) to seconds and nanoseconds (1e-9). 87 // milliseconds (1e-3) to seconds and nanoseconds (1e-9).
88 88
89 struct timespec ts;
90 #if HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE 89 #if HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE
91 // Use relative time version, which tends to be more efficient for 90 // Use relative time version, which tends to be more efficient for
92 // pthread implementations where provided (like on Android). 91 // pthread implementations where provided (like on Android).
93 ts.tv_sec = milliseconds / 1000; 92 ts.tv_sec = milliseconds / 1000;
94 ts.tv_nsec = (milliseconds % 1000) * 1000000; 93 ts.tv_nsec = (milliseconds % 1000) * 1000000;
95 #else 94 #else
96 struct timeval tv; 95 struct timeval tv;
97 gettimeofday(&tv, NULL); 96 gettimeofday(&tv, NULL);
98 97
99 ts.tv_sec = tv.tv_sec + (milliseconds / 1000); 98 ts.tv_sec = tv.tv_sec + (milliseconds / 1000);
100 ts.tv_nsec = tv.tv_usec * 1000 + (milliseconds % 1000) * 1000000; 99 ts.tv_nsec = tv.tv_usec * 1000 + (milliseconds % 1000) * 1000000;
101 100
102 // Handle overflow. 101 // Handle overflow.
103 if (ts.tv_nsec >= 1000000000) { 102 if (ts.tv_nsec >= 1000000000) {
104 ts.tv_sec++; 103 ts.tv_sec++;
105 ts.tv_nsec -= 1000000000; 104 ts.tv_nsec -= 1000000000;
106 } 105 }
107 #endif 106 #endif
107 }
108 108
109 pthread_mutex_lock(&event_mutex_);
tommi 2016/02/19 16:40:42 There's a slight change in behavior because of thi
110 if (milliseconds != kForever) {
109 while (!event_status_ && error == 0) { 111 while (!event_status_ && error == 0) {
110 #if HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE 112 #if HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE
111 error = pthread_cond_timedwait_relative_np( 113 error = pthread_cond_timedwait_relative_np(
112 &event_cond_, &event_mutex_, &ts); 114 &event_cond_, &event_mutex_, &ts);
113 #else 115 #else
114 error = pthread_cond_timedwait(&event_cond_, &event_mutex_, &ts); 116 error = pthread_cond_timedwait(&event_cond_, &event_mutex_, &ts);
115 #endif 117 #endif
116 } 118 }
117 } else { 119 } else {
118 while (!event_status_ && error == 0) 120 while (!event_status_ && error == 0)
119 error = pthread_cond_wait(&event_cond_, &event_mutex_); 121 error = pthread_cond_wait(&event_cond_, &event_mutex_);
120 } 122 }
121 123
122 // NOTE(liulk): Exactly one thread will auto-reset this event. All 124 // NOTE(liulk): Exactly one thread will auto-reset this event. All
123 // the other threads will think it's unsignaled. This seems to be 125 // the other threads will think it's unsignaled. This seems to be
124 // consistent with auto-reset events in WEBRTC_WIN 126 // consistent with auto-reset events in WEBRTC_WIN
125 if (error == 0 && !is_manual_reset_) 127 if (error == 0 && !is_manual_reset_)
126 event_status_ = false; 128 event_status_ = false;
127 129
128 pthread_mutex_unlock(&event_mutex_); 130 pthread_mutex_unlock(&event_mutex_);
129 131
130 return (error == 0); 132 return (error == 0);
131 } 133 }
132 134
133 #endif 135 #endif
134 136
135 } // namespace rtc 137 } // namespace rtc
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698