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