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 | 30c2a31 | 2022-07-19 12:12:43 | [diff] [blame] | 27 | #include "absl/functional/any_invocable.h" |
Danil Chapovalov | eb17524 | 2019-02-12 09:44:38 | [diff] [blame] | 28 | #include "absl/strings/string_view.h" |
Danil Chapovalov | eb17524 | 2019-02-12 09:44:38 | [diff] [blame] | 29 | #include "api/task_queue/task_queue_base.h" |
Danil Chapovalov | 30c2a31 | 2022-07-19 12:12:43 | [diff] [blame] | 30 | #include "api/units/time_delta.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" |
Byoungchan Lee | d69a726 | 2022-06-23 13:06:00 | [diff] [blame] | 39 | #include "third_party/libevent/event.h" |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 40 | |
Danil Chapovalov | eb17524 | 2019-02-12 09:44:38 | [diff] [blame] | 41 | namespace webrtc { |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 42 | namespace { |
Danil Chapovalov | eb17524 | 2019-02-12 09:44:38 | [diff] [blame] | 43 | constexpr char kQuit = 1; |
Steve Anton | 9a83dd7 | 2020-01-09 19:03:25 | [diff] [blame] | 44 | constexpr char kRunTasks = 2; |
tommi | 8c80c6e | 2017-02-23 08:34:52 | [diff] [blame] | 45 | |
Danil Chapovalov | eb17524 | 2019-02-12 09:44:38 | [diff] [blame] | 46 | using Priority = TaskQueueFactory::Priority; |
tommi | c9bb791 | 2017-02-24 18:42:14 | [diff] [blame] | 47 | |
tommi | 8c80c6e | 2017-02-23 08:34:52 | [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 14:46:58 | [diff] [blame] | 51 | // We can run into that situation so we ignore this signal and continue as |
| 52 | // normal. |
tommi | 8c80c6e | 2017-02-23 08:34:52 | [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 18:31:40 | [diff] [blame] | 67 | |
tommi | c06b133 | 2016-05-14 18:31:40 | [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 17:58:12 | [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 18:42:14 | [diff] [blame] | 93 | |
Danil Chapovalov | eb17524 | 2019-02-12 09:44:38 | [diff] [blame] | 94 | rtc::ThreadPriority TaskQueuePriorityToThreadPriority(Priority priority) { |
tommi | c9bb791 | 2017-02-24 18:42:14 | [diff] [blame] | 95 | switch (priority) { |
| 96 | case Priority::HIGH: |
Markus Handell | ad5037b | 2021-05-07 13:02:36 | [diff] [blame] | 97 | return rtc::ThreadPriority::kRealtime; |
tommi | c9bb791 | 2017-02-24 18:42:14 | [diff] [blame] | 98 | case Priority::LOW: |
Markus Handell | ad5037b | 2021-05-07 13:02:36 | [diff] [blame] | 99 | return rtc::ThreadPriority::kLow; |
tommi | c9bb791 | 2017-02-24 18:42:14 | [diff] [blame] | 100 | case Priority::NORMAL: |
Markus Handell | ad5037b | 2021-05-07 13:02:36 | [diff] [blame] | 101 | return rtc::ThreadPriority::kNormal; |
tommi | c9bb791 | 2017-02-24 18:42:14 | [diff] [blame] | 102 | } |
tommi | c9bb791 | 2017-02-24 18:42:14 | [diff] [blame] | 103 | } |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 104 | |
Danil Chapovalov | eb17524 | 2019-02-12 09:44:38 | [diff] [blame] | 105 | class TaskQueueLibevent final : public TaskQueueBase { |
perkj | 650fdae | 2017-08-25 12:00:11 | [diff] [blame] | 106 | public: |
Danil Chapovalov | eb17524 | 2019-02-12 09:44:38 | [diff] [blame] | 107 | TaskQueueLibevent(absl::string_view queue_name, rtc::ThreadPriority priority); |
perkj | 650fdae | 2017-08-25 12:00:11 | [diff] [blame] | 108 | |
Danil Chapovalov | eb17524 | 2019-02-12 09:44:38 | [diff] [blame] | 109 | void Delete() override; |
Markus Handell | 2a256c8 | 2023-02-27 11:41:39 | [diff] [blame] | 110 | |
| 111 | protected: |
| 112 | void PostTaskImpl(absl::AnyInvocable<void() &&> task, |
| 113 | const PostTaskTraits& traits, |
| 114 | const Location& location) override; |
| 115 | void PostDelayedTaskImpl(absl::AnyInvocable<void() &&> task, |
| 116 | TimeDelta delay, |
| 117 | const PostDelayedTaskTraits& traits, |
| 118 | const Location& location) override; |
perkj | 650fdae | 2017-08-25 12:00:11 | [diff] [blame] | 119 | |
| 120 | private: |
Danil Chapovalov | eb17524 | 2019-02-12 09:44:38 | [diff] [blame] | 121 | struct TimerEvent; |
| 122 | |
Danil Chapovalov | 30c2a31 | 2022-07-19 12:12:43 | [diff] [blame] | 123 | void PostDelayedTaskOnTaskQueue(absl::AnyInvocable<void() &&> task, |
| 124 | TimeDelta delay); |
| 125 | |
Danil Chapovalov | eb17524 | 2019-02-12 09:44:38 | [diff] [blame] | 126 | ~TaskQueueLibevent() override = default; |
| 127 | |
perkj | 650fdae | 2017-08-25 12:00:11 | [diff] [blame] | 128 | static void OnWakeup(int socket, short flags, void* context); // NOLINT |
perkj | 650fdae | 2017-08-25 12:00:11 | [diff] [blame] | 129 | static void RunTimer(int fd, short flags, void* context); // NOLINT |
| 130 | |
Danil Chapovalov | eb17524 | 2019-02-12 09:44:38 | [diff] [blame] | 131 | bool is_active_ = true; |
perkj | 650fdae | 2017-08-25 12:00:11 | [diff] [blame] | 132 | int wakeup_pipe_in_ = -1; |
| 133 | int wakeup_pipe_out_ = -1; |
| 134 | event_base* event_base_; |
Danil Chapovalov | eb17524 | 2019-02-12 09:44:38 | [diff] [blame] | 135 | event wakeup_event_; |
| 136 | rtc::PlatformThread thread_; |
Markus Handell | 18523c3 | 2020-07-08 15:55:58 | [diff] [blame] | 137 | Mutex pending_lock_; |
Danil Chapovalov | 30c2a31 | 2022-07-19 12:12:43 | [diff] [blame] | 138 | absl::InlinedVector<absl::AnyInvocable<void() &&>, 4> pending_ |
Steve Anton | 9a83dd7 | 2020-01-09 19:03:25 | [diff] [blame] | 139 | RTC_GUARDED_BY(pending_lock_); |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 140 | // Holds a list of events pending timers for cleanup when the loop exits. |
| 141 | std::list<TimerEvent*> pending_timers_; |
| 142 | }; |
| 143 | |
Danil Chapovalov | eb17524 | 2019-02-12 09:44:38 | [diff] [blame] | 144 | struct TaskQueueLibevent::TimerEvent { |
Danil Chapovalov | 30c2a31 | 2022-07-19 12:12:43 | [diff] [blame] | 145 | TimerEvent(TaskQueueLibevent* task_queue, absl::AnyInvocable<void() &&> task) |
Danil Chapovalov | eb17524 | 2019-02-12 09:44:38 | [diff] [blame] | 146 | : task_queue(task_queue), task(std::move(task)) {} |
| 147 | ~TimerEvent() { event_del(&ev); } |
| 148 | |
| 149 | event ev; |
| 150 | TaskQueueLibevent* task_queue; |
Danil Chapovalov | 30c2a31 | 2022-07-19 12:12:43 | [diff] [blame] | 151 | absl::AnyInvocable<void() &&> task; |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 152 | }; |
| 153 | |
Danil Chapovalov | eb17524 | 2019-02-12 09:44:38 | [diff] [blame] | 154 | TaskQueueLibevent::TaskQueueLibevent(absl::string_view queue_name, |
| 155 | rtc::ThreadPriority priority) |
Markus Handell | ad5037b | 2021-05-07 13:02:36 | [diff] [blame] | 156 | : event_base_(event_base_new()) { |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 157 | int fds[2]; |
| 158 | RTC_CHECK(pipe(fds) == 0); |
| 159 | SetNonBlocking(fds[0]); |
| 160 | SetNonBlocking(fds[1]); |
| 161 | wakeup_pipe_out_ = fds[0]; |
| 162 | wakeup_pipe_in_ = fds[1]; |
tommi | 8c80c6e | 2017-02-23 08:34:52 | [diff] [blame] | 163 | |
Danil Chapovalov | eb17524 | 2019-02-12 09:44:38 | [diff] [blame] | 164 | EventAssign(&wakeup_event_, event_base_, wakeup_pipe_out_, |
tommi | 1666b61 | 2016-07-13 17:58:12 | [diff] [blame] | 165 | EV_READ | EV_PERSIST, OnWakeup, this); |
Danil Chapovalov | eb17524 | 2019-02-12 09:44:38 | [diff] [blame] | 166 | event_add(&wakeup_event_, 0); |
Markus Handell | ad5037b | 2021-05-07 13:02:36 | [diff] [blame] | 167 | thread_ = rtc::PlatformThread::SpawnJoinable( |
| 168 | [this] { |
| 169 | { |
| 170 | CurrentTaskQueueSetter set_current(this); |
| 171 | while (is_active_) |
| 172 | event_base_loop(event_base_, 0); |
Markus Handell | ad5037b | 2021-05-07 13:02:36 | [diff] [blame] | 173 | |
Markus Handell | 82da932 | 2022-12-16 14:50:24 | [diff] [blame] | 174 | // Ensure remaining deleted tasks are destroyed with Current() set up |
| 175 | // to this task queue. |
| 176 | absl::InlinedVector<absl::AnyInvocable<void() &&>, 4> pending; |
| 177 | MutexLock lock(&pending_lock_); |
| 178 | pending_.swap(pending); |
| 179 | } |
Markus Handell | ad5037b | 2021-05-07 13:02:36 | [diff] [blame] | 180 | for (TimerEvent* timer : pending_timers_) |
| 181 | delete timer; |
Markus Handell | 82da932 | 2022-12-16 14:50:24 | [diff] [blame] | 182 | |
| 183 | #if RTC_DCHECK_IS_ON |
| 184 | MutexLock lock(&pending_lock_); |
| 185 | RTC_DCHECK(pending_.empty()); |
| 186 | #endif |
Markus Handell | ad5037b | 2021-05-07 13:02:36 | [diff] [blame] | 187 | }, |
| 188 | queue_name, rtc::ThreadAttributes().SetPriority(priority)); |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 189 | } |
| 190 | |
Danil Chapovalov | eb17524 | 2019-02-12 09:44:38 | [diff] [blame] | 191 | void TaskQueueLibevent::Delete() { |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 192 | RTC_DCHECK(!IsCurrent()); |
| 193 | struct timespec ts; |
| 194 | char message = kQuit; |
| 195 | while (write(wakeup_pipe_in_, &message, sizeof(message)) != sizeof(message)) { |
| 196 | // The queue is full, so we have no choice but to wait and retry. |
| 197 | RTC_CHECK_EQ(EAGAIN, errno); |
| 198 | ts.tv_sec = 0; |
| 199 | ts.tv_nsec = 1000000; |
| 200 | nanosleep(&ts, nullptr); |
| 201 | } |
| 202 | |
Markus Handell | ad5037b | 2021-05-07 13:02:36 | [diff] [blame] | 203 | thread_.Finalize(); |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 204 | |
Danil Chapovalov | eb17524 | 2019-02-12 09:44:38 | [diff] [blame] | 205 | event_del(&wakeup_event_); |
tommi | 8c80c6e | 2017-02-23 08:34:52 | [diff] [blame] | 206 | |
| 207 | IgnoreSigPipeSignalOnCurrentThread(); |
| 208 | |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 209 | close(wakeup_pipe_in_); |
| 210 | close(wakeup_pipe_out_); |
| 211 | wakeup_pipe_in_ = -1; |
| 212 | wakeup_pipe_out_ = -1; |
| 213 | |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 214 | event_base_free(event_base_); |
Danil Chapovalov | eb17524 | 2019-02-12 09:44:38 | [diff] [blame] | 215 | delete this; |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 216 | } |
| 217 | |
Markus Handell | 2a256c8 | 2023-02-27 11:41:39 | [diff] [blame] | 218 | void TaskQueueLibevent::PostTaskImpl(absl::AnyInvocable<void() &&> task, |
| 219 | const PostTaskTraits& traits, |
| 220 | const Location& location) { |
Danil Chapovalov | 00e71ef | 2019-06-11 16:01:56 | [diff] [blame] | 221 | { |
Markus Handell | 18523c3 | 2020-07-08 15:55:58 | [diff] [blame] | 222 | MutexLock lock(&pending_lock_); |
Steve Anton | 9a83dd7 | 2020-01-09 19:03:25 | [diff] [blame] | 223 | bool had_pending_tasks = !pending_.empty(); |
Danil Chapovalov | 00e71ef | 2019-06-11 16:01:56 | [diff] [blame] | 224 | pending_.push_back(std::move(task)); |
Steve Anton | 9a83dd7 | 2020-01-09 19:03:25 | [diff] [blame] | 225 | |
| 226 | // Only write to the pipe if there were no pending tasks before this one |
| 227 | // since the thread could be sleeping. If there were already pending tasks |
| 228 | // then we know there's either a pending write in the pipe or the thread has |
| 229 | // not yet processed the pending tasks. In either case, the thread will |
| 230 | // eventually wake up and process all pending tasks including this one. |
| 231 | if (had_pending_tasks) { |
| 232 | return; |
| 233 | } |
Danil Chapovalov | 00e71ef | 2019-06-11 16:01:56 | [diff] [blame] | 234 | } |
Steve Anton | 9a83dd7 | 2020-01-09 19:03:25 | [diff] [blame] | 235 | |
| 236 | // Note: This behvior outlined above ensures we never fill up the pipe write |
| 237 | // buffer since there will only ever be 1 byte pending. |
| 238 | char message = kRunTasks; |
| 239 | RTC_CHECK_EQ(write(wakeup_pipe_in_, &message, sizeof(message)), |
| 240 | sizeof(message)); |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 241 | } |
| 242 | |
Danil Chapovalov | 30c2a31 | 2022-07-19 12:12:43 | [diff] [blame] | 243 | void TaskQueueLibevent::PostDelayedTaskOnTaskQueue( |
| 244 | absl::AnyInvocable<void() &&> task, |
| 245 | TimeDelta delay) { |
| 246 | // libevent api is not thread safe by default, thus event_add need to be |
| 247 | // called on the `thread_`. |
| 248 | RTC_DCHECK(IsCurrent()); |
| 249 | |
| 250 | TimerEvent* timer = new TimerEvent(this, std::move(task)); |
| 251 | EventAssign(&timer->ev, event_base_, -1, 0, &TaskQueueLibevent::RunTimer, |
| 252 | timer); |
| 253 | pending_timers_.push_back(timer); |
| 254 | timeval tv = {.tv_sec = rtc::dchecked_cast<int>(delay.us() / 1'000'000), |
| 255 | .tv_usec = rtc::dchecked_cast<int>(delay.us() % 1'000'000)}; |
| 256 | event_add(&timer->ev, &tv); |
| 257 | } |
| 258 | |
Markus Handell | 2a256c8 | 2023-02-27 11:41:39 | [diff] [blame] | 259 | void TaskQueueLibevent::PostDelayedTaskImpl(absl::AnyInvocable<void() &&> task, |
| 260 | TimeDelta delay, |
| 261 | const PostDelayedTaskTraits& traits, |
| 262 | const Location& location) { |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 263 | if (IsCurrent()) { |
Danil Chapovalov | 30c2a31 | 2022-07-19 12:12:43 | [diff] [blame] | 264 | PostDelayedTaskOnTaskQueue(std::move(task), delay); |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 265 | } else { |
Danil Chapovalov | 30c2a31 | 2022-07-19 12:12:43 | [diff] [blame] | 266 | int64_t posted_us = rtc::TimeMicros(); |
| 267 | PostTask([posted_us, delay, task = std::move(task), this]() mutable { |
| 268 | // Compensate for the time that has passed since the posting. |
| 269 | TimeDelta post_time = TimeDelta::Micros(rtc::TimeMicros() - posted_us); |
| 270 | PostDelayedTaskOnTaskQueue( |
| 271 | std::move(task), std::max(delay - post_time, TimeDelta::Zero())); |
| 272 | }); |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 273 | } |
| 274 | } |
| 275 | |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 276 | // static |
Danil Chapovalov | eb17524 | 2019-02-12 09:44:38 | [diff] [blame] | 277 | void TaskQueueLibevent::OnWakeup(int socket, |
| 278 | short flags, // NOLINT |
| 279 | void* context) { |
| 280 | TaskQueueLibevent* me = static_cast<TaskQueueLibevent*>(context); |
| 281 | RTC_DCHECK(me->wakeup_pipe_out_ == socket); |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 282 | char buf; |
| 283 | RTC_CHECK(sizeof(buf) == read(socket, &buf, sizeof(buf))); |
| 284 | switch (buf) { |
| 285 | case kQuit: |
Danil Chapovalov | eb17524 | 2019-02-12 09:44:38 | [diff] [blame] | 286 | me->is_active_ = false; |
| 287 | event_base_loopbreak(me->event_base_); |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 288 | break; |
Steve Anton | 9a83dd7 | 2020-01-09 19:03:25 | [diff] [blame] | 289 | case kRunTasks: { |
Danil Chapovalov | 30c2a31 | 2022-07-19 12:12:43 | [diff] [blame] | 290 | absl::InlinedVector<absl::AnyInvocable<void() &&>, 4> tasks; |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 291 | { |
Markus Handell | 18523c3 | 2020-07-08 15:55:58 | [diff] [blame] | 292 | MutexLock lock(&me->pending_lock_); |
Steve Anton | 9a83dd7 | 2020-01-09 19:03:25 | [diff] [blame] | 293 | tasks.swap(me->pending_); |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 294 | } |
Steve Anton | 9a83dd7 | 2020-01-09 19:03:25 | [diff] [blame] | 295 | RTC_DCHECK(!tasks.empty()); |
| 296 | for (auto& task : tasks) { |
Danil Chapovalov | 30c2a31 | 2022-07-19 12:12:43 | [diff] [blame] | 297 | std::move(task)(); |
| 298 | // Prefer to delete the `task` before running the next one. |
| 299 | task = nullptr; |
Steve Anton | 9a83dd7 | 2020-01-09 19:03:25 | [diff] [blame] | 300 | } |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 301 | break; |
| 302 | } |
| 303 | default: |
Artem Titov | d325196 | 2021-11-15 15:57:07 | [diff] [blame] | 304 | RTC_DCHECK_NOTREACHED(); |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 305 | break; |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | // static |
Danil Chapovalov | eb17524 | 2019-02-12 09:44:38 | [diff] [blame] | 310 | void TaskQueueLibevent::RunTimer(int fd, |
| 311 | short flags, // NOLINT |
| 312 | void* context) { |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 313 | TimerEvent* timer = static_cast<TimerEvent*>(context); |
Danil Chapovalov | 30c2a31 | 2022-07-19 12:12:43 | [diff] [blame] | 314 | std::move(timer->task)(); |
Danil Chapovalov | eb17524 | 2019-02-12 09:44:38 | [diff] [blame] | 315 | timer->task_queue->pending_timers_.remove(timer); |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 316 | delete timer; |
| 317 | } |
| 318 | |
Danil Chapovalov | eb17524 | 2019-02-12 09:44:38 | [diff] [blame] | 319 | class TaskQueueLibeventFactory final : public TaskQueueFactory { |
| 320 | public: |
| 321 | std::unique_ptr<TaskQueueBase, TaskQueueDeleter> CreateTaskQueue( |
| 322 | absl::string_view name, |
| 323 | Priority priority) const override { |
| 324 | return std::unique_ptr<TaskQueueBase, TaskQueueDeleter>( |
| 325 | new TaskQueueLibevent(name, |
| 326 | TaskQueuePriorityToThreadPriority(priority))); |
| 327 | } |
| 328 | }; |
| 329 | |
| 330 | } // namespace |
| 331 | |
| 332 | std::unique_ptr<TaskQueueFactory> CreateTaskQueueLibeventFactory() { |
Mirko Bonadei | 317a1f0 | 2019-09-17 15:06:18 | [diff] [blame] | 333 | return std::make_unique<TaskQueueLibeventFactory>(); |
perkj | 650fdae | 2017-08-25 12:00:11 | [diff] [blame] | 334 | } |
| 335 | |
Danil Chapovalov | eb17524 | 2019-02-12 09:44:38 | [diff] [blame] | 336 | } // namespace webrtc |