| 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 |
| 11 #include "webrtc/system_wrappers/source/condition_variable_posix.h" | 11 #include "webrtc/system_wrappers/source/condition_variable_posix.h" |
| 12 | 12 |
| 13 #include "webrtc/system_wrappers/include/critical_section_wrapper.h" |
| 14 |
| 13 #include <errno.h> | 15 #include <errno.h> |
| 14 #if defined(WEBRTC_LINUX) | 16 #if defined(WEBRTC_LINUX) |
| 15 #include <time.h> | 17 #include <time.h> |
| 16 #else | 18 #else |
| 17 #include <sys/time.h> | 19 #include <sys/time.h> |
| 18 #endif | 20 #endif |
| 19 | 21 |
| 20 #include "webrtc/system_wrappers/source/critical_section_posix.h" | |
| 21 | |
| 22 namespace webrtc { | 22 namespace webrtc { |
| 23 | 23 |
| 24 ConditionVariableWrapper* ConditionVariablePosix::Create() { | 24 ConditionVariableWrapper* ConditionVariablePosix::Create() { |
| 25 ConditionVariablePosix* ptr = new ConditionVariablePosix; | 25 ConditionVariablePosix* ptr = new ConditionVariablePosix; |
| 26 if (!ptr) { | 26 if (!ptr) { |
| 27 return NULL; | 27 return NULL; |
| 28 } | 28 } |
| 29 | 29 |
| 30 const int error = ptr->Construct(); | 30 const int error = ptr->Construct(); |
| 31 if (error) { | 31 if (error) { |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 63 } | 63 } |
| 64 #endif | 64 #endif |
| 65 return 0; | 65 return 0; |
| 66 } | 66 } |
| 67 | 67 |
| 68 ConditionVariablePosix::~ConditionVariablePosix() { | 68 ConditionVariablePosix::~ConditionVariablePosix() { |
| 69 pthread_cond_destroy(&cond_); | 69 pthread_cond_destroy(&cond_); |
| 70 } | 70 } |
| 71 | 71 |
| 72 void ConditionVariablePosix::SleepCS(CriticalSectionWrapper& crit_sect) { | 72 void ConditionVariablePosix::SleepCS(CriticalSectionWrapper& crit_sect) { |
| 73 CriticalSectionPosix* cs = reinterpret_cast<CriticalSectionPosix*>( | 73 pthread_cond_wait(&cond_, &crit_sect.mutex_); |
| 74 &crit_sect); | |
| 75 pthread_cond_wait(&cond_, &cs->mutex_); | |
| 76 } | 74 } |
| 77 | 75 |
| 78 bool ConditionVariablePosix::SleepCS(CriticalSectionWrapper& crit_sect, | 76 bool ConditionVariablePosix::SleepCS(CriticalSectionWrapper& crit_sect, |
| 79 unsigned long max_time_inMS) { | 77 unsigned long max_time_inMS) { |
| 80 const unsigned long INFINITE = 0xFFFFFFFF; | 78 const unsigned long INFINITE = 0xFFFFFFFF; |
| 81 const int MILLISECONDS_PER_SECOND = 1000; | 79 const int MILLISECONDS_PER_SECOND = 1000; |
| 82 #ifndef WEBRTC_LINUX | 80 #ifndef WEBRTC_LINUX |
| 83 const int MICROSECONDS_PER_MILLISECOND = 1000; | 81 const int MICROSECONDS_PER_MILLISECOND = 1000; |
| 84 #endif | 82 #endif |
| 85 const int NANOSECONDS_PER_SECOND = 1000000000; | 83 const int NANOSECONDS_PER_SECOND = 1000000000; |
| 86 const int NANOSECONDS_PER_MILLISECOND = 1000000; | 84 const int NANOSECONDS_PER_MILLISECOND = 1000000; |
| 87 | 85 |
| 88 CriticalSectionPosix* cs = reinterpret_cast<CriticalSectionPosix*>( | |
| 89 &crit_sect); | |
| 90 | |
| 91 if (max_time_inMS != INFINITE) { | 86 if (max_time_inMS != INFINITE) { |
| 92 timespec ts; | 87 timespec ts; |
| 93 #ifndef WEBRTC_MAC | 88 #ifndef WEBRTC_MAC |
| 94 #ifdef WEBRTC_CLOCK_TYPE_REALTIME | 89 #ifdef WEBRTC_CLOCK_TYPE_REALTIME |
| 95 clock_gettime(CLOCK_REALTIME, &ts); | 90 clock_gettime(CLOCK_REALTIME, &ts); |
| 96 #else | 91 #else |
| 97 clock_gettime(CLOCK_MONOTONIC, &ts); | 92 clock_gettime(CLOCK_MONOTONIC, &ts); |
| 98 #endif | 93 #endif |
| 99 #else // WEBRTC_MAC | 94 #else // WEBRTC_MAC |
| 100 struct timeval tv; | 95 struct timeval tv; |
| 101 gettimeofday(&tv, 0); | 96 gettimeofday(&tv, 0); |
| 102 ts.tv_sec = tv.tv_sec; | 97 ts.tv_sec = tv.tv_sec; |
| 103 ts.tv_nsec = tv.tv_usec * MICROSECONDS_PER_MILLISECOND; | 98 ts.tv_nsec = tv.tv_usec * MICROSECONDS_PER_MILLISECOND; |
| 104 #endif | 99 #endif |
| 105 | 100 |
| 106 ts.tv_sec += max_time_inMS / MILLISECONDS_PER_SECOND; | 101 ts.tv_sec += max_time_inMS / MILLISECONDS_PER_SECOND; |
| 107 ts.tv_nsec += | 102 ts.tv_nsec += |
| 108 (max_time_inMS | 103 (max_time_inMS |
| 109 - ((max_time_inMS / MILLISECONDS_PER_SECOND) * MILLISECONDS_PER_SECOND)) | 104 - ((max_time_inMS / MILLISECONDS_PER_SECOND) * MILLISECONDS_PER_SECOND)) |
| 110 * NANOSECONDS_PER_MILLISECOND; | 105 * NANOSECONDS_PER_MILLISECOND; |
| 111 | 106 |
| 112 if (ts.tv_nsec >= NANOSECONDS_PER_SECOND) { | 107 if (ts.tv_nsec >= NANOSECONDS_PER_SECOND) { |
| 113 ts.tv_sec += ts.tv_nsec / NANOSECONDS_PER_SECOND; | 108 ts.tv_sec += ts.tv_nsec / NANOSECONDS_PER_SECOND; |
| 114 ts.tv_nsec %= NANOSECONDS_PER_SECOND; | 109 ts.tv_nsec %= NANOSECONDS_PER_SECOND; |
| 115 } | 110 } |
| 116 const int res = pthread_cond_timedwait(&cond_, &cs->mutex_, &ts); | 111 const int res = pthread_cond_timedwait(&cond_, &crit_sect.mutex_, &ts); |
| 117 return (res == ETIMEDOUT) ? false : true; | 112 return (res == ETIMEDOUT) ? false : true; |
| 118 } else { | 113 } else { |
| 119 pthread_cond_wait(&cond_, &cs->mutex_); | 114 pthread_cond_wait(&cond_, &crit_sect.mutex_); |
| 120 return true; | 115 return true; |
| 121 } | 116 } |
| 122 } | 117 } |
| 123 | 118 |
| 124 void ConditionVariablePosix::Wake() { | 119 void ConditionVariablePosix::Wake() { |
| 125 pthread_cond_signal(&cond_); | 120 pthread_cond_signal(&cond_); |
| 126 } | 121 } |
| 127 | 122 |
| 128 void ConditionVariablePosix::WakeAll() { | 123 void ConditionVariablePosix::WakeAll() { |
| 129 pthread_cond_broadcast(&cond_); | 124 pthread_cond_broadcast(&cond_); |
| 130 } | 125 } |
| 131 | 126 |
| 132 } // namespace webrtc | 127 } // namespace webrtc |
| OLD | NEW |