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