| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2011 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 23 matching lines...) Expand all Loading... |
| 34 : event_set_(false), | 34 : event_set_(false), |
| 35 timer_thread_(nullptr), | 35 timer_thread_(nullptr), |
| 36 created_at_(), | 36 created_at_(), |
| 37 periodic_(false), | 37 periodic_(false), |
| 38 time_(0), | 38 time_(0), |
| 39 count_(0) { | 39 count_(0) { |
| 40 pthread_mutexattr_t attr; | 40 pthread_mutexattr_t attr; |
| 41 pthread_mutexattr_init(&attr); | 41 pthread_mutexattr_init(&attr); |
| 42 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); | 42 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); |
| 43 pthread_mutex_init(&mutex_, &attr); | 43 pthread_mutex_init(&mutex_, &attr); |
| 44 #ifdef WEBRTC_CLOCK_TYPE_REALTIME | |
| 45 pthread_cond_init(&cond_, 0); | |
| 46 #else | |
| 47 pthread_condattr_t cond_attr; | 44 pthread_condattr_t cond_attr; |
| 48 pthread_condattr_init(&cond_attr); | 45 pthread_condattr_init(&cond_attr); |
| 46 // TODO(sprang): Remove HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC special case once |
| 47 // all supported Android platforms support pthread_condattr_setclock. |
| 48 // TODO(sprang): Add support for monotonic clock on Apple platforms. |
| 49 #if !(defined(WEBRTC_MAC) || defined(WEBRTC_IOS)) && \ |
| 50 !(defined(WEBRTC_ANDROID) && \ |
| 51 defined(HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC)) |
| 49 pthread_condattr_setclock(&cond_attr, CLOCK_MONOTONIC); | 52 pthread_condattr_setclock(&cond_attr, CLOCK_MONOTONIC); |
| 53 #endif |
| 50 pthread_cond_init(&cond_, &cond_attr); | 54 pthread_cond_init(&cond_, &cond_attr); |
| 51 pthread_condattr_destroy(&cond_attr); | 55 pthread_condattr_destroy(&cond_attr); |
| 52 #endif | |
| 53 } | 56 } |
| 54 | 57 |
| 55 EventTimerPosix::~EventTimerPosix() { | 58 EventTimerPosix::~EventTimerPosix() { |
| 56 StopTimer(); | 59 StopTimer(); |
| 57 pthread_cond_destroy(&cond_); | 60 pthread_cond_destroy(&cond_); |
| 58 pthread_mutex_destroy(&mutex_); | 61 pthread_mutex_destroy(&mutex_); |
| 59 } | 62 } |
| 60 | 63 |
| 61 // TODO(pbos): Make this void. | 64 // TODO(pbos): Make this void. |
| 62 bool EventTimerPosix::Set() { | 65 bool EventTimerPosix::Set() { |
| 63 RTC_CHECK_EQ(0, pthread_mutex_lock(&mutex_)); | 66 RTC_CHECK_EQ(0, pthread_mutex_lock(&mutex_)); |
| 64 event_set_ = true; | 67 event_set_ = true; |
| 65 pthread_cond_signal(&cond_); | 68 pthread_cond_signal(&cond_); |
| 66 pthread_mutex_unlock(&mutex_); | 69 pthread_mutex_unlock(&mutex_); |
| 67 return true; | 70 return true; |
| 68 } | 71 } |
| 69 | 72 |
| 70 EventTypeWrapper EventTimerPosix::Wait(unsigned long timeout) { | 73 EventTypeWrapper EventTimerPosix::Wait(unsigned long timeout) { |
| 71 int ret_val = 0; | 74 int ret_val = 0; |
| 72 RTC_CHECK_EQ(0, pthread_mutex_lock(&mutex_)); | 75 RTC_CHECK_EQ(0, pthread_mutex_lock(&mutex_)); |
| 73 | 76 |
| 74 if (!event_set_) { | 77 if (!event_set_) { |
| 75 if (WEBRTC_EVENT_INFINITE != timeout) { | 78 if (WEBRTC_EVENT_INFINITE != timeout) { |
| 76 timespec end_at; | 79 timespec end_at; |
| 77 #ifndef WEBRTC_MAC | 80 #ifndef WEBRTC_MAC |
| 78 #ifdef WEBRTC_CLOCK_TYPE_REALTIME | |
| 79 clock_gettime(CLOCK_REALTIME, &end_at); | |
| 80 #else | |
| 81 clock_gettime(CLOCK_MONOTONIC, &end_at); | 81 clock_gettime(CLOCK_MONOTONIC, &end_at); |
| 82 #endif | |
| 83 #else | 82 #else |
| 84 timeval value; | 83 timeval value; |
| 85 struct timezone time_zone; | 84 struct timezone time_zone; |
| 86 time_zone.tz_minuteswest = 0; | 85 time_zone.tz_minuteswest = 0; |
| 87 time_zone.tz_dsttime = 0; | 86 time_zone.tz_dsttime = 0; |
| 88 gettimeofday(&value, &time_zone); | 87 gettimeofday(&value, &time_zone); |
| 89 TIMEVAL_TO_TIMESPEC(&value, &end_at); | 88 TIMEVAL_TO_TIMESPEC(&value, &end_at); |
| 90 #endif | 89 #endif |
| 91 end_at.tv_sec += timeout / 1000; | 90 end_at.tv_sec += timeout / 1000; |
| 92 end_at.tv_nsec += (timeout - (timeout / 1000) * 1000) * E6; | 91 end_at.tv_nsec += (timeout - (timeout / 1000) * 1000) * E6; |
| 93 | 92 |
| 94 if (end_at.tv_nsec >= E9) { | 93 if (end_at.tv_nsec >= E9) { |
| 95 end_at.tv_sec++; | 94 end_at.tv_sec++; |
| 96 end_at.tv_nsec -= E9; | 95 end_at.tv_nsec -= E9; |
| 97 } | 96 } |
| 98 while (ret_val == 0 && !event_set_) | 97 while (ret_val == 0 && !event_set_) { |
| 98 #if defined(WEBRTC_ANDROID) && defined(HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC) |
| 99 ret_val = pthread_cond_timedwait_monotonic_np(&cond_, &mutex_, &end_at); |
| 100 #else |
| 99 ret_val = pthread_cond_timedwait(&cond_, &mutex_, &end_at); | 101 ret_val = pthread_cond_timedwait(&cond_, &mutex_, &end_at); |
| 102 #endif // WEBRTC_ANDROID && HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC |
| 103 } |
| 100 } else { | 104 } else { |
| 101 while (ret_val == 0 && !event_set_) | 105 while (ret_val == 0 && !event_set_) |
| 102 ret_val = pthread_cond_wait(&cond_, &mutex_); | 106 ret_val = pthread_cond_wait(&cond_, &mutex_); |
| 103 } | 107 } |
| 104 } | 108 } |
| 105 | 109 |
| 106 RTC_DCHECK(ret_val == 0 || ret_val == ETIMEDOUT); | 110 RTC_DCHECK(ret_val == 0 || ret_val == ETIMEDOUT); |
| 107 | 111 |
| 108 // Reset and signal if set, regardless of why the thread woke up. | 112 // Reset and signal if set, regardless of why the thread woke up. |
| 109 if (event_set_) { | 113 if (event_set_) { |
| 110 ret_val = 0; | 114 ret_val = 0; |
| 111 event_set_ = false; | 115 event_set_ = false; |
| 112 } | 116 } |
| 113 pthread_mutex_unlock(&mutex_); | 117 pthread_mutex_unlock(&mutex_); |
| 114 | 118 |
| 115 return ret_val == 0 ? kEventSignaled : kEventTimeout; | 119 return ret_val == 0 ? kEventSignaled : kEventTimeout; |
| 116 } | 120 } |
| 117 | 121 |
| 118 EventTypeWrapper EventTimerPosix::Wait(timespec* end_at) { | 122 EventTypeWrapper EventTimerPosix::Wait(timespec* end_at) { |
| 119 int ret_val = 0; | 123 int ret_val = 0; |
| 120 RTC_CHECK_EQ(0, pthread_mutex_lock(&mutex_)); | 124 RTC_CHECK_EQ(0, pthread_mutex_lock(&mutex_)); |
| 121 | 125 |
| 122 while (ret_val == 0 && !event_set_) | 126 while (ret_val == 0 && !event_set_) { |
| 127 #if defined(WEBRTC_ANDROID) && defined(HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC) |
| 128 ret_val = pthread_cond_timedwait_monotonic_np(&cond_, &mutex_, end_at); |
| 129 #else |
| 123 ret_val = pthread_cond_timedwait(&cond_, &mutex_, end_at); | 130 ret_val = pthread_cond_timedwait(&cond_, &mutex_, end_at); |
| 131 #endif // WEBRTC_ANDROID && HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC |
| 132 } |
| 124 | 133 |
| 125 RTC_DCHECK(ret_val == 0 || ret_val == ETIMEDOUT); | 134 RTC_DCHECK(ret_val == 0 || ret_val == ETIMEDOUT); |
| 126 | 135 |
| 127 // Reset and signal if set, regardless of why the thread woke up. | 136 // Reset and signal if set, regardless of why the thread woke up. |
| 128 if (event_set_) { | 137 if (event_set_) { |
| 129 ret_val = 0; | 138 ret_val = 0; |
| 130 event_set_ = false; | 139 event_set_ = false; |
| 131 } | 140 } |
| 132 pthread_mutex_unlock(&mutex_); | 141 pthread_mutex_unlock(&mutex_); |
| 133 | 142 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 165 } | 174 } |
| 166 | 175 |
| 167 bool EventTimerPosix::Run(void* obj) { | 176 bool EventTimerPosix::Run(void* obj) { |
| 168 return static_cast<EventTimerPosix*>(obj)->Process(); | 177 return static_cast<EventTimerPosix*>(obj)->Process(); |
| 169 } | 178 } |
| 170 | 179 |
| 171 bool EventTimerPosix::Process() { | 180 bool EventTimerPosix::Process() { |
| 172 pthread_mutex_lock(&mutex_); | 181 pthread_mutex_lock(&mutex_); |
| 173 if (created_at_.tv_sec == 0) { | 182 if (created_at_.tv_sec == 0) { |
| 174 #ifndef WEBRTC_MAC | 183 #ifndef WEBRTC_MAC |
| 175 #ifdef WEBRTC_CLOCK_TYPE_REALTIME | |
| 176 clock_gettime(CLOCK_REALTIME, &created_at_); | |
| 177 #else | |
| 178 clock_gettime(CLOCK_MONOTONIC, &created_at_); | 184 clock_gettime(CLOCK_MONOTONIC, &created_at_); |
| 179 #endif | |
| 180 #else | 185 #else |
| 181 timeval value; | 186 timeval value; |
| 182 struct timezone time_zone; | 187 struct timezone time_zone; |
| 183 time_zone.tz_minuteswest = 0; | 188 time_zone.tz_minuteswest = 0; |
| 184 time_zone.tz_dsttime = 0; | 189 time_zone.tz_dsttime = 0; |
| 185 gettimeofday(&value, &time_zone); | 190 gettimeofday(&value, &time_zone); |
| 186 TIMEVAL_TO_TIMESPEC(&value, &created_at_); | 191 TIMEVAL_TO_TIMESPEC(&value, &created_at_); |
| 187 #endif | 192 #endif |
| 188 count_ = 0; | 193 count_ = 0; |
| 189 } | 194 } |
| (...skipping 30 matching lines...) Expand all Loading... |
| 220 } | 225 } |
| 221 timer_event_.reset(); | 226 timer_event_.reset(); |
| 222 | 227 |
| 223 // Set time to zero to force new reference time for the timer. | 228 // Set time to zero to force new reference time for the timer. |
| 224 memset(&created_at_, 0, sizeof(created_at_)); | 229 memset(&created_at_, 0, sizeof(created_at_)); |
| 225 count_ = 0; | 230 count_ = 0; |
| 226 return true; | 231 return true; |
| 227 } | 232 } |
| 228 | 233 |
| 229 } // namespace webrtc | 234 } // namespace webrtc |
| OLD | NEW |