niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2011 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 | |
pbos@webrtc.org | acaf3a1 | 2013-05-27 15:07:45 +0000 | [diff] [blame] | 11 | #include "webrtc/system_wrappers/source/condition_variable_posix.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 12 | |
phoglund@webrtc.org | a36d75a | 2012-11-14 09:55:04 +0000 | [diff] [blame] | 13 | #include <errno.h> |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 14 | #if defined(WEBRTC_LINUX) |
| 15 | #include <ctime> |
| 16 | #else |
| 17 | #include <sys/time.h> |
| 18 | #endif |
| 19 | |
pbos@webrtc.org | acaf3a1 | 2013-05-27 15:07:45 +0000 | [diff] [blame] | 20 | #include "webrtc/system_wrappers/source/critical_section_posix.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 21 | |
| 22 | namespace webrtc { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 23 | |
phoglund@webrtc.org | a36d75a | 2012-11-14 09:55:04 +0000 | [diff] [blame] | 24 | ConditionVariableWrapper* ConditionVariablePosix::Create() { |
| 25 | ConditionVariablePosix* ptr = new ConditionVariablePosix; |
| 26 | if (!ptr) { |
| 27 | return NULL; |
| 28 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 29 | |
phoglund@webrtc.org | a36d75a | 2012-11-14 09:55:04 +0000 | [diff] [blame] | 30 | const int error = ptr->Construct(); |
| 31 | if (error) { |
| 32 | delete ptr; |
| 33 | return NULL; |
| 34 | } |
| 35 | |
| 36 | return ptr; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 37 | } |
| 38 | |
phoglund@webrtc.org | a36d75a | 2012-11-14 09:55:04 +0000 | [diff] [blame] | 39 | ConditionVariablePosix::ConditionVariablePosix() { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 40 | } |
| 41 | |
phoglund@webrtc.org | a36d75a | 2012-11-14 09:55:04 +0000 | [diff] [blame] | 42 | int ConditionVariablePosix::Construct() { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 43 | #ifdef WEBRTC_CLOCK_TYPE_REALTIME |
phoglund@webrtc.org | a36d75a | 2012-11-14 09:55:04 +0000 | [diff] [blame] | 44 | pthread_cond_init(&cond_, NULL); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 45 | #else |
phoglund@webrtc.org | a36d75a | 2012-11-14 09:55:04 +0000 | [diff] [blame] | 46 | int result = 0; |
| 47 | pthread_condattr_t cond_attr; |
| 48 | result = pthread_condattr_init(&cond_attr); |
| 49 | if (result != 0) { |
| 50 | return -1; |
| 51 | } |
| 52 | result = pthread_condattr_setclock(&cond_attr, CLOCK_MONOTONIC); |
| 53 | if (result != 0) { |
| 54 | return -1; |
| 55 | } |
| 56 | result = pthread_cond_init(&cond_, &cond_attr); |
| 57 | if (result != 0) { |
| 58 | return -1; |
| 59 | } |
| 60 | result = pthread_condattr_destroy(&cond_attr); |
| 61 | if (result != 0) { |
| 62 | return -1; |
| 63 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 64 | #endif |
phoglund@webrtc.org | a36d75a | 2012-11-14 09:55:04 +0000 | [diff] [blame] | 65 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 66 | } |
| 67 | |
phoglund@webrtc.org | a36d75a | 2012-11-14 09:55:04 +0000 | [diff] [blame] | 68 | ConditionVariablePosix::~ConditionVariablePosix() { |
| 69 | pthread_cond_destroy(&cond_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 70 | } |
| 71 | |
phoglund@webrtc.org | a36d75a | 2012-11-14 09:55:04 +0000 | [diff] [blame] | 72 | void ConditionVariablePosix::SleepCS(CriticalSectionWrapper& crit_sect) { |
| 73 | CriticalSectionPosix* cs = reinterpret_cast<CriticalSectionPosix*>( |
| 74 | &crit_sect); |
phoglund@webrtc.org | 99f7c91 | 2012-11-30 10:44:49 +0000 | [diff] [blame] | 75 | pthread_cond_wait(&cond_, &cs->mutex_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 76 | } |
| 77 | |
phoglund@webrtc.org | a36d75a | 2012-11-14 09:55:04 +0000 | [diff] [blame] | 78 | bool ConditionVariablePosix::SleepCS(CriticalSectionWrapper& crit_sect, |
| 79 | unsigned long max_time_inMS) { |
| 80 | const unsigned long INFINITE = 0xFFFFFFFF; |
| 81 | const int MILLISECONDS_PER_SECOND = 1000; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 82 | #ifndef WEBRTC_LINUX |
phoglund@webrtc.org | a36d75a | 2012-11-14 09:55:04 +0000 | [diff] [blame] | 83 | const int MICROSECONDS_PER_MILLISECOND = 1000; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 84 | #endif |
phoglund@webrtc.org | a36d75a | 2012-11-14 09:55:04 +0000 | [diff] [blame] | 85 | const int NANOSECONDS_PER_SECOND = 1000000000; |
| 86 | const int NANOSECONDS_PER_MILLISECOND = 1000000; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 87 | |
phoglund@webrtc.org | a36d75a | 2012-11-14 09:55:04 +0000 | [diff] [blame] | 88 | CriticalSectionPosix* cs = reinterpret_cast<CriticalSectionPosix*>( |
| 89 | &crit_sect); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 90 | |
phoglund@webrtc.org | a36d75a | 2012-11-14 09:55:04 +0000 | [diff] [blame] | 91 | if (max_time_inMS != INFINITE) { |
| 92 | timespec ts; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 93 | #ifndef WEBRTC_MAC |
| 94 | #ifdef WEBRTC_CLOCK_TYPE_REALTIME |
phoglund@webrtc.org | a36d75a | 2012-11-14 09:55:04 +0000 | [diff] [blame] | 95 | clock_gettime(CLOCK_REALTIME, &ts); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 96 | #else |
phoglund@webrtc.org | a36d75a | 2012-11-14 09:55:04 +0000 | [diff] [blame] | 97 | clock_gettime(CLOCK_MONOTONIC, &ts); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 98 | #endif |
phoglund@webrtc.org | a36d75a | 2012-11-14 09:55:04 +0000 | [diff] [blame] | 99 | #else // WEBRTC_MAC |
| 100 | struct timeval tv; |
| 101 | gettimeofday(&tv, 0); |
| 102 | ts.tv_sec = tv.tv_sec; |
| 103 | ts.tv_nsec = tv.tv_usec * MICROSECONDS_PER_MILLISECOND; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 104 | #endif |
| 105 | |
phoglund@webrtc.org | a36d75a | 2012-11-14 09:55:04 +0000 | [diff] [blame] | 106 | ts.tv_sec += max_time_inMS / MILLISECONDS_PER_SECOND; |
| 107 | ts.tv_nsec += |
| 108 | (max_time_inMS |
| 109 | - ((max_time_inMS / MILLISECONDS_PER_SECOND) * MILLISECONDS_PER_SECOND)) |
| 110 | * NANOSECONDS_PER_MILLISECOND; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 111 | |
phoglund@webrtc.org | a36d75a | 2012-11-14 09:55:04 +0000 | [diff] [blame] | 112 | if (ts.tv_nsec >= NANOSECONDS_PER_SECOND) { |
| 113 | ts.tv_sec += ts.tv_nsec / NANOSECONDS_PER_SECOND; |
| 114 | ts.tv_nsec %= NANOSECONDS_PER_SECOND; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 115 | } |
phoglund@webrtc.org | 99f7c91 | 2012-11-30 10:44:49 +0000 | [diff] [blame] | 116 | const int res = pthread_cond_timedwait(&cond_, &cs->mutex_, &ts); |
phoglund@webrtc.org | a36d75a | 2012-11-14 09:55:04 +0000 | [diff] [blame] | 117 | return (res == ETIMEDOUT) ? false : true; |
| 118 | } else { |
phoglund@webrtc.org | 99f7c91 | 2012-11-30 10:44:49 +0000 | [diff] [blame] | 119 | pthread_cond_wait(&cond_, &cs->mutex_); |
phoglund@webrtc.org | a36d75a | 2012-11-14 09:55:04 +0000 | [diff] [blame] | 120 | return true; |
| 121 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 122 | } |
| 123 | |
phoglund@webrtc.org | a36d75a | 2012-11-14 09:55:04 +0000 | [diff] [blame] | 124 | void ConditionVariablePosix::Wake() { |
| 125 | pthread_cond_signal(&cond_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 126 | } |
| 127 | |
phoglund@webrtc.org | a36d75a | 2012-11-14 09:55:04 +0000 | [diff] [blame] | 128 | void ConditionVariablePosix::WakeAll() { |
| 129 | pthread_cond_broadcast(&cond_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 130 | } |
phoglund@webrtc.org | a36d75a | 2012-11-14 09:55:04 +0000 | [diff] [blame] | 131 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 132 | } // namespace webrtc |