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