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::Set() { |
| 96 | if (0 != pthread_mutex_lock(&mutex_)) { |
| 97 | return false; |
| 98 | } |
| 99 | state_ = kUp; |
| 100 | // Release all waiting threads |
| 101 | pthread_cond_broadcast(&cond_); |
| 102 | pthread_mutex_unlock(&mutex_); |
| 103 | return true; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 104 | } |
| 105 | |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 106 | EventTypeWrapper EventPosix::Wait(unsigned long timeout) { |
| 107 | int ret_val = 0; |
| 108 | if (0 != pthread_mutex_lock(&mutex_)) { |
| 109 | return kEventError; |
| 110 | } |
| 111 | |
| 112 | if (kDown == state_) { |
| 113 | if (WEBRTC_EVENT_INFINITE != timeout) { |
| 114 | timespec end_at; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 115 | #ifndef WEBRTC_MAC |
| 116 | #ifdef WEBRTC_CLOCK_TYPE_REALTIME |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 117 | clock_gettime(CLOCK_REALTIME, &end_at); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 118 | #else |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 119 | clock_gettime(CLOCK_MONOTONIC, &end_at); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 120 | #endif |
| 121 | #else |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 122 | timeval value; |
| 123 | struct timezone time_zone; |
| 124 | time_zone.tz_minuteswest = 0; |
| 125 | time_zone.tz_dsttime = 0; |
| 126 | gettimeofday(&value, &time_zone); |
| 127 | TIMEVAL_TO_TIMESPEC(&value, &end_at); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 128 | #endif |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 129 | end_at.tv_sec += timeout / 1000; |
| 130 | end_at.tv_nsec += (timeout - (timeout / 1000) * 1000) * E6; |
| 131 | |
| 132 | if (end_at.tv_nsec >= E9) { |
| 133 | end_at.tv_sec++; |
| 134 | end_at.tv_nsec -= E9; |
| 135 | } |
| 136 | ret_val = pthread_cond_timedwait(&cond_, &mutex_, &end_at); |
| 137 | } else { |
| 138 | ret_val = pthread_cond_wait(&cond_, &mutex_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 139 | } |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 140 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 141 | |
tommi@webrtc.org | 30015e3 | 2015-02-10 09:33:28 +0000 | [diff] [blame] | 142 | // Be careful to only change the state if we're about to report that the |
| 143 | // event was signaled. |
| 144 | if (ret_val == 0) { |
sprang@webrtc.org | a6e8ceb | 2015-02-11 15:19:08 +0000 | [diff] [blame] | 145 | // state_ might already be kDown, in case of multiple waiters. That's OK. |
tommi@webrtc.org | 30015e3 | 2015-02-10 09:33:28 +0000 | [diff] [blame] | 146 | state_ = kDown; |
| 147 | } |
tommi@webrtc.org | f7e6cfd | 2015-02-09 18:25:38 +0000 | [diff] [blame] | 148 | |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 149 | pthread_mutex_unlock(&mutex_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 150 | |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 151 | switch (ret_val) { |
| 152 | case 0: |
| 153 | return kEventSignaled; |
| 154 | case ETIMEDOUT: |
| 155 | return kEventTimeout; |
| 156 | default: |
| 157 | return kEventError; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | EventTypeWrapper EventPosix::Wait(timespec& wake_at) { |
| 162 | int ret_val = 0; |
| 163 | if (0 != pthread_mutex_lock(&mutex_)) { |
| 164 | return kEventError; |
| 165 | } |
| 166 | |
| 167 | if (kUp != state_) { |
| 168 | ret_val = pthread_cond_timedwait(&cond_, &mutex_, &wake_at); |
| 169 | } |
| 170 | state_ = kDown; |
| 171 | |
| 172 | pthread_mutex_unlock(&mutex_); |
| 173 | |
| 174 | switch (ret_val) { |
| 175 | case 0: |
| 176 | return kEventSignaled; |
| 177 | case ETIMEDOUT: |
| 178 | return kEventTimeout; |
| 179 | default: |
| 180 | return kEventError; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | bool EventPosix::StartTimer(bool periodic, unsigned long time) { |
pbos@webrtc.org | e6dc38e | 2013-08-20 09:49:19 +0000 | [diff] [blame] | 185 | pthread_mutex_lock(&mutex_); |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 186 | if (timer_thread_) { |
| 187 | if (periodic_) { |
| 188 | // Timer already started. |
pbos@webrtc.org | e6dc38e | 2013-08-20 09:49:19 +0000 | [diff] [blame] | 189 | pthread_mutex_unlock(&mutex_); |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 190 | return false; |
| 191 | } else { |
| 192 | // New one shot timer |
| 193 | time_ = time; |
| 194 | created_at_.tv_sec = 0; |
| 195 | timer_event_->Set(); |
pbos@webrtc.org | e6dc38e | 2013-08-20 09:49:19 +0000 | [diff] [blame] | 196 | pthread_mutex_unlock(&mutex_); |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 197 | return true; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 198 | } |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 199 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 200 | |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 201 | // Start the timer thread |
| 202 | timer_event_ = static_cast<EventPosix*>(EventWrapper::Create()); |
| 203 | const char* thread_name = "WebRtc_event_timer_thread"; |
| 204 | timer_thread_ = ThreadWrapper::CreateThread(Run, this, kRealtimePriority, |
| 205 | thread_name); |
| 206 | periodic_ = periodic; |
| 207 | time_ = time; |
pbos@webrtc.org | 8663973 | 2015-03-13 00:06:21 +0000 | [diff] [blame] | 208 | bool started = timer_thread_->Start(); |
pbos@webrtc.org | e6dc38e | 2013-08-20 09:49:19 +0000 | [diff] [blame] | 209 | pthread_mutex_unlock(&mutex_); |
| 210 | |
| 211 | return started; |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | bool EventPosix::Run(ThreadObj obj) { |
| 215 | return static_cast<EventPosix*>(obj)->Process(); |
| 216 | } |
| 217 | |
| 218 | bool EventPosix::Process() { |
pbos@webrtc.org | e6dc38e | 2013-08-20 09:49:19 +0000 | [diff] [blame] | 219 | pthread_mutex_lock(&mutex_); |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 220 | if (created_at_.tv_sec == 0) { |
| 221 | #ifndef WEBRTC_MAC |
| 222 | #ifdef WEBRTC_CLOCK_TYPE_REALTIME |
| 223 | clock_gettime(CLOCK_REALTIME, &created_at_); |
| 224 | #else |
| 225 | clock_gettime(CLOCK_MONOTONIC, &created_at_); |
| 226 | #endif |
| 227 | #else |
| 228 | timeval value; |
| 229 | struct timezone time_zone; |
| 230 | time_zone.tz_minuteswest = 0; |
| 231 | time_zone.tz_dsttime = 0; |
| 232 | gettimeofday(&value, &time_zone); |
| 233 | TIMEVAL_TO_TIMESPEC(&value, &created_at_); |
| 234 | #endif |
| 235 | count_ = 0; |
| 236 | } |
| 237 | |
| 238 | timespec end_at; |
| 239 | unsigned long long time = time_ * ++count_; |
| 240 | end_at.tv_sec = created_at_.tv_sec + time / 1000; |
| 241 | end_at.tv_nsec = created_at_.tv_nsec + (time - (time / 1000) * 1000) * E6; |
| 242 | |
| 243 | if (end_at.tv_nsec >= E9) { |
| 244 | end_at.tv_sec++; |
| 245 | end_at.tv_nsec -= E9; |
| 246 | } |
| 247 | |
pbos@webrtc.org | e6dc38e | 2013-08-20 09:49:19 +0000 | [diff] [blame] | 248 | pthread_mutex_unlock(&mutex_); |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 249 | switch (timer_event_->Wait(end_at)) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 250 | case kEventSignaled: |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 251 | return true; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 252 | case kEventError: |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 253 | return false; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 254 | case kEventTimeout: |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 255 | break; |
| 256 | } |
pbos@webrtc.org | e6dc38e | 2013-08-20 09:49:19 +0000 | [diff] [blame] | 257 | |
| 258 | pthread_mutex_lock(&mutex_); |
| 259 | if (periodic_ || count_ == 1) |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 260 | Set(); |
pbos@webrtc.org | e6dc38e | 2013-08-20 09:49:19 +0000 | [diff] [blame] | 261 | pthread_mutex_unlock(&mutex_); |
| 262 | |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 263 | return true; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 264 | } |
| 265 | |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 266 | bool EventPosix::StopTimer() { |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 267 | if (timer_event_) { |
| 268 | timer_event_->Set(); |
| 269 | } |
| 270 | if (timer_thread_) { |
| 271 | if (!timer_thread_->Stop()) { |
| 272 | return false; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 273 | } |
| 274 | |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 275 | delete timer_thread_; |
| 276 | timer_thread_ = 0; |
| 277 | } |
| 278 | if (timer_event_) { |
| 279 | delete timer_event_; |
| 280 | timer_event_ = 0; |
| 281 | } |
| 282 | |
| 283 | // Set time to zero to force new reference time for the timer. |
| 284 | memset(&created_at_, 0, sizeof(created_at_)); |
| 285 | count_ = 0; |
| 286 | return true; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 287 | } |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 288 | |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 289 | } // namespace webrtc |