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 | |
Peter Boström | 64c0366 | 2015-04-08 11:24:19 +0200 | [diff] [blame] | 11 | #include "webrtc/system_wrappers/source/event_timer_posix.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 12 | |
| 13 | #include <errno.h> |
| 14 | #include <pthread.h> |
| 15 | #include <signal.h> |
| 16 | #include <stdio.h> |
| 17 | #include <string.h> |
| 18 | #include <sys/time.h> |
| 19 | #include <unistd.h> |
| 20 | |
tommi@webrtc.org | f7e6cfd | 2015-02-09 18:25:38 +0000 | [diff] [blame] | 21 | #include "webrtc/base/checks.h" |
| 22 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 23 | namespace webrtc { |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 24 | |
Peter Boström | 64c0366 | 2015-04-08 11:24:19 +0200 | [diff] [blame] | 25 | // static |
| 26 | EventTimerWrapper* EventTimerWrapper::Create() { |
| 27 | return new EventTimerPosix(); |
| 28 | } |
| 29 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 30 | const long int E6 = 1000000; |
| 31 | const long int E9 = 1000 * E6; |
| 32 | |
Peter Boström | 64c0366 | 2015-04-08 11:24:19 +0200 | [diff] [blame] | 33 | EventTimerPosix::EventTimerPosix() |
pbos@webrtc.org | a846371 | 2015-03-17 13:11:15 +0000 | [diff] [blame] | 34 | : event_set_(false), |
| 35 | timer_thread_(nullptr), |
pbos@webrtc.org | a846371 | 2015-03-17 13:11:15 +0000 | [diff] [blame] | 36 | created_at_(), |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 37 | periodic_(false), |
| 38 | time_(0), |
pbos@webrtc.org | a846371 | 2015-03-17 13:11:15 +0000 | [diff] [blame] | 39 | count_(0) { |
pbos@webrtc.org | e6dc38e | 2013-08-20 09:49:19 +0000 | [diff] [blame] | 40 | pthread_mutexattr_t attr; |
| 41 | pthread_mutexattr_init(&attr); |
| 42 | pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); |
pbos@webrtc.org | a846371 | 2015-03-17 13:11:15 +0000 | [diff] [blame] | 43 | pthread_mutex_init(&mutex_, &attr); |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 44 | pthread_condattr_t cond_attr; |
pbos@webrtc.org | a846371 | 2015-03-17 13:11:15 +0000 | [diff] [blame] | 45 | pthread_condattr_init(&cond_attr); |
sprang | e791ffd | 2016-01-26 01:53:20 -0800 | [diff] [blame^] | 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)) |
pbos@webrtc.org | a846371 | 2015-03-17 13:11:15 +0000 | [diff] [blame] | 52 | pthread_condattr_setclock(&cond_attr, CLOCK_MONOTONIC); |
sprang | e791ffd | 2016-01-26 01:53:20 -0800 | [diff] [blame^] | 53 | #endif |
pbos@webrtc.org | a846371 | 2015-03-17 13:11:15 +0000 | [diff] [blame] | 54 | pthread_cond_init(&cond_, &cond_attr); |
| 55 | pthread_condattr_destroy(&cond_attr); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Peter Boström | 64c0366 | 2015-04-08 11:24:19 +0200 | [diff] [blame] | 58 | EventTimerPosix::~EventTimerPosix() { |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 59 | StopTimer(); |
| 60 | pthread_cond_destroy(&cond_); |
| 61 | pthread_mutex_destroy(&mutex_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 62 | } |
| 63 | |
pbos@webrtc.org | a846371 | 2015-03-17 13:11:15 +0000 | [diff] [blame] | 64 | // TODO(pbos): Make this void. |
Peter Boström | 64c0366 | 2015-04-08 11:24:19 +0200 | [diff] [blame] | 65 | bool EventTimerPosix::Set() { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 66 | RTC_CHECK_EQ(0, pthread_mutex_lock(&mutex_)); |
pbos@webrtc.org | a846371 | 2015-03-17 13:11:15 +0000 | [diff] [blame] | 67 | event_set_ = true; |
| 68 | pthread_cond_signal(&cond_); |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 69 | pthread_mutex_unlock(&mutex_); |
| 70 | return true; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Peter Boström | 64c0366 | 2015-04-08 11:24:19 +0200 | [diff] [blame] | 73 | EventTypeWrapper EventTimerPosix::Wait(unsigned long timeout) { |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 74 | int ret_val = 0; |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 75 | RTC_CHECK_EQ(0, pthread_mutex_lock(&mutex_)); |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 76 | |
pbos@webrtc.org | a846371 | 2015-03-17 13:11:15 +0000 | [diff] [blame] | 77 | if (!event_set_) { |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 78 | if (WEBRTC_EVENT_INFINITE != timeout) { |
| 79 | timespec end_at; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 80 | #ifndef WEBRTC_MAC |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 81 | clock_gettime(CLOCK_MONOTONIC, &end_at); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 82 | #else |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 83 | timeval value; |
| 84 | struct timezone time_zone; |
| 85 | time_zone.tz_minuteswest = 0; |
| 86 | time_zone.tz_dsttime = 0; |
| 87 | gettimeofday(&value, &time_zone); |
| 88 | TIMEVAL_TO_TIMESPEC(&value, &end_at); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 89 | #endif |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 90 | end_at.tv_sec += timeout / 1000; |
| 91 | end_at.tv_nsec += (timeout - (timeout / 1000) * 1000) * E6; |
| 92 | |
| 93 | if (end_at.tv_nsec >= E9) { |
| 94 | end_at.tv_sec++; |
| 95 | end_at.tv_nsec -= E9; |
| 96 | } |
sprang | e791ffd | 2016-01-26 01:53:20 -0800 | [diff] [blame^] | 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 |
pbos@webrtc.org | a846371 | 2015-03-17 13:11:15 +0000 | [diff] [blame] | 101 | ret_val = pthread_cond_timedwait(&cond_, &mutex_, &end_at); |
sprang | e791ffd | 2016-01-26 01:53:20 -0800 | [diff] [blame^] | 102 | #endif // WEBRTC_ANDROID && HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC |
| 103 | } |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 104 | } else { |
pbos@webrtc.org | a846371 | 2015-03-17 13:11:15 +0000 | [diff] [blame] | 105 | while (ret_val == 0 && !event_set_) |
| 106 | ret_val = pthread_cond_wait(&cond_, &mutex_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 107 | } |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 108 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 109 | |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 110 | RTC_DCHECK(ret_val == 0 || ret_val == ETIMEDOUT); |
tommi@webrtc.org | f7e6cfd | 2015-02-09 18:25:38 +0000 | [diff] [blame] | 111 | |
pbos@webrtc.org | a846371 | 2015-03-17 13:11:15 +0000 | [diff] [blame] | 112 | // Reset and signal if set, regardless of why the thread woke up. |
| 113 | if (event_set_) { |
| 114 | ret_val = 0; |
| 115 | event_set_ = false; |
| 116 | } |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 117 | pthread_mutex_unlock(&mutex_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 118 | |
pbos@webrtc.org | a846371 | 2015-03-17 13:11:15 +0000 | [diff] [blame] | 119 | return ret_val == 0 ? kEventSignaled : kEventTimeout; |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Peter Boström | 64c0366 | 2015-04-08 11:24:19 +0200 | [diff] [blame] | 122 | EventTypeWrapper EventTimerPosix::Wait(timespec* end_at) { |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 123 | int ret_val = 0; |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 124 | RTC_CHECK_EQ(0, pthread_mutex_lock(&mutex_)); |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 125 | |
sprang | e791ffd | 2016-01-26 01:53:20 -0800 | [diff] [blame^] | 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 |
pbos@webrtc.org | a846371 | 2015-03-17 13:11:15 +0000 | [diff] [blame] | 130 | ret_val = pthread_cond_timedwait(&cond_, &mutex_, end_at); |
sprang | e791ffd | 2016-01-26 01:53:20 -0800 | [diff] [blame^] | 131 | #endif // WEBRTC_ANDROID && HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC |
| 132 | } |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 133 | |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 134 | RTC_DCHECK(ret_val == 0 || ret_val == ETIMEDOUT); |
pbos@webrtc.org | a846371 | 2015-03-17 13:11:15 +0000 | [diff] [blame] | 135 | |
| 136 | // Reset and signal if set, regardless of why the thread woke up. |
| 137 | if (event_set_) { |
| 138 | ret_val = 0; |
| 139 | event_set_ = false; |
| 140 | } |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 141 | pthread_mutex_unlock(&mutex_); |
| 142 | |
pbos@webrtc.org | a846371 | 2015-03-17 13:11:15 +0000 | [diff] [blame] | 143 | return ret_val == 0 ? kEventSignaled : kEventTimeout; |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Peter Boström | 64c0366 | 2015-04-08 11:24:19 +0200 | [diff] [blame] | 146 | bool EventTimerPosix::StartTimer(bool periodic, unsigned long time) { |
pbos@webrtc.org | e6dc38e | 2013-08-20 09:49:19 +0000 | [diff] [blame] | 147 | pthread_mutex_lock(&mutex_); |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 148 | if (timer_thread_) { |
| 149 | if (periodic_) { |
| 150 | // Timer already started. |
pbos@webrtc.org | e6dc38e | 2013-08-20 09:49:19 +0000 | [diff] [blame] | 151 | pthread_mutex_unlock(&mutex_); |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 152 | return false; |
| 153 | } else { |
| 154 | // New one shot timer |
| 155 | time_ = time; |
| 156 | created_at_.tv_sec = 0; |
| 157 | timer_event_->Set(); |
pbos@webrtc.org | e6dc38e | 2013-08-20 09:49:19 +0000 | [diff] [blame] | 158 | pthread_mutex_unlock(&mutex_); |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 159 | return true; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 160 | } |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 161 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 162 | |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 163 | // Start the timer thread |
Peter Boström | 64c0366 | 2015-04-08 11:24:19 +0200 | [diff] [blame] | 164 | timer_event_.reset(new EventTimerPosix()); |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 165 | const char* thread_name = "WebRtc_event_timer_thread"; |
Peter Boström | 8c38e8b | 2015-11-26 17:45:47 +0100 | [diff] [blame] | 166 | timer_thread_.reset(new rtc::PlatformThread(Run, this, thread_name)); |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 167 | periodic_ = periodic; |
| 168 | time_ = time; |
Peter Boström | 8c38e8b | 2015-11-26 17:45:47 +0100 | [diff] [blame] | 169 | timer_thread_->Start(); |
| 170 | timer_thread_->SetPriority(rtc::kRealtimePriority); |
pbos@webrtc.org | e6dc38e | 2013-08-20 09:49:19 +0000 | [diff] [blame] | 171 | pthread_mutex_unlock(&mutex_); |
| 172 | |
Peter Boström | 8c38e8b | 2015-11-26 17:45:47 +0100 | [diff] [blame] | 173 | return true; |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Peter Boström | 64c0366 | 2015-04-08 11:24:19 +0200 | [diff] [blame] | 176 | bool EventTimerPosix::Run(void* obj) { |
| 177 | return static_cast<EventTimerPosix*>(obj)->Process(); |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Peter Boström | 64c0366 | 2015-04-08 11:24:19 +0200 | [diff] [blame] | 180 | bool EventTimerPosix::Process() { |
pbos@webrtc.org | e6dc38e | 2013-08-20 09:49:19 +0000 | [diff] [blame] | 181 | pthread_mutex_lock(&mutex_); |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 182 | if (created_at_.tv_sec == 0) { |
| 183 | #ifndef WEBRTC_MAC |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 184 | clock_gettime(CLOCK_MONOTONIC, &created_at_); |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 185 | #else |
| 186 | timeval value; |
| 187 | struct timezone time_zone; |
| 188 | time_zone.tz_minuteswest = 0; |
| 189 | time_zone.tz_dsttime = 0; |
| 190 | gettimeofday(&value, &time_zone); |
| 191 | TIMEVAL_TO_TIMESPEC(&value, &created_at_); |
| 192 | #endif |
| 193 | count_ = 0; |
| 194 | } |
| 195 | |
| 196 | timespec end_at; |
| 197 | unsigned long long time = time_ * ++count_; |
| 198 | end_at.tv_sec = created_at_.tv_sec + time / 1000; |
| 199 | end_at.tv_nsec = created_at_.tv_nsec + (time - (time / 1000) * 1000) * E6; |
| 200 | |
| 201 | if (end_at.tv_nsec >= E9) { |
| 202 | end_at.tv_sec++; |
| 203 | end_at.tv_nsec -= E9; |
| 204 | } |
| 205 | |
pbos@webrtc.org | e6dc38e | 2013-08-20 09:49:19 +0000 | [diff] [blame] | 206 | pthread_mutex_unlock(&mutex_); |
pbos@webrtc.org | a846371 | 2015-03-17 13:11:15 +0000 | [diff] [blame] | 207 | if (timer_event_->Wait(&end_at) == kEventSignaled) |
| 208 | return true; |
pbos@webrtc.org | e6dc38e | 2013-08-20 09:49:19 +0000 | [diff] [blame] | 209 | |
| 210 | pthread_mutex_lock(&mutex_); |
| 211 | if (periodic_ || count_ == 1) |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 212 | Set(); |
pbos@webrtc.org | e6dc38e | 2013-08-20 09:49:19 +0000 | [diff] [blame] | 213 | pthread_mutex_unlock(&mutex_); |
| 214 | |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 215 | return true; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 216 | } |
| 217 | |
Peter Boström | 64c0366 | 2015-04-08 11:24:19 +0200 | [diff] [blame] | 218 | bool EventTimerPosix::StopTimer() { |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 219 | if (timer_event_) { |
| 220 | timer_event_->Set(); |
| 221 | } |
| 222 | if (timer_thread_) { |
Peter Boström | 8c38e8b | 2015-11-26 17:45:47 +0100 | [diff] [blame] | 223 | timer_thread_->Stop(); |
tommi@webrtc.org | 361981f | 2015-03-19 14:44:18 +0000 | [diff] [blame] | 224 | timer_thread_.reset(); |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 225 | } |
Peter Boström | 64c0366 | 2015-04-08 11:24:19 +0200 | [diff] [blame] | 226 | timer_event_.reset(); |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 227 | |
| 228 | // Set time to zero to force new reference time for the timer. |
| 229 | memset(&created_at_, 0, sizeof(created_at_)); |
| 230 | count_ = 0; |
| 231 | return true; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 232 | } |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 233 | |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 234 | } // namespace webrtc |