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() { |
| 29 | EventPosix* ptr = new EventPosix; |
| 30 | if (!ptr) { |
| 31 | return NULL; |
| 32 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 33 | |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 34 | const int error = ptr->Construct(); |
| 35 | if (error) { |
| 36 | delete ptr; |
| 37 | return NULL; |
| 38 | } |
| 39 | return ptr; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 40 | } |
| 41 | |
ajm@google.com | b5c49ff | 2011-08-01 17:04:04 +0000 | [diff] [blame] | 42 | EventPosix::EventPosix() |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 43 | : timer_thread_(0), |
| 44 | timer_event_(0), |
| 45 | periodic_(false), |
| 46 | time_(0), |
| 47 | count_(0), |
| 48 | state_(kDown) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 49 | } |
| 50 | |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 51 | int EventPosix::Construct() { |
| 52 | // Set start time to zero |
| 53 | memset(&created_at_, 0, sizeof(created_at_)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 54 | |
pbos@webrtc.org | e6dc38e | 2013-08-20 09:49:19 +0000 | [diff] [blame] | 55 | pthread_mutexattr_t attr; |
| 56 | pthread_mutexattr_init(&attr); |
| 57 | pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); |
| 58 | int result = pthread_mutex_init(&mutex_, &attr); |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 59 | if (result != 0) { |
| 60 | return -1; |
| 61 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 62 | #ifdef WEBRTC_CLOCK_TYPE_REALTIME |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 63 | result = pthread_cond_init(&cond_, 0); |
| 64 | if (result != 0) { |
| 65 | return -1; |
| 66 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 67 | #else |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 68 | pthread_condattr_t cond_attr; |
| 69 | result = pthread_condattr_init(&cond_attr); |
| 70 | if (result != 0) { |
| 71 | return -1; |
| 72 | } |
| 73 | result = pthread_condattr_setclock(&cond_attr, CLOCK_MONOTONIC); |
| 74 | if (result != 0) { |
| 75 | return -1; |
| 76 | } |
| 77 | result = pthread_cond_init(&cond_, &cond_attr); |
| 78 | if (result != 0) { |
| 79 | return -1; |
| 80 | } |
| 81 | result = pthread_condattr_destroy(&cond_attr); |
| 82 | if (result != 0) { |
| 83 | return -1; |
| 84 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 85 | #endif |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 86 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 87 | } |
| 88 | |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 89 | EventPosix::~EventPosix() { |
| 90 | StopTimer(); |
| 91 | pthread_cond_destroy(&cond_); |
| 92 | pthread_mutex_destroy(&mutex_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 93 | } |
| 94 | |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 95 | bool EventPosix::Reset() { |
| 96 | if (0 != pthread_mutex_lock(&mutex_)) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 97 | return false; |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 98 | } |
| 99 | state_ = kDown; |
| 100 | pthread_mutex_unlock(&mutex_); |
| 101 | return true; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 102 | } |
| 103 | |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 104 | bool EventPosix::Set() { |
| 105 | if (0 != pthread_mutex_lock(&mutex_)) { |
| 106 | return false; |
| 107 | } |
| 108 | state_ = kUp; |
| 109 | // Release all waiting threads |
| 110 | pthread_cond_broadcast(&cond_); |
| 111 | pthread_mutex_unlock(&mutex_); |
| 112 | return true; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 113 | } |
| 114 | |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 115 | EventTypeWrapper EventPosix::Wait(unsigned long timeout) { |
| 116 | int ret_val = 0; |
| 117 | if (0 != pthread_mutex_lock(&mutex_)) { |
| 118 | return kEventError; |
| 119 | } |
| 120 | |
| 121 | if (kDown == state_) { |
| 122 | if (WEBRTC_EVENT_INFINITE != timeout) { |
| 123 | timespec end_at; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 124 | #ifndef WEBRTC_MAC |
| 125 | #ifdef WEBRTC_CLOCK_TYPE_REALTIME |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 126 | clock_gettime(CLOCK_REALTIME, &end_at); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 127 | #else |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 128 | clock_gettime(CLOCK_MONOTONIC, &end_at); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 129 | #endif |
| 130 | #else |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 131 | timeval value; |
| 132 | struct timezone time_zone; |
| 133 | time_zone.tz_minuteswest = 0; |
| 134 | time_zone.tz_dsttime = 0; |
| 135 | gettimeofday(&value, &time_zone); |
| 136 | TIMEVAL_TO_TIMESPEC(&value, &end_at); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 137 | #endif |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 138 | end_at.tv_sec += timeout / 1000; |
| 139 | end_at.tv_nsec += (timeout - (timeout / 1000) * 1000) * E6; |
| 140 | |
| 141 | if (end_at.tv_nsec >= E9) { |
| 142 | end_at.tv_sec++; |
| 143 | end_at.tv_nsec -= E9; |
| 144 | } |
| 145 | ret_val = pthread_cond_timedwait(&cond_, &mutex_, &end_at); |
| 146 | } else { |
| 147 | ret_val = pthread_cond_wait(&cond_, &mutex_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 148 | } |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 149 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 150 | |
tommi@webrtc.org | 30015e3 | 2015-02-10 09:33:28 +0000 | [diff] [blame] | 151 | // Be careful to only change the state if we're about to report that the |
| 152 | // event was signaled. |
| 153 | if (ret_val == 0) { |
| 154 | DCHECK(state_ == kUp); |
| 155 | state_ = kDown; |
| 156 | } |
tommi@webrtc.org | f7e6cfd | 2015-02-09 18:25:38 +0000 | [diff] [blame] | 157 | |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 158 | pthread_mutex_unlock(&mutex_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 159 | |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 160 | switch (ret_val) { |
| 161 | case 0: |
| 162 | return kEventSignaled; |
| 163 | case ETIMEDOUT: |
| 164 | return kEventTimeout; |
| 165 | default: |
| 166 | return kEventError; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | EventTypeWrapper EventPosix::Wait(timespec& wake_at) { |
| 171 | int ret_val = 0; |
| 172 | if (0 != pthread_mutex_lock(&mutex_)) { |
| 173 | return kEventError; |
| 174 | } |
| 175 | |
| 176 | if (kUp != state_) { |
| 177 | ret_val = pthread_cond_timedwait(&cond_, &mutex_, &wake_at); |
| 178 | } |
| 179 | state_ = kDown; |
| 180 | |
| 181 | pthread_mutex_unlock(&mutex_); |
| 182 | |
| 183 | switch (ret_val) { |
| 184 | case 0: |
| 185 | return kEventSignaled; |
| 186 | case ETIMEDOUT: |
| 187 | return kEventTimeout; |
| 188 | default: |
| 189 | return kEventError; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | bool EventPosix::StartTimer(bool periodic, unsigned long time) { |
pbos@webrtc.org | e6dc38e | 2013-08-20 09:49:19 +0000 | [diff] [blame] | 194 | pthread_mutex_lock(&mutex_); |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 195 | if (timer_thread_) { |
| 196 | if (periodic_) { |
| 197 | // Timer already started. |
pbos@webrtc.org | e6dc38e | 2013-08-20 09:49:19 +0000 | [diff] [blame] | 198 | pthread_mutex_unlock(&mutex_); |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 199 | return false; |
| 200 | } else { |
| 201 | // New one shot timer |
| 202 | time_ = time; |
| 203 | created_at_.tv_sec = 0; |
| 204 | timer_event_->Set(); |
pbos@webrtc.org | e6dc38e | 2013-08-20 09:49:19 +0000 | [diff] [blame] | 205 | pthread_mutex_unlock(&mutex_); |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 206 | return true; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 207 | } |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 208 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 209 | |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 210 | // Start the timer thread |
| 211 | timer_event_ = static_cast<EventPosix*>(EventWrapper::Create()); |
| 212 | const char* thread_name = "WebRtc_event_timer_thread"; |
| 213 | timer_thread_ = ThreadWrapper::CreateThread(Run, this, kRealtimePriority, |
| 214 | thread_name); |
| 215 | periodic_ = periodic; |
| 216 | time_ = time; |
| 217 | unsigned int id = 0; |
pbos@webrtc.org | e6dc38e | 2013-08-20 09:49:19 +0000 | [diff] [blame] | 218 | bool started = timer_thread_->Start(id); |
| 219 | pthread_mutex_unlock(&mutex_); |
| 220 | |
| 221 | return started; |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | bool EventPosix::Run(ThreadObj obj) { |
| 225 | return static_cast<EventPosix*>(obj)->Process(); |
| 226 | } |
| 227 | |
| 228 | bool EventPosix::Process() { |
pbos@webrtc.org | e6dc38e | 2013-08-20 09:49:19 +0000 | [diff] [blame] | 229 | pthread_mutex_lock(&mutex_); |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 230 | if (created_at_.tv_sec == 0) { |
| 231 | #ifndef WEBRTC_MAC |
| 232 | #ifdef WEBRTC_CLOCK_TYPE_REALTIME |
| 233 | clock_gettime(CLOCK_REALTIME, &created_at_); |
| 234 | #else |
| 235 | clock_gettime(CLOCK_MONOTONIC, &created_at_); |
| 236 | #endif |
| 237 | #else |
| 238 | timeval value; |
| 239 | struct timezone time_zone; |
| 240 | time_zone.tz_minuteswest = 0; |
| 241 | time_zone.tz_dsttime = 0; |
| 242 | gettimeofday(&value, &time_zone); |
| 243 | TIMEVAL_TO_TIMESPEC(&value, &created_at_); |
| 244 | #endif |
| 245 | count_ = 0; |
| 246 | } |
| 247 | |
| 248 | timespec end_at; |
| 249 | unsigned long long time = time_ * ++count_; |
| 250 | end_at.tv_sec = created_at_.tv_sec + time / 1000; |
| 251 | end_at.tv_nsec = created_at_.tv_nsec + (time - (time / 1000) * 1000) * E6; |
| 252 | |
| 253 | if (end_at.tv_nsec >= E9) { |
| 254 | end_at.tv_sec++; |
| 255 | end_at.tv_nsec -= E9; |
| 256 | } |
| 257 | |
pbos@webrtc.org | e6dc38e | 2013-08-20 09:49:19 +0000 | [diff] [blame] | 258 | pthread_mutex_unlock(&mutex_); |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 259 | switch (timer_event_->Wait(end_at)) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 260 | case kEventSignaled: |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 261 | return true; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 262 | case kEventError: |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 263 | return false; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 264 | case kEventTimeout: |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 265 | break; |
| 266 | } |
pbos@webrtc.org | e6dc38e | 2013-08-20 09:49:19 +0000 | [diff] [blame] | 267 | |
| 268 | pthread_mutex_lock(&mutex_); |
| 269 | if (periodic_ || count_ == 1) |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 270 | Set(); |
pbos@webrtc.org | e6dc38e | 2013-08-20 09:49:19 +0000 | [diff] [blame] | 271 | pthread_mutex_unlock(&mutex_); |
| 272 | |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 273 | return true; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 274 | } |
| 275 | |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 276 | bool EventPosix::StopTimer() { |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 277 | if (timer_event_) { |
| 278 | timer_event_->Set(); |
| 279 | } |
| 280 | if (timer_thread_) { |
| 281 | if (!timer_thread_->Stop()) { |
| 282 | return false; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 283 | } |
| 284 | |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 285 | delete timer_thread_; |
| 286 | timer_thread_ = 0; |
| 287 | } |
| 288 | if (timer_event_) { |
| 289 | delete timer_event_; |
| 290 | timer_event_ = 0; |
| 291 | } |
| 292 | |
| 293 | // Set time to zero to force new reference time for the timer. |
| 294 | memset(&created_at_, 0, sizeof(created_at_)); |
| 295 | count_ = 0; |
| 296 | return true; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 297 | } |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 298 | |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 299 | } // namespace webrtc |