tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 | |
Danil Chapovalov | eb17524 | 2019-02-12 09:44:38 | [diff] [blame] | 11 | #include "rtc_base/task_queue_libevent.h" |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 12 | |
Yves Gerey | 988cc08 | 2018-10-23 10:03:01 | [diff] [blame] | 13 | #include <errno.h> |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 14 | #include <fcntl.h> |
Yves Gerey | 988cc08 | 2018-10-23 10:03:01 | [diff] [blame] | 15 | #include <pthread.h> |
tommi | 8c80c6e | 2017-02-23 08:34:52 | [diff] [blame] | 16 | #include <signal.h> |
Yves Gerey | 988cc08 | 2018-10-23 10:03:01 | [diff] [blame] | 17 | #include <stdint.h> |
| 18 | #include <time.h> |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 19 | #include <unistd.h> |
Jonas Olsson | a4d8737 | 2019-07-05 17:08:33 | [diff] [blame] | 20 | |
Danil Chapovalov | 02fddf6 | 2018-02-12 11:41:16 | [diff] [blame] | 21 | #include <list> |
Yves Gerey | 988cc08 | 2018-10-23 10:03:01 | [diff] [blame] | 22 | #include <memory> |
| 23 | #include <type_traits> |
| 24 | #include <utility> |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 25 | |
Steve Anton | 9a83dd7 | 2020-01-09 19:03:25 | [diff] [blame] | 26 | #include "absl/container/inlined_vector.h" |
Danil Chapovalov | eb17524 | 2019-02-12 09:44:38 | [diff] [blame] | 27 | #include "absl/strings/string_view.h" |
| 28 | #include "api/task_queue/queued_task.h" |
| 29 | #include "api/task_queue/task_queue_base.h" |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 30 | #include "base/third_party/libevent/event.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 31 | #include "rtc_base/checks.h" |
| 32 | #include "rtc_base/logging.h" |
Karl Wiberg | e40468b | 2017-11-22 09:42:26 | [diff] [blame] | 33 | #include "rtc_base/numerics/safe_conversions.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 34 | #include "rtc_base/platform_thread.h" |
Yves Gerey | 988cc08 | 2018-10-23 10:03:01 | [diff] [blame] | 35 | #include "rtc_base/platform_thread_types.h" |
Markus Handell | 18523c3 | 2020-07-08 15:55:58 | [diff] [blame] | 36 | #include "rtc_base/synchronization/mutex.h" |
Yves Gerey | 988cc08 | 2018-10-23 10:03:01 | [diff] [blame] | 37 | #include "rtc_base/thread_annotations.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 38 | #include "rtc_base/time_utils.h" |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 39 | |
Danil Chapovalov | eb17524 | 2019-02-12 09:44:38 | [diff] [blame] | 40 | namespace webrtc { |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 41 | namespace { |
Danil Chapovalov | eb17524 | 2019-02-12 09:44:38 | [diff] [blame] | 42 | constexpr char kQuit = 1; |
Steve Anton | 9a83dd7 | 2020-01-09 19:03:25 | [diff] [blame] | 43 | constexpr char kRunTasks = 2; |
tommi | 8c80c6e | 2017-02-23 08:34:52 | [diff] [blame] | 44 | |
Danil Chapovalov | eb17524 | 2019-02-12 09:44:38 | [diff] [blame] | 45 | using Priority = TaskQueueFactory::Priority; |
tommi | c9bb791 | 2017-02-24 18:42:14 | [diff] [blame] | 46 | |
tommi | 8c80c6e | 2017-02-23 08:34:52 | [diff] [blame] | 47 | // This ignores the SIGPIPE signal on the calling thread. |
| 48 | // This signal can be fired when trying to write() to a pipe that's being |
| 49 | // closed or while closing a pipe that's being written to. |
Danil Chapovalov | 43f3982 | 2018-12-05 14:46:58 | [diff] [blame] | 50 | // We can run into that situation so we ignore this signal and continue as |
| 51 | // normal. |
tommi | 8c80c6e | 2017-02-23 08:34:52 | [diff] [blame] | 52 | // As a side note for this implementation, it would be great if we could safely |
| 53 | // restore the sigmask, but unfortunately the operation of restoring it, can |
| 54 | // itself actually cause SIGPIPE to be signaled :-| (e.g. on MacOS) |
| 55 | // The SIGPIPE signal by default causes the process to be terminated, so we |
| 56 | // don't want to risk that. |
| 57 | // An alternative to this approach is to ignore the signal for the whole |
| 58 | // process: |
| 59 | // signal(SIGPIPE, SIG_IGN); |
| 60 | void IgnoreSigPipeSignalOnCurrentThread() { |
| 61 | sigset_t sigpipe_mask; |
| 62 | sigemptyset(&sigpipe_mask); |
| 63 | sigaddset(&sigpipe_mask, SIGPIPE); |
| 64 | pthread_sigmask(SIG_BLOCK, &sigpipe_mask, nullptr); |
| 65 | } |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 66 | |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 67 | bool SetNonBlocking(int fd) { |
| 68 | const int flags = fcntl(fd, F_GETFL); |
| 69 | RTC_CHECK(flags != -1); |
| 70 | return (flags & O_NONBLOCK) || fcntl(fd, F_SETFL, flags | O_NONBLOCK) != -1; |
| 71 | } |
tommi | 1666b61 | 2016-07-13 17:58:12 | [diff] [blame] | 72 | |
| 73 | // TODO(tommi): This is a hack to support two versions of libevent that we're |
| 74 | // compatible with. The method we really want to call is event_assign(), |
| 75 | // since event_set() has been marked as deprecated (and doesn't accept |
| 76 | // passing event_base__ as a parameter). However, the version of libevent |
| 77 | // that we have in Chromium, doesn't have event_assign(), so we need to call |
| 78 | // event_set() there. |
| 79 | void EventAssign(struct event* ev, |
| 80 | struct event_base* base, |
| 81 | int fd, |
| 82 | short events, |
| 83 | void (*callback)(int, short, void*), |
| 84 | void* arg) { |
| 85 | #if defined(_EVENT2_EVENT_H_) |
| 86 | RTC_CHECK_EQ(0, event_assign(ev, base, fd, events, callback, arg)); |
| 87 | #else |
| 88 | event_set(ev, fd, events, callback, arg); |
| 89 | RTC_CHECK_EQ(0, event_base_set(base, ev)); |
| 90 | #endif |
| 91 | } |
tommi | c9bb791 | 2017-02-24 18:42:14 | [diff] [blame] | 92 | |
Danil Chapovalov | eb17524 | 2019-02-12 09:44:38 | [diff] [blame] | 93 | rtc::ThreadPriority TaskQueuePriorityToThreadPriority(Priority priority) { |
tommi | c9bb791 | 2017-02-24 18:42:14 | [diff] [blame] | 94 | switch (priority) { |
| 95 | case Priority::HIGH: |
Markus Handell | ad5037b | 2021-05-07 13:02:36 | [diff] [blame] | 96 | return rtc::ThreadPriority::kRealtime; |
tommi | c9bb791 | 2017-02-24 18:42:14 | [diff] [blame] | 97 | case Priority::LOW: |
Markus Handell | ad5037b | 2021-05-07 13:02:36 | [diff] [blame] | 98 | return rtc::ThreadPriority::kLow; |
tommi | c9bb791 | 2017-02-24 18:42:14 | [diff] [blame] | 99 | case Priority::NORMAL: |
Markus Handell | ad5037b | 2021-05-07 13:02:36 | [diff] [blame] | 100 | return rtc::ThreadPriority::kNormal; |
tommi | c9bb791 | 2017-02-24 18:42:14 | [diff] [blame] | 101 | } |
tommi | c9bb791 | 2017-02-24 18:42:14 | [diff] [blame] | 102 | } |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 103 | |
Danil Chapovalov | eb17524 | 2019-02-12 09:44:38 | [diff] [blame] | 104 | class TaskQueueLibevent final : public TaskQueueBase { |
perkj | 650fdae | 2017-08-25 12:00:11 | [diff] [blame] | 105 | public: |
Danil Chapovalov | eb17524 | 2019-02-12 09:44:38 | [diff] [blame] | 106 | TaskQueueLibevent(absl::string_view queue_name, rtc::ThreadPriority priority); |
perkj | 650fdae | 2017-08-25 12:00:11 | [diff] [blame] | 107 | |
Danil Chapovalov | eb17524 | 2019-02-12 09:44:38 | [diff] [blame] | 108 | void Delete() override; |
| 109 | void PostTask(std::unique_ptr<QueuedTask> task) override; |
| 110 | void PostDelayedTask(std::unique_ptr<QueuedTask> task, |
| 111 | uint32_t milliseconds) override; |
perkj | 650fdae | 2017-08-25 12:00:11 | [diff] [blame] | 112 | |
| 113 | private: |
Danil Chapovalov | eb17524 | 2019-02-12 09:44:38 | [diff] [blame] | 114 | class SetTimerTask; |
| 115 | struct TimerEvent; |
| 116 | |
| 117 | ~TaskQueueLibevent() override = default; |
| 118 | |
perkj | 650fdae | 2017-08-25 12:00:11 | [diff] [blame] | 119 | static void OnWakeup(int socket, short flags, void* context); // NOLINT |
perkj | 650fdae | 2017-08-25 12:00:11 | [diff] [blame] | 120 | static void RunTimer(int fd, short flags, void* context); // NOLINT |
| 121 | |
Danil Chapovalov | eb17524 | 2019-02-12 09:44:38 | [diff] [blame] | 122 | bool is_active_ = true; |
perkj | 650fdae | 2017-08-25 12:00:11 | [diff] [blame] | 123 | int wakeup_pipe_in_ = -1; |
| 124 | int wakeup_pipe_out_ = -1; |
| 125 | event_base* event_base_; |
Danil Chapovalov | eb17524 | 2019-02-12 09:44:38 | [diff] [blame] | 126 | event wakeup_event_; |
| 127 | rtc::PlatformThread thread_; |
Markus Handell | 18523c3 | 2020-07-08 15:55:58 | [diff] [blame] | 128 | Mutex pending_lock_; |
Steve Anton | 9a83dd7 | 2020-01-09 19:03:25 | [diff] [blame] | 129 | absl::InlinedVector<std::unique_ptr<QueuedTask>, 4> pending_ |
| 130 | RTC_GUARDED_BY(pending_lock_); |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 131 | // Holds a list of events pending timers for cleanup when the loop exits. |
| 132 | std::list<TimerEvent*> pending_timers_; |
| 133 | }; |
| 134 | |
Danil Chapovalov | eb17524 | 2019-02-12 09:44:38 | [diff] [blame] | 135 | struct TaskQueueLibevent::TimerEvent { |
| 136 | TimerEvent(TaskQueueLibevent* task_queue, std::unique_ptr<QueuedTask> task) |
| 137 | : task_queue(task_queue), task(std::move(task)) {} |
| 138 | ~TimerEvent() { event_del(&ev); } |
| 139 | |
| 140 | event ev; |
| 141 | TaskQueueLibevent* task_queue; |
| 142 | std::unique_ptr<QueuedTask> task; |
| 143 | }; |
| 144 | |
| 145 | class TaskQueueLibevent::SetTimerTask : public QueuedTask { |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 146 | public: |
| 147 | SetTimerTask(std::unique_ptr<QueuedTask> task, uint32_t milliseconds) |
| 148 | : task_(std::move(task)), |
| 149 | milliseconds_(milliseconds), |
Danil Chapovalov | eb17524 | 2019-02-12 09:44:38 | [diff] [blame] | 150 | posted_(rtc::Time32()) {} |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 151 | |
| 152 | private: |
| 153 | bool Run() override { |
| 154 | // Compensate for the time that has passed since construction |
| 155 | // and until we got here. |
Danil Chapovalov | eb17524 | 2019-02-12 09:44:38 | [diff] [blame] | 156 | uint32_t post_time = rtc::Time32() - posted_; |
| 157 | TaskQueueLibevent::Current()->PostDelayedTask( |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 158 | std::move(task_), |
| 159 | post_time > milliseconds_ ? 0 : milliseconds_ - post_time); |
| 160 | return true; |
| 161 | } |
| 162 | |
| 163 | std::unique_ptr<QueuedTask> task_; |
| 164 | const uint32_t milliseconds_; |
| 165 | const uint32_t posted_; |
| 166 | }; |
| 167 | |
Danil Chapovalov | eb17524 | 2019-02-12 09:44:38 | [diff] [blame] | 168 | TaskQueueLibevent::TaskQueueLibevent(absl::string_view queue_name, |
| 169 | rtc::ThreadPriority priority) |
Markus Handell | ad5037b | 2021-05-07 13:02:36 | [diff] [blame] | 170 | : event_base_(event_base_new()) { |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 171 | int fds[2]; |
| 172 | RTC_CHECK(pipe(fds) == 0); |
| 173 | SetNonBlocking(fds[0]); |
| 174 | SetNonBlocking(fds[1]); |
| 175 | wakeup_pipe_out_ = fds[0]; |
| 176 | wakeup_pipe_in_ = fds[1]; |
tommi | 8c80c6e | 2017-02-23 08:34:52 | [diff] [blame] | 177 | |
Danil Chapovalov | eb17524 | 2019-02-12 09:44:38 | [diff] [blame] | 178 | EventAssign(&wakeup_event_, event_base_, wakeup_pipe_out_, |
tommi | 1666b61 | 2016-07-13 17:58:12 | [diff] [blame] | 179 | EV_READ | EV_PERSIST, OnWakeup, this); |
Danil Chapovalov | eb17524 | 2019-02-12 09:44:38 | [diff] [blame] | 180 | event_add(&wakeup_event_, 0); |
Markus Handell | ad5037b | 2021-05-07 13:02:36 | [diff] [blame] | 181 | thread_ = rtc::PlatformThread::SpawnJoinable( |
| 182 | [this] { |
| 183 | { |
| 184 | CurrentTaskQueueSetter set_current(this); |
| 185 | while (is_active_) |
| 186 | event_base_loop(event_base_, 0); |
| 187 | } |
| 188 | |
| 189 | for (TimerEvent* timer : pending_timers_) |
| 190 | delete timer; |
| 191 | }, |
| 192 | queue_name, rtc::ThreadAttributes().SetPriority(priority)); |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 193 | } |
| 194 | |
Danil Chapovalov | eb17524 | 2019-02-12 09:44:38 | [diff] [blame] | 195 | void TaskQueueLibevent::Delete() { |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 196 | RTC_DCHECK(!IsCurrent()); |
| 197 | struct timespec ts; |
| 198 | char message = kQuit; |
| 199 | while (write(wakeup_pipe_in_, &message, sizeof(message)) != sizeof(message)) { |
| 200 | // The queue is full, so we have no choice but to wait and retry. |
| 201 | RTC_CHECK_EQ(EAGAIN, errno); |
| 202 | ts.tv_sec = 0; |
| 203 | ts.tv_nsec = 1000000; |
| 204 | nanosleep(&ts, nullptr); |
| 205 | } |
| 206 | |
Markus Handell | ad5037b | 2021-05-07 13:02:36 | [diff] [blame] | 207 | thread_.Finalize(); |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 208 | |
Danil Chapovalov | eb17524 | 2019-02-12 09:44:38 | [diff] [blame] | 209 | event_del(&wakeup_event_); |
tommi | 8c80c6e | 2017-02-23 08:34:52 | [diff] [blame] | 210 | |
| 211 | IgnoreSigPipeSignalOnCurrentThread(); |
| 212 | |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 213 | close(wakeup_pipe_in_); |
| 214 | close(wakeup_pipe_out_); |
| 215 | wakeup_pipe_in_ = -1; |
| 216 | wakeup_pipe_out_ = -1; |
| 217 | |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 218 | event_base_free(event_base_); |
Danil Chapovalov | eb17524 | 2019-02-12 09:44:38 | [diff] [blame] | 219 | delete this; |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 220 | } |
| 221 | |
Danil Chapovalov | eb17524 | 2019-02-12 09:44:38 | [diff] [blame] | 222 | void TaskQueueLibevent::PostTask(std::unique_ptr<QueuedTask> task) { |
Danil Chapovalov | 00e71ef | 2019-06-11 16:01:56 | [diff] [blame] | 223 | { |
Markus Handell | 18523c3 | 2020-07-08 15:55:58 | [diff] [blame] | 224 | MutexLock lock(&pending_lock_); |
Steve Anton | 9a83dd7 | 2020-01-09 19:03:25 | [diff] [blame] | 225 | bool had_pending_tasks = !pending_.empty(); |
Danil Chapovalov | 00e71ef | 2019-06-11 16:01:56 | [diff] [blame] | 226 | pending_.push_back(std::move(task)); |
Steve Anton | 9a83dd7 | 2020-01-09 19:03:25 | [diff] [blame] | 227 | |
| 228 | // Only write to the pipe if there were no pending tasks before this one |
| 229 | // since the thread could be sleeping. If there were already pending tasks |
| 230 | // then we know there's either a pending write in the pipe or the thread has |
| 231 | // not yet processed the pending tasks. In either case, the thread will |
| 232 | // eventually wake up and process all pending tasks including this one. |
| 233 | if (had_pending_tasks) { |
| 234 | return; |
| 235 | } |
Danil Chapovalov | 00e71ef | 2019-06-11 16:01:56 | [diff] [blame] | 236 | } |
Steve Anton | 9a83dd7 | 2020-01-09 19:03:25 | [diff] [blame] | 237 | |
| 238 | // Note: This behvior outlined above ensures we never fill up the pipe write |
| 239 | // buffer since there will only ever be 1 byte pending. |
| 240 | char message = kRunTasks; |
| 241 | RTC_CHECK_EQ(write(wakeup_pipe_in_, &message, sizeof(message)), |
| 242 | sizeof(message)); |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 243 | } |
| 244 | |
Danil Chapovalov | eb17524 | 2019-02-12 09:44:38 | [diff] [blame] | 245 | void TaskQueueLibevent::PostDelayedTask(std::unique_ptr<QueuedTask> task, |
| 246 | uint32_t milliseconds) { |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 247 | if (IsCurrent()) { |
Danil Chapovalov | eb17524 | 2019-02-12 09:44:38 | [diff] [blame] | 248 | TimerEvent* timer = new TimerEvent(this, std::move(task)); |
| 249 | EventAssign(&timer->ev, event_base_, -1, 0, &TaskQueueLibevent::RunTimer, |
perkj | 650fdae | 2017-08-25 12:00:11 | [diff] [blame] | 250 | timer); |
Danil Chapovalov | eb17524 | 2019-02-12 09:44:38 | [diff] [blame] | 251 | pending_timers_.push_back(timer); |
kwiberg | 5b9746e | 2017-08-16 11:52:35 | [diff] [blame] | 252 | timeval tv = {rtc::dchecked_cast<int>(milliseconds / 1000), |
| 253 | rtc::dchecked_cast<int>(milliseconds % 1000) * 1000}; |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 254 | event_add(&timer->ev, &tv); |
| 255 | } else { |
Mirko Bonadei | 317a1f0 | 2019-09-17 15:06:18 | [diff] [blame] | 256 | PostTask(std::make_unique<SetTimerTask>(std::move(task), milliseconds)); |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 257 | } |
| 258 | } |
| 259 | |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 260 | // static |
Danil Chapovalov | eb17524 | 2019-02-12 09:44:38 | [diff] [blame] | 261 | void TaskQueueLibevent::OnWakeup(int socket, |
| 262 | short flags, // NOLINT |
| 263 | void* context) { |
| 264 | TaskQueueLibevent* me = static_cast<TaskQueueLibevent*>(context); |
| 265 | RTC_DCHECK(me->wakeup_pipe_out_ == socket); |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 266 | char buf; |
| 267 | RTC_CHECK(sizeof(buf) == read(socket, &buf, sizeof(buf))); |
| 268 | switch (buf) { |
| 269 | case kQuit: |
Danil Chapovalov | eb17524 | 2019-02-12 09:44:38 | [diff] [blame] | 270 | me->is_active_ = false; |
| 271 | event_base_loopbreak(me->event_base_); |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 272 | break; |
Steve Anton | 9a83dd7 | 2020-01-09 19:03:25 | [diff] [blame] | 273 | case kRunTasks: { |
| 274 | absl::InlinedVector<std::unique_ptr<QueuedTask>, 4> tasks; |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 275 | { |
Markus Handell | 18523c3 | 2020-07-08 15:55:58 | [diff] [blame] | 276 | MutexLock lock(&me->pending_lock_); |
Steve Anton | 9a83dd7 | 2020-01-09 19:03:25 | [diff] [blame] | 277 | tasks.swap(me->pending_); |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 278 | } |
Steve Anton | 9a83dd7 | 2020-01-09 19:03:25 | [diff] [blame] | 279 | RTC_DCHECK(!tasks.empty()); |
| 280 | for (auto& task : tasks) { |
| 281 | if (task->Run()) { |
| 282 | task.reset(); |
| 283 | } else { |
Artem Titov | 96e3b99 | 2021-07-26 14:03:14 | [diff] [blame] | 284 | // `false` means the task should *not* be deleted. |
Steve Anton | 9a83dd7 | 2020-01-09 19:03:25 | [diff] [blame] | 285 | task.release(); |
| 286 | } |
| 287 | } |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 288 | break; |
| 289 | } |
| 290 | default: |
Artem Titov | d325196 | 2021-11-15 15:57:07 | [diff] [blame] | 291 | RTC_DCHECK_NOTREACHED(); |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 292 | break; |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | // static |
Danil Chapovalov | eb17524 | 2019-02-12 09:44:38 | [diff] [blame] | 297 | void TaskQueueLibevent::RunTimer(int fd, |
| 298 | short flags, // NOLINT |
| 299 | void* context) { |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 300 | TimerEvent* timer = static_cast<TimerEvent*>(context); |
| 301 | if (!timer->task->Run()) |
| 302 | timer->task.release(); |
Danil Chapovalov | eb17524 | 2019-02-12 09:44:38 | [diff] [blame] | 303 | timer->task_queue->pending_timers_.remove(timer); |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 304 | delete timer; |
| 305 | } |
| 306 | |
Danil Chapovalov | eb17524 | 2019-02-12 09:44:38 | [diff] [blame] | 307 | class TaskQueueLibeventFactory final : public TaskQueueFactory { |
| 308 | public: |
| 309 | std::unique_ptr<TaskQueueBase, TaskQueueDeleter> CreateTaskQueue( |
| 310 | absl::string_view name, |
| 311 | Priority priority) const override { |
| 312 | return std::unique_ptr<TaskQueueBase, TaskQueueDeleter>( |
| 313 | new TaskQueueLibevent(name, |
| 314 | TaskQueuePriorityToThreadPriority(priority))); |
| 315 | } |
| 316 | }; |
| 317 | |
| 318 | } // namespace |
| 319 | |
| 320 | std::unique_ptr<TaskQueueFactory> CreateTaskQueueLibeventFactory() { |
Mirko Bonadei | 317a1f0 | 2019-09-17 15:06:18 | [diff] [blame] | 321 | return std::make_unique<TaskQueueLibeventFactory>(); |
perkj | 650fdae | 2017-08-25 12:00:11 | [diff] [blame] | 322 | } |
| 323 | |
Danil Chapovalov | eb17524 | 2019-02-12 09:44:38 | [diff] [blame] | 324 | } // namespace webrtc |