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 | |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 11 | #include "webrtc/system_wrappers/source/event_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 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 25 | const long int E6 = 1000000; |
| 26 | const long int E9 = 1000 * E6; |
| 27 | |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 28 | EventWrapper* EventPosix::Create() { |
pbos@webrtc.org | a846371 | 2015-03-17 13:11:15 +0000 | [diff] [blame] | 29 | return new EventPosix(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 30 | } |
| 31 | |
ajm@google.com | b5c49ff | 2011-08-01 17:04:04 +0000 | [diff] [blame] | 32 | EventPosix::EventPosix() |
pbos@webrtc.org | a846371 | 2015-03-17 13:11:15 +0000 | [diff] [blame] | 33 | : event_set_(false), |
| 34 | timer_thread_(nullptr), |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 35 | timer_event_(0), |
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 | |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 55 | EventPosix::~EventPosix() { |
| 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. |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 62 | bool EventPosix::Set() { |
pbos@webrtc.org | a846371 | 2015-03-17 13:11:15 +0000 | [diff] [blame] | 63 | CHECK_EQ(0, pthread_mutex_lock(&mutex_)); |
| 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 | |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 70 | EventTypeWrapper EventPosix::Wait(unsigned long timeout) { |
| 71 | int ret_val = 0; |
pbos@webrtc.org | a846371 | 2015-03-17 13:11:15 +0000 | [diff] [blame] | 72 | 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 | |
pbos@webrtc.org | a846371 | 2015-03-17 13:11:15 +0000 | [diff] [blame] | 106 | 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 | |
pbos@webrtc.org | a846371 | 2015-03-17 13:11:15 +0000 | [diff] [blame] | 118 | EventTypeWrapper EventPosix::Wait(timespec* end_at) { |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 119 | int ret_val = 0; |
pbos@webrtc.org | a846371 | 2015-03-17 13:11:15 +0000 | [diff] [blame] | 120 | 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 | |
pbos@webrtc.org | a846371 | 2015-03-17 13:11:15 +0000 | [diff] [blame] | 125 | DCHECK(ret_val == 0 || ret_val == ETIMEDOUT); |
| 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 | |
| 137 | bool EventPosix::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 |
| 155 | timer_event_ = static_cast<EventPosix*>(EventWrapper::Create()); |
| 156 | const char* thread_name = "WebRtc_event_timer_thread"; |
| 157 | timer_thread_ = ThreadWrapper::CreateThread(Run, this, kRealtimePriority, |
| 158 | thread_name); |
| 159 | periodic_ = periodic; |
| 160 | time_ = time; |
pbos@webrtc.org | 8663973 | 2015-03-13 00:06:21 +0000 | [diff] [blame] | 161 | bool started = timer_thread_->Start(); |
pbos@webrtc.org | e6dc38e | 2013-08-20 09:49:19 +0000 | [diff] [blame] | 162 | pthread_mutex_unlock(&mutex_); |
| 163 | |
| 164 | return started; |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 165 | } |
| 166 | |
tommi@webrtc.org | 27c0be9 | 2015-03-19 14:35:58 +0000 | [diff] [blame] | 167 | bool EventPosix::Run(void* obj) { |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 168 | return static_cast<EventPosix*>(obj)->Process(); |
| 169 | } |
| 170 | |
| 171 | bool EventPosix::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 | |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 213 | bool EventPosix::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_) { |
| 218 | if (!timer_thread_->Stop()) { |
| 219 | return false; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 220 | } |
tommi@webrtc.org | 361981f | 2015-03-19 14:44:18 +0000 | [diff] [blame] | 221 | timer_thread_.reset(); |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 222 | } |
| 223 | if (timer_event_) { |
| 224 | delete timer_event_; |
| 225 | timer_event_ = 0; |
| 226 | } |
| 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 |