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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "system_wrappers/source/event_timer_posix.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 12 | |
Mirko Bonadei | 7fd74ff | 2018-02-26 17:06:51 +0100 | [diff] [blame] | 13 | #if defined(WEBRTC_ANDROID) |
| 14 | #include <android/api-level.h> |
| 15 | #endif |
| 16 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 17 | #include <errno.h> |
| 18 | #include <pthread.h> |
| 19 | #include <signal.h> |
| 20 | #include <stdio.h> |
| 21 | #include <string.h> |
| 22 | #include <sys/time.h> |
| 23 | #include <unistd.h> |
| 24 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 25 | #include "rtc_base/checks.h" |
tommi@webrtc.org | f7e6cfd | 2015-02-09 18:25:38 +0000 | [diff] [blame] | 26 | |
Mirko Bonadei | 7fd74ff | 2018-02-26 17:06:51 +0100 | [diff] [blame] | 27 | #if defined(HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC) |
| 28 | // Chromium build is always defining this macro if __ANDROID_API__ < 20. |
| 29 | #undef HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC |
| 30 | #endif |
| 31 | |
| 32 | #if defined(WEBRTC_ANDROID) && defined(__ANDROID_API__) |
| 33 | #define HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC (__ANDROID_API__ < 21) |
| 34 | #else |
| 35 | #define HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC 0 |
| 36 | #endif |
| 37 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 38 | namespace webrtc { |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 39 | |
Peter Boström | 64c0366 | 2015-04-08 11:24:19 +0200 | [diff] [blame] | 40 | // static |
| 41 | EventTimerWrapper* EventTimerWrapper::Create() { |
| 42 | return new EventTimerPosix(); |
| 43 | } |
| 44 | |
sprang | 53cf346 | 2016-03-22 01:51:39 -0700 | [diff] [blame] | 45 | const int64_t kNanosecondsPerMillisecond = 1000000; |
| 46 | const int64_t kNanosecondsPerSecond = 1000000000; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 47 | |
Peter Boström | 64c0366 | 2015-04-08 11:24:19 +0200 | [diff] [blame] | 48 | EventTimerPosix::EventTimerPosix() |
pbos@webrtc.org | a846371 | 2015-03-17 13:11:15 +0000 | [diff] [blame] | 49 | : event_set_(false), |
| 50 | timer_thread_(nullptr), |
pbos@webrtc.org | a846371 | 2015-03-17 13:11:15 +0000 | [diff] [blame] | 51 | created_at_(), |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 52 | periodic_(false), |
sprang | 53cf346 | 2016-03-22 01:51:39 -0700 | [diff] [blame] | 53 | time_ms_(0), |
| 54 | count_(0), |
| 55 | is_stopping_(false) { |
pbos@webrtc.org | e6dc38e | 2013-08-20 09:49:19 +0000 | [diff] [blame] | 56 | pthread_mutexattr_t attr; |
| 57 | pthread_mutexattr_init(&attr); |
| 58 | pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); |
pbos@webrtc.org | a846371 | 2015-03-17 13:11:15 +0000 | [diff] [blame] | 59 | pthread_mutex_init(&mutex_, &attr); |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 60 | pthread_condattr_t cond_attr; |
pbos@webrtc.org | a846371 | 2015-03-17 13:11:15 +0000 | [diff] [blame] | 61 | pthread_condattr_init(&cond_attr); |
sprang | e791ffd | 2016-01-26 01:53:20 -0800 | [diff] [blame] | 62 | // TODO(sprang): Remove HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC special case once |
| 63 | // all supported Android platforms support pthread_condattr_setclock. |
| 64 | // TODO(sprang): Add support for monotonic clock on Apple platforms. |
| 65 | #if !(defined(WEBRTC_MAC) || defined(WEBRTC_IOS)) && \ |
Mirko Bonadei | 7fd74ff | 2018-02-26 17:06:51 +0100 | [diff] [blame] | 66 | !(defined(WEBRTC_ANDROID) && HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC) |
pbos@webrtc.org | a846371 | 2015-03-17 13:11:15 +0000 | [diff] [blame] | 67 | pthread_condattr_setclock(&cond_attr, CLOCK_MONOTONIC); |
sprang | e791ffd | 2016-01-26 01:53:20 -0800 | [diff] [blame] | 68 | #endif |
pbos@webrtc.org | a846371 | 2015-03-17 13:11:15 +0000 | [diff] [blame] | 69 | pthread_cond_init(&cond_, &cond_attr); |
| 70 | pthread_condattr_destroy(&cond_attr); |
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 | EventTimerPosix::~EventTimerPosix() { |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 74 | StopTimer(); |
| 75 | pthread_cond_destroy(&cond_); |
| 76 | pthread_mutex_destroy(&mutex_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 77 | } |
| 78 | |
pbos@webrtc.org | a846371 | 2015-03-17 13:11:15 +0000 | [diff] [blame] | 79 | // TODO(pbos): Make this void. |
Peter Boström | 64c0366 | 2015-04-08 11:24:19 +0200 | [diff] [blame] | 80 | bool EventTimerPosix::Set() { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 81 | RTC_CHECK_EQ(0, pthread_mutex_lock(&mutex_)); |
pbos@webrtc.org | a846371 | 2015-03-17 13:11:15 +0000 | [diff] [blame] | 82 | event_set_ = true; |
| 83 | pthread_cond_signal(&cond_); |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 84 | pthread_mutex_unlock(&mutex_); |
| 85 | return true; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 86 | } |
| 87 | |
sprang | 53cf346 | 2016-03-22 01:51:39 -0700 | [diff] [blame] | 88 | EventTypeWrapper EventTimerPosix::Wait(unsigned long timeout_ms) { |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 89 | int ret_val = 0; |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 90 | RTC_CHECK_EQ(0, pthread_mutex_lock(&mutex_)); |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 91 | |
pbos@webrtc.org | a846371 | 2015-03-17 13:11:15 +0000 | [diff] [blame] | 92 | if (!event_set_) { |
sprang | 53cf346 | 2016-03-22 01:51:39 -0700 | [diff] [blame] | 93 | if (WEBRTC_EVENT_INFINITE != timeout_ms) { |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 94 | timespec end_at; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 95 | #ifndef WEBRTC_MAC |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 96 | clock_gettime(CLOCK_MONOTONIC, &end_at); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 97 | #else |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 98 | timeval value; |
| 99 | struct timezone time_zone; |
| 100 | time_zone.tz_minuteswest = 0; |
| 101 | time_zone.tz_dsttime = 0; |
| 102 | gettimeofday(&value, &time_zone); |
| 103 | TIMEVAL_TO_TIMESPEC(&value, &end_at); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 104 | #endif |
sprang | 53cf346 | 2016-03-22 01:51:39 -0700 | [diff] [blame] | 105 | end_at.tv_sec += timeout_ms / 1000; |
| 106 | end_at.tv_nsec += (timeout_ms % 1000) * kNanosecondsPerMillisecond; |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 107 | |
sprang | 53cf346 | 2016-03-22 01:51:39 -0700 | [diff] [blame] | 108 | if (end_at.tv_nsec >= kNanosecondsPerSecond) { |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 109 | end_at.tv_sec++; |
sprang | 53cf346 | 2016-03-22 01:51:39 -0700 | [diff] [blame] | 110 | end_at.tv_nsec -= kNanosecondsPerSecond; |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 111 | } |
sprang | e791ffd | 2016-01-26 01:53:20 -0800 | [diff] [blame] | 112 | while (ret_val == 0 && !event_set_) { |
Mirko Bonadei | 7fd74ff | 2018-02-26 17:06:51 +0100 | [diff] [blame] | 113 | #if defined(WEBRTC_ANDROID) && HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC |
sprang | e791ffd | 2016-01-26 01:53:20 -0800 | [diff] [blame] | 114 | ret_val = pthread_cond_timedwait_monotonic_np(&cond_, &mutex_, &end_at); |
| 115 | #else |
pbos@webrtc.org | a846371 | 2015-03-17 13:11:15 +0000 | [diff] [blame] | 116 | ret_val = pthread_cond_timedwait(&cond_, &mutex_, &end_at); |
sprang | e791ffd | 2016-01-26 01:53:20 -0800 | [diff] [blame] | 117 | #endif // WEBRTC_ANDROID && HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC |
| 118 | } |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 119 | } else { |
pbos@webrtc.org | a846371 | 2015-03-17 13:11:15 +0000 | [diff] [blame] | 120 | while (ret_val == 0 && !event_set_) |
| 121 | ret_val = pthread_cond_wait(&cond_, &mutex_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 122 | } |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 123 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 124 | |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 125 | RTC_DCHECK(ret_val == 0 || ret_val == ETIMEDOUT); |
tommi@webrtc.org | f7e6cfd | 2015-02-09 18:25:38 +0000 | [diff] [blame] | 126 | |
pbos@webrtc.org | a846371 | 2015-03-17 13:11:15 +0000 | [diff] [blame] | 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_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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 | |
sprang | 53cf346 | 2016-03-22 01:51:39 -0700 | [diff] [blame] | 137 | EventTypeWrapper EventTimerPosix::Wait(timespec* end_at, bool reset_event) { |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 138 | int ret_val = 0; |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 139 | RTC_CHECK_EQ(0, pthread_mutex_lock(&mutex_)); |
sprang | 53cf346 | 2016-03-22 01:51:39 -0700 | [diff] [blame] | 140 | if (reset_event) { |
| 141 | // Only wake for new events or timeouts. |
| 142 | event_set_ = false; |
| 143 | } |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 144 | |
sprang | e791ffd | 2016-01-26 01:53:20 -0800 | [diff] [blame] | 145 | while (ret_val == 0 && !event_set_) { |
Mirko Bonadei | 7fd74ff | 2018-02-26 17:06:51 +0100 | [diff] [blame] | 146 | #if defined(WEBRTC_ANDROID) && HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC |
sprang | e791ffd | 2016-01-26 01:53:20 -0800 | [diff] [blame] | 147 | ret_val = pthread_cond_timedwait_monotonic_np(&cond_, &mutex_, end_at); |
| 148 | #else |
pbos@webrtc.org | a846371 | 2015-03-17 13:11:15 +0000 | [diff] [blame] | 149 | ret_val = pthread_cond_timedwait(&cond_, &mutex_, end_at); |
sprang | e791ffd | 2016-01-26 01:53:20 -0800 | [diff] [blame] | 150 | #endif // WEBRTC_ANDROID && HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC |
| 151 | } |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 152 | |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 153 | RTC_DCHECK(ret_val == 0 || ret_val == ETIMEDOUT); |
pbos@webrtc.org | a846371 | 2015-03-17 13:11:15 +0000 | [diff] [blame] | 154 | |
| 155 | // Reset and signal if set, regardless of why the thread woke up. |
| 156 | if (event_set_) { |
| 157 | ret_val = 0; |
| 158 | event_set_ = false; |
| 159 | } |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 160 | pthread_mutex_unlock(&mutex_); |
| 161 | |
pbos@webrtc.org | a846371 | 2015-03-17 13:11:15 +0000 | [diff] [blame] | 162 | return ret_val == 0 ? kEventSignaled : kEventTimeout; |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 163 | } |
| 164 | |
sprang | 53cf346 | 2016-03-22 01:51:39 -0700 | [diff] [blame] | 165 | rtc::PlatformThread* EventTimerPosix::CreateThread() { |
| 166 | const char* kThreadName = "WebRtc_event_timer_thread"; |
| 167 | return new rtc::PlatformThread(Run, this, kThreadName); |
| 168 | } |
| 169 | |
| 170 | bool EventTimerPosix::StartTimer(bool periodic, unsigned long time_ms) { |
pbos@webrtc.org | e6dc38e | 2013-08-20 09:49:19 +0000 | [diff] [blame] | 171 | pthread_mutex_lock(&mutex_); |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 172 | if (timer_thread_) { |
| 173 | if (periodic_) { |
| 174 | // Timer already started. |
pbos@webrtc.org | e6dc38e | 2013-08-20 09:49:19 +0000 | [diff] [blame] | 175 | pthread_mutex_unlock(&mutex_); |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 176 | return false; |
Karl Wiberg | 79eb1d9 | 2017-11-08 12:26:07 +0100 | [diff] [blame] | 177 | } else { |
sprang | 53cf346 | 2016-03-22 01:51:39 -0700 | [diff] [blame] | 178 | // New one shot timer. |
| 179 | time_ms_ = time_ms; |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 180 | created_at_.tv_sec = 0; |
| 181 | timer_event_->Set(); |
pbos@webrtc.org | e6dc38e | 2013-08-20 09:49:19 +0000 | [diff] [blame] | 182 | pthread_mutex_unlock(&mutex_); |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 183 | return true; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 184 | } |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 185 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 186 | |
sprang | 53cf346 | 2016-03-22 01:51:39 -0700 | [diff] [blame] | 187 | // Start the timer thread. |
Peter Boström | 64c0366 | 2015-04-08 11:24:19 +0200 | [diff] [blame] | 188 | timer_event_.reset(new EventTimerPosix()); |
sprang | 53cf346 | 2016-03-22 01:51:39 -0700 | [diff] [blame] | 189 | timer_thread_.reset(CreateThread()); |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 190 | periodic_ = periodic; |
sprang | 53cf346 | 2016-03-22 01:51:39 -0700 | [diff] [blame] | 191 | time_ms_ = time_ms; |
Peter Boström | 8c38e8b | 2015-11-26 17:45:47 +0100 | [diff] [blame] | 192 | timer_thread_->Start(); |
| 193 | timer_thread_->SetPriority(rtc::kRealtimePriority); |
pbos@webrtc.org | e6dc38e | 2013-08-20 09:49:19 +0000 | [diff] [blame] | 194 | pthread_mutex_unlock(&mutex_); |
| 195 | |
Peter Boström | 8c38e8b | 2015-11-26 17:45:47 +0100 | [diff] [blame] | 196 | return true; |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Peter Boström | 64c0366 | 2015-04-08 11:24:19 +0200 | [diff] [blame] | 199 | bool EventTimerPosix::Run(void* obj) { |
| 200 | return static_cast<EventTimerPosix*>(obj)->Process(); |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 201 | } |
| 202 | |
Peter Boström | 64c0366 | 2015-04-08 11:24:19 +0200 | [diff] [blame] | 203 | bool EventTimerPosix::Process() { |
pbos@webrtc.org | e6dc38e | 2013-08-20 09:49:19 +0000 | [diff] [blame] | 204 | pthread_mutex_lock(&mutex_); |
sprang | 53cf346 | 2016-03-22 01:51:39 -0700 | [diff] [blame] | 205 | if (is_stopping_) { |
| 206 | pthread_mutex_unlock(&mutex_); |
| 207 | return false; |
| 208 | } |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 209 | if (created_at_.tv_sec == 0) { |
| 210 | #ifndef WEBRTC_MAC |
sprang | 53cf346 | 2016-03-22 01:51:39 -0700 | [diff] [blame] | 211 | RTC_CHECK_EQ(0, clock_gettime(CLOCK_MONOTONIC, &created_at_)); |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 212 | #else |
| 213 | timeval value; |
| 214 | struct timezone time_zone; |
| 215 | time_zone.tz_minuteswest = 0; |
| 216 | time_zone.tz_dsttime = 0; |
| 217 | gettimeofday(&value, &time_zone); |
| 218 | TIMEVAL_TO_TIMESPEC(&value, &created_at_); |
| 219 | #endif |
| 220 | count_ = 0; |
| 221 | } |
| 222 | |
| 223 | timespec end_at; |
sprang | 53cf346 | 2016-03-22 01:51:39 -0700 | [diff] [blame] | 224 | unsigned long long total_delta_ms = time_ms_ * ++count_; |
| 225 | if (!periodic_ && count_ >= 1) { |
| 226 | // No need to wake up often if we're not going to signal waiting threads. |
| 227 | total_delta_ms = |
| 228 | std::min<uint64_t>(total_delta_ms, 60 * kNanosecondsPerSecond); |
| 229 | } |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 230 | |
sprang | 53cf346 | 2016-03-22 01:51:39 -0700 | [diff] [blame] | 231 | end_at.tv_sec = created_at_.tv_sec + total_delta_ms / 1000; |
| 232 | end_at.tv_nsec = created_at_.tv_nsec + |
| 233 | (total_delta_ms % 1000) * kNanosecondsPerMillisecond; |
| 234 | |
| 235 | if (end_at.tv_nsec >= kNanosecondsPerSecond) { |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 236 | end_at.tv_sec++; |
sprang | 53cf346 | 2016-03-22 01:51:39 -0700 | [diff] [blame] | 237 | end_at.tv_nsec -= kNanosecondsPerSecond; |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 238 | } |
| 239 | |
pbos@webrtc.org | e6dc38e | 2013-08-20 09:49:19 +0000 | [diff] [blame] | 240 | pthread_mutex_unlock(&mutex_); |
sprang | 53cf346 | 2016-03-22 01:51:39 -0700 | [diff] [blame] | 241 | // Reset event on first call so that we don't immediately return here if this |
| 242 | // thread was not blocked on timer_event_->Wait when the StartTimer() call |
| 243 | // was made. |
| 244 | if (timer_event_->Wait(&end_at, count_ == 1) == kEventSignaled) |
pbos@webrtc.org | a846371 | 2015-03-17 13:11:15 +0000 | [diff] [blame] | 245 | return true; |
pbos@webrtc.org | e6dc38e | 2013-08-20 09:49:19 +0000 | [diff] [blame] | 246 | |
| 247 | pthread_mutex_lock(&mutex_); |
| 248 | if (periodic_ || count_ == 1) |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 249 | Set(); |
pbos@webrtc.org | e6dc38e | 2013-08-20 09:49:19 +0000 | [diff] [blame] | 250 | pthread_mutex_unlock(&mutex_); |
| 251 | |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 252 | return true; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 253 | } |
| 254 | |
Peter Boström | 64c0366 | 2015-04-08 11:24:19 +0200 | [diff] [blame] | 255 | bool EventTimerPosix::StopTimer() { |
sprang | 53cf346 | 2016-03-22 01:51:39 -0700 | [diff] [blame] | 256 | pthread_mutex_lock(&mutex_); |
| 257 | is_stopping_ = true; |
| 258 | pthread_mutex_unlock(&mutex_); |
| 259 | |
| 260 | if (timer_event_) |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 261 | timer_event_->Set(); |
sprang | 53cf346 | 2016-03-22 01:51:39 -0700 | [diff] [blame] | 262 | |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 263 | if (timer_thread_) { |
Peter Boström | 8c38e8b | 2015-11-26 17:45:47 +0100 | [diff] [blame] | 264 | timer_thread_->Stop(); |
tommi@webrtc.org | 361981f | 2015-03-19 14:44:18 +0000 | [diff] [blame] | 265 | timer_thread_.reset(); |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 266 | } |
Peter Boström | 64c0366 | 2015-04-08 11:24:19 +0200 | [diff] [blame] | 267 | timer_event_.reset(); |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 268 | |
| 269 | // Set time to zero to force new reference time for the timer. |
| 270 | memset(&created_at_, 0, sizeof(created_at_)); |
| 271 | count_ = 0; |
| 272 | return true; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 273 | } |
phoglund@webrtc.org | 5bbe069 | 2012-12-10 10:44:37 +0000 | [diff] [blame] | 274 | |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 275 | } // namespace webrtc |