OLD | NEW |
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 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 } | 79 } |
80 | 80 |
81 bool Event::Wait(int milliseconds) { | 81 bool Event::Wait(int milliseconds) { |
82 int error = 0; | 82 int error = 0; |
83 | 83 |
84 struct timespec ts; | 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 #if HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE | 89 #ifdef HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE |
90 // Use relative time version, which tends to be more efficient for | 90 // Use relative time version, which tends to be more efficient for |
91 // pthread implementations where provided (like on Android). | 91 // pthread implementations where provided (like on Android). |
92 ts.tv_sec = milliseconds / 1000; | 92 ts.tv_sec = milliseconds / 1000; |
93 ts.tv_nsec = (milliseconds % 1000) * 1000000; | 93 ts.tv_nsec = (milliseconds % 1000) * 1000000; |
94 #else | 94 #else |
95 struct timeval tv; | 95 struct timeval tv; |
96 gettimeofday(&tv, NULL); | 96 gettimeofday(&tv, NULL); |
97 | 97 |
98 ts.tv_sec = tv.tv_sec + (milliseconds / 1000); | 98 ts.tv_sec = tv.tv_sec + (milliseconds / 1000); |
99 ts.tv_nsec = tv.tv_usec * 1000 + (milliseconds % 1000) * 1000000; | 99 ts.tv_nsec = tv.tv_usec * 1000 + (milliseconds % 1000) * 1000000; |
100 | 100 |
101 // Handle overflow. | 101 // Handle overflow. |
102 if (ts.tv_nsec >= 1000000000) { | 102 if (ts.tv_nsec >= 1000000000) { |
103 ts.tv_sec++; | 103 ts.tv_sec++; |
104 ts.tv_nsec -= 1000000000; | 104 ts.tv_nsec -= 1000000000; |
105 } | 105 } |
106 #endif | 106 #endif |
107 } | 107 } |
108 | 108 |
109 pthread_mutex_lock(&event_mutex_); | 109 pthread_mutex_lock(&event_mutex_); |
110 if (milliseconds != kForever) { | 110 if (milliseconds != kForever) { |
111 while (!event_status_ && error == 0) { | 111 while (!event_status_ && error == 0) { |
112 #if HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE | 112 #ifdef HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE |
113 error = pthread_cond_timedwait_relative_np( | 113 error = pthread_cond_timedwait_relative_np( |
114 &event_cond_, &event_mutex_, &ts); | 114 &event_cond_, &event_mutex_, &ts); |
115 #else | 115 #else |
116 error = pthread_cond_timedwait(&event_cond_, &event_mutex_, &ts); | 116 error = pthread_cond_timedwait(&event_cond_, &event_mutex_, &ts); |
117 #endif | 117 #endif |
118 } | 118 } |
119 } else { | 119 } else { |
120 while (!event_status_ && error == 0) | 120 while (!event_status_ && error == 0) |
121 error = pthread_cond_wait(&event_cond_, &event_mutex_); | 121 error = pthread_cond_wait(&event_cond_, &event_mutex_); |
122 } | 122 } |
123 | 123 |
124 // NOTE(liulk): Exactly one thread will auto-reset this event. All | 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 | 125 // the other threads will think it's unsignaled. This seems to be |
126 // consistent with auto-reset events in WEBRTC_WIN | 126 // consistent with auto-reset events in WEBRTC_WIN |
127 if (error == 0 && !is_manual_reset_) | 127 if (error == 0 && !is_manual_reset_) |
128 event_status_ = false; | 128 event_status_ = false; |
129 | 129 |
130 pthread_mutex_unlock(&event_mutex_); | 130 pthread_mutex_unlock(&event_mutex_); |
131 | 131 |
132 return (error == 0); | 132 return (error == 0); |
133 } | 133 } |
134 | 134 |
135 #endif | 135 #endif |
136 | 136 |
137 } // namespace rtc | 137 } // namespace rtc |
OLD | NEW |