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