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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 11 | #include "rtc_base/task_queue.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 | |
| 25 | #include "base/third_party/libevent/event.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 26 | #include "rtc_base/checks.h" |
Danil Chapovalov | 02fddf6 | 2018-02-12 11:41:16 | [diff] [blame] | 27 | #include "rtc_base/criticalsection.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 28 | #include "rtc_base/logging.h" |
Karl Wiberg | e40468b | 2017-11-22 09:42:26 | [diff] [blame] | 29 | #include "rtc_base/numerics/safe_conversions.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 30 | #include "rtc_base/platform_thread.h" |
Yves Gerey | 988cc08 | 2018-10-23 10:03:01 | [diff] [blame] | 31 | #include "rtc_base/platform_thread_types.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 32 | #include "rtc_base/refcount.h" |
| 33 | #include "rtc_base/refcountedobject.h" |
Yves Gerey | 988cc08 | 2018-10-23 10:03:01 | [diff] [blame] | 34 | #include "rtc_base/scoped_ref_ptr.h" |
Niels Möller | a12c42a | 2018-07-25 14:05:48 | [diff] [blame] | 35 | #include "rtc_base/system/unused.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 36 | #include "rtc_base/task_queue_posix.h" |
Yves Gerey | 988cc08 | 2018-10-23 10:03:01 | [diff] [blame] | 37 | #include "rtc_base/thread_annotations.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 38 | #include "rtc_base/timeutils.h" |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 39 | |
| 40 | namespace rtc { |
| 41 | using internal::GetQueuePtrTls; |
| 42 | using internal::AutoSetCurrentQueuePtr; |
| 43 | |
| 44 | namespace { |
| 45 | static const char kQuit = 1; |
| 46 | static const char kRunTask = 2; |
tommi | 8c80c6e | 2017-02-23 08:34:52 | [diff] [blame] | 47 | static const char kRunReplyTask = 3; |
| 48 | |
tommi | c9bb791 | 2017-02-24 18:42:14 | [diff] [blame] | 49 | using Priority = TaskQueue::Priority; |
| 50 | |
tommi | 8c80c6e | 2017-02-23 08:34:52 | [diff] [blame] | 51 | // This ignores the SIGPIPE signal on the calling thread. |
| 52 | // This signal can be fired when trying to write() to a pipe that's being |
| 53 | // closed or while closing a pipe that's being written to. |
| 54 | // We can run into that situation (e.g. reply tasks that don't get a chance to |
| 55 | // run because the task queue is being deleted) so we ignore this signal and |
| 56 | // continue as normal. |
| 57 | // As a side note for this implementation, it would be great if we could safely |
| 58 | // restore the sigmask, but unfortunately the operation of restoring it, can |
| 59 | // itself actually cause SIGPIPE to be signaled :-| (e.g. on MacOS) |
| 60 | // The SIGPIPE signal by default causes the process to be terminated, so we |
| 61 | // don't want to risk that. |
| 62 | // An alternative to this approach is to ignore the signal for the whole |
| 63 | // process: |
| 64 | // signal(SIGPIPE, SIG_IGN); |
| 65 | void IgnoreSigPipeSignalOnCurrentThread() { |
| 66 | sigset_t sigpipe_mask; |
| 67 | sigemptyset(&sigpipe_mask); |
| 68 | sigaddset(&sigpipe_mask, SIGPIPE); |
| 69 | pthread_sigmask(SIG_BLOCK, &sigpipe_mask, nullptr); |
| 70 | } |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 71 | |
| 72 | struct TimerEvent { |
| 73 | explicit TimerEvent(std::unique_ptr<QueuedTask> task) |
| 74 | : task(std::move(task)) {} |
| 75 | ~TimerEvent() { event_del(&ev); } |
| 76 | event ev; |
| 77 | std::unique_ptr<QueuedTask> task; |
| 78 | }; |
| 79 | |
| 80 | bool SetNonBlocking(int fd) { |
| 81 | const int flags = fcntl(fd, F_GETFL); |
| 82 | RTC_CHECK(flags != -1); |
| 83 | return (flags & O_NONBLOCK) || fcntl(fd, F_SETFL, flags | O_NONBLOCK) != -1; |
| 84 | } |
tommi | 1666b61 | 2016-07-13 17:58:12 | [diff] [blame] | 85 | |
| 86 | // TODO(tommi): This is a hack to support two versions of libevent that we're |
| 87 | // compatible with. The method we really want to call is event_assign(), |
| 88 | // since event_set() has been marked as deprecated (and doesn't accept |
| 89 | // passing event_base__ as a parameter). However, the version of libevent |
| 90 | // that we have in Chromium, doesn't have event_assign(), so we need to call |
| 91 | // event_set() there. |
| 92 | void EventAssign(struct event* ev, |
| 93 | struct event_base* base, |
| 94 | int fd, |
| 95 | short events, |
| 96 | void (*callback)(int, short, void*), |
| 97 | void* arg) { |
| 98 | #if defined(_EVENT2_EVENT_H_) |
| 99 | RTC_CHECK_EQ(0, event_assign(ev, base, fd, events, callback, arg)); |
| 100 | #else |
| 101 | event_set(ev, fd, events, callback, arg); |
| 102 | RTC_CHECK_EQ(0, event_base_set(base, ev)); |
| 103 | #endif |
| 104 | } |
tommi | c9bb791 | 2017-02-24 18:42:14 | [diff] [blame] | 105 | |
| 106 | ThreadPriority TaskQueuePriorityToThreadPriority(Priority priority) { |
| 107 | switch (priority) { |
| 108 | case Priority::HIGH: |
| 109 | return kRealtimePriority; |
| 110 | case Priority::LOW: |
| 111 | return kLowPriority; |
| 112 | case Priority::NORMAL: |
| 113 | return kNormalPriority; |
| 114 | default: |
| 115 | RTC_NOTREACHED(); |
| 116 | break; |
| 117 | } |
| 118 | return kNormalPriority; |
| 119 | } |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 120 | } // namespace |
| 121 | |
perkj | 650fdae | 2017-08-25 12:00:11 | [diff] [blame] | 122 | class TaskQueue::Impl : public RefCountInterface { |
| 123 | public: |
| 124 | explicit Impl(const char* queue_name, |
| 125 | TaskQueue* queue, |
| 126 | Priority priority = Priority::NORMAL); |
| 127 | ~Impl() override; |
| 128 | |
| 129 | static TaskQueue::Impl* Current(); |
| 130 | static TaskQueue* CurrentQueue(); |
| 131 | |
| 132 | // Used for DCHECKing the current queue. |
perkj | 650fdae | 2017-08-25 12:00:11 | [diff] [blame] | 133 | bool IsCurrent() const; |
| 134 | |
| 135 | void PostTask(std::unique_ptr<QueuedTask> task); |
| 136 | void PostTaskAndReply(std::unique_ptr<QueuedTask> task, |
| 137 | std::unique_ptr<QueuedTask> reply, |
| 138 | TaskQueue::Impl* reply_queue); |
| 139 | |
| 140 | void PostDelayedTask(std::unique_ptr<QueuedTask> task, uint32_t milliseconds); |
| 141 | |
| 142 | private: |
| 143 | static void ThreadMain(void* context); |
| 144 | static void OnWakeup(int socket, short flags, void* context); // NOLINT |
| 145 | static void RunTask(int fd, short flags, void* context); // NOLINT |
| 146 | static void RunTimer(int fd, short flags, void* context); // NOLINT |
| 147 | |
| 148 | class ReplyTaskOwner; |
| 149 | class PostAndReplyTask; |
| 150 | class SetTimerTask; |
| 151 | |
| 152 | typedef RefCountedObject<ReplyTaskOwner> ReplyTaskOwnerRef; |
| 153 | |
| 154 | void PrepareReplyTask(scoped_refptr<ReplyTaskOwnerRef> reply_task); |
| 155 | |
| 156 | struct QueueContext; |
| 157 | TaskQueue* const queue_; |
| 158 | int wakeup_pipe_in_ = -1; |
| 159 | int wakeup_pipe_out_ = -1; |
| 160 | event_base* event_base_; |
| 161 | std::unique_ptr<event> wakeup_event_; |
| 162 | PlatformThread thread_; |
| 163 | rtc::CriticalSection pending_lock_; |
danilchap | 3c6abd2 | 2017-09-06 12:46:29 | [diff] [blame] | 164 | std::list<std::unique_ptr<QueuedTask>> pending_ RTC_GUARDED_BY(pending_lock_); |
perkj | 650fdae | 2017-08-25 12:00:11 | [diff] [blame] | 165 | std::list<scoped_refptr<ReplyTaskOwnerRef>> pending_replies_ |
danilchap | 3c6abd2 | 2017-09-06 12:46:29 | [diff] [blame] | 166 | RTC_GUARDED_BY(pending_lock_); |
perkj | 650fdae | 2017-08-25 12:00:11 | [diff] [blame] | 167 | }; |
| 168 | |
| 169 | struct TaskQueue::Impl::QueueContext { |
| 170 | explicit QueueContext(TaskQueue::Impl* q) : queue(q), is_active(true) {} |
| 171 | TaskQueue::Impl* queue; |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 172 | bool is_active; |
| 173 | // Holds a list of events pending timers for cleanup when the loop exits. |
| 174 | std::list<TimerEvent*> pending_timers_; |
| 175 | }; |
| 176 | |
tommi | 8c80c6e | 2017-02-23 08:34:52 | [diff] [blame] | 177 | // Posting a reply task is tricky business. This class owns the reply task |
| 178 | // and a reference to it is held by both the reply queue and the first task. |
| 179 | // Here's an outline of what happens when dealing with a reply task. |
| 180 | // * The ReplyTaskOwner owns the |reply_| task. |
| 181 | // * One ref owned by PostAndReplyTask |
| 182 | // * One ref owned by the reply TaskQueue |
| 183 | // * ReplyTaskOwner has a flag |run_task_| initially set to false. |
| 184 | // * ReplyTaskOwner has a method: HasOneRef() (provided by RefCountedObject). |
| 185 | // * After successfully running the original |task_|, PostAndReplyTask() calls |
| 186 | // set_should_run_task(). This sets |run_task_| to true. |
| 187 | // * In PostAndReplyTask's dtor: |
| 188 | // * It releases its reference to ReplyTaskOwner (important to do this first). |
| 189 | // * Sends (write()) a kRunReplyTask message to the reply queue's pipe. |
| 190 | // * PostAndReplyTask doesn't care if write() fails, but when it does: |
| 191 | // * The reply queue is gone. |
| 192 | // * ReplyTaskOwner has already been deleted and the reply task too. |
| 193 | // * If write() succeeds: |
| 194 | // * ReplyQueue receives the kRunReplyTask message |
| 195 | // * Goes through all pending tasks, finding the first that HasOneRef() |
| 196 | // * Calls ReplyTaskOwner::Run() |
| 197 | // * if set_should_run_task() was called, the reply task will be run |
| 198 | // * Release the reference to ReplyTaskOwner |
| 199 | // * ReplyTaskOwner and associated |reply_| are deleted. |
perkj | 650fdae | 2017-08-25 12:00:11 | [diff] [blame] | 200 | class TaskQueue::Impl::ReplyTaskOwner { |
tommi | 8c80c6e | 2017-02-23 08:34:52 | [diff] [blame] | 201 | public: |
| 202 | ReplyTaskOwner(std::unique_ptr<QueuedTask> reply) |
| 203 | : reply_(std::move(reply)) {} |
| 204 | |
| 205 | void Run() { |
| 206 | RTC_DCHECK(reply_); |
| 207 | if (run_task_) { |
| 208 | if (!reply_->Run()) |
| 209 | reply_.release(); |
| 210 | } |
| 211 | reply_.reset(); |
| 212 | } |
| 213 | |
| 214 | void set_should_run_task() { |
| 215 | RTC_DCHECK(!run_task_); |
| 216 | run_task_ = true; |
| 217 | } |
| 218 | |
| 219 | private: |
| 220 | std::unique_ptr<QueuedTask> reply_; |
| 221 | bool run_task_ = false; |
| 222 | }; |
| 223 | |
perkj | 650fdae | 2017-08-25 12:00:11 | [diff] [blame] | 224 | class TaskQueue::Impl::PostAndReplyTask : public QueuedTask { |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 225 | public: |
| 226 | PostAndReplyTask(std::unique_ptr<QueuedTask> task, |
| 227 | std::unique_ptr<QueuedTask> reply, |
perkj | 650fdae | 2017-08-25 12:00:11 | [diff] [blame] | 228 | TaskQueue::Impl* reply_queue, |
tommi | 8c80c6e | 2017-02-23 08:34:52 | [diff] [blame] | 229 | int reply_pipe) |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 230 | : task_(std::move(task)), |
tommi | 8c80c6e | 2017-02-23 08:34:52 | [diff] [blame] | 231 | reply_pipe_(reply_pipe), |
| 232 | reply_task_owner_( |
| 233 | new RefCountedObject<ReplyTaskOwner>(std::move(reply))) { |
| 234 | reply_queue->PrepareReplyTask(reply_task_owner_); |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | ~PostAndReplyTask() override { |
tommi | 8c80c6e | 2017-02-23 08:34:52 | [diff] [blame] | 238 | reply_task_owner_ = nullptr; |
| 239 | IgnoreSigPipeSignalOnCurrentThread(); |
| 240 | // Send a signal to the reply queue that the reply task can run now. |
| 241 | // Depending on whether |set_should_run_task()| was called by the |
| 242 | // PostAndReplyTask(), the reply task may or may not actually run. |
| 243 | // In either case, it will be deleted. |
| 244 | char message = kRunReplyTask; |
Tommi | 79a5560 | 2018-02-03 10:17:09 | [diff] [blame] | 245 | RTC_UNUSED(write(reply_pipe_, &message, sizeof(message))); |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | private: |
| 249 | bool Run() override { |
| 250 | if (!task_->Run()) |
| 251 | task_.release(); |
tommi | 8c80c6e | 2017-02-23 08:34:52 | [diff] [blame] | 252 | reply_task_owner_->set_should_run_task(); |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 253 | return true; |
| 254 | } |
| 255 | |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 256 | std::unique_ptr<QueuedTask> task_; |
tommi | 8c80c6e | 2017-02-23 08:34:52 | [diff] [blame] | 257 | int reply_pipe_; |
| 258 | scoped_refptr<RefCountedObject<ReplyTaskOwner>> reply_task_owner_; |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 259 | }; |
| 260 | |
perkj | 650fdae | 2017-08-25 12:00:11 | [diff] [blame] | 261 | class TaskQueue::Impl::SetTimerTask : public QueuedTask { |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 262 | public: |
| 263 | SetTimerTask(std::unique_ptr<QueuedTask> task, uint32_t milliseconds) |
| 264 | : task_(std::move(task)), |
| 265 | milliseconds_(milliseconds), |
| 266 | posted_(Time32()) {} |
| 267 | |
| 268 | private: |
| 269 | bool Run() override { |
| 270 | // Compensate for the time that has passed since construction |
| 271 | // and until we got here. |
| 272 | uint32_t post_time = Time32() - posted_; |
perkj | 650fdae | 2017-08-25 12:00:11 | [diff] [blame] | 273 | TaskQueue::Impl::Current()->PostDelayedTask( |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 274 | std::move(task_), |
| 275 | post_time > milliseconds_ ? 0 : milliseconds_ - post_time); |
| 276 | return true; |
| 277 | } |
| 278 | |
| 279 | std::unique_ptr<QueuedTask> task_; |
| 280 | const uint32_t milliseconds_; |
| 281 | const uint32_t posted_; |
| 282 | }; |
| 283 | |
perkj | 650fdae | 2017-08-25 12:00:11 | [diff] [blame] | 284 | TaskQueue::Impl::Impl(const char* queue_name, |
| 285 | TaskQueue* queue, |
| 286 | Priority priority /*= NORMAL*/) |
| 287 | : queue_(queue), |
| 288 | event_base_(event_base_new()), |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 289 | wakeup_event_(new event()), |
perkj | 650fdae | 2017-08-25 12:00:11 | [diff] [blame] | 290 | thread_(&TaskQueue::Impl::ThreadMain, |
tommi | c9bb791 | 2017-02-24 18:42:14 | [diff] [blame] | 291 | this, |
| 292 | queue_name, |
| 293 | TaskQueuePriorityToThreadPriority(priority)) { |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 294 | RTC_DCHECK(queue_name); |
| 295 | int fds[2]; |
| 296 | RTC_CHECK(pipe(fds) == 0); |
| 297 | SetNonBlocking(fds[0]); |
| 298 | SetNonBlocking(fds[1]); |
| 299 | wakeup_pipe_out_ = fds[0]; |
| 300 | wakeup_pipe_in_ = fds[1]; |
tommi | 8c80c6e | 2017-02-23 08:34:52 | [diff] [blame] | 301 | |
tommi | 1666b61 | 2016-07-13 17:58:12 | [diff] [blame] | 302 | EventAssign(wakeup_event_.get(), event_base_, wakeup_pipe_out_, |
| 303 | EV_READ | EV_PERSIST, OnWakeup, this); |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 304 | event_add(wakeup_event_.get(), 0); |
| 305 | thread_.Start(); |
| 306 | } |
| 307 | |
perkj | 650fdae | 2017-08-25 12:00:11 | [diff] [blame] | 308 | TaskQueue::Impl::~Impl() { |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 309 | RTC_DCHECK(!IsCurrent()); |
| 310 | struct timespec ts; |
| 311 | char message = kQuit; |
| 312 | while (write(wakeup_pipe_in_, &message, sizeof(message)) != sizeof(message)) { |
| 313 | // The queue is full, so we have no choice but to wait and retry. |
| 314 | RTC_CHECK_EQ(EAGAIN, errno); |
| 315 | ts.tv_sec = 0; |
| 316 | ts.tv_nsec = 1000000; |
| 317 | nanosleep(&ts, nullptr); |
| 318 | } |
| 319 | |
| 320 | thread_.Stop(); |
| 321 | |
| 322 | event_del(wakeup_event_.get()); |
tommi | 8c80c6e | 2017-02-23 08:34:52 | [diff] [blame] | 323 | |
| 324 | IgnoreSigPipeSignalOnCurrentThread(); |
| 325 | |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 326 | close(wakeup_pipe_in_); |
| 327 | close(wakeup_pipe_out_); |
| 328 | wakeup_pipe_in_ = -1; |
| 329 | wakeup_pipe_out_ = -1; |
| 330 | |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 331 | event_base_free(event_base_); |
| 332 | } |
| 333 | |
| 334 | // static |
perkj | 650fdae | 2017-08-25 12:00:11 | [diff] [blame] | 335 | TaskQueue::Impl* TaskQueue::Impl::Current() { |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 336 | QueueContext* ctx = |
| 337 | static_cast<QueueContext*>(pthread_getspecific(GetQueuePtrTls())); |
| 338 | return ctx ? ctx->queue : nullptr; |
| 339 | } |
| 340 | |
| 341 | // static |
perkj | 650fdae | 2017-08-25 12:00:11 | [diff] [blame] | 342 | TaskQueue* TaskQueue::Impl::CurrentQueue() { |
| 343 | TaskQueue::Impl* current = Current(); |
| 344 | if (current) { |
| 345 | return current->queue_; |
| 346 | } |
| 347 | return nullptr; |
| 348 | } |
| 349 | |
perkj | 650fdae | 2017-08-25 12:00:11 | [diff] [blame] | 350 | bool TaskQueue::Impl::IsCurrent() const { |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 351 | return IsThreadRefEqual(thread_.GetThreadRef(), CurrentThreadRef()); |
| 352 | } |
| 353 | |
perkj | 650fdae | 2017-08-25 12:00:11 | [diff] [blame] | 354 | void TaskQueue::Impl::PostTask(std::unique_ptr<QueuedTask> task) { |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 355 | RTC_DCHECK(task.get()); |
| 356 | // libevent isn't thread safe. This means that we can't use methods such |
| 357 | // as event_base_once to post tasks to the worker thread from a different |
| 358 | // thread. However, we can use it when posting from the worker thread itself. |
| 359 | if (IsCurrent()) { |
perkj | 650fdae | 2017-08-25 12:00:11 | [diff] [blame] | 360 | if (event_base_once(event_base_, -1, EV_TIMEOUT, &TaskQueue::Impl::RunTask, |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 361 | task.get(), nullptr) == 0) { |
| 362 | task.release(); |
| 363 | } |
| 364 | } else { |
| 365 | QueuedTask* task_id = task.get(); // Only used for comparison. |
| 366 | { |
| 367 | CritScope lock(&pending_lock_); |
| 368 | pending_.push_back(std::move(task)); |
| 369 | } |
| 370 | char message = kRunTask; |
| 371 | if (write(wakeup_pipe_in_, &message, sizeof(message)) != sizeof(message)) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 372 | RTC_LOG(WARNING) << "Failed to queue task."; |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 373 | CritScope lock(&pending_lock_); |
| 374 | pending_.remove_if([task_id](std::unique_ptr<QueuedTask>& t) { |
| 375 | return t.get() == task_id; |
| 376 | }); |
| 377 | } |
| 378 | } |
| 379 | } |
| 380 | |
perkj | 650fdae | 2017-08-25 12:00:11 | [diff] [blame] | 381 | void TaskQueue::Impl::PostDelayedTask(std::unique_ptr<QueuedTask> task, |
| 382 | uint32_t milliseconds) { |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 383 | if (IsCurrent()) { |
| 384 | TimerEvent* timer = new TimerEvent(std::move(task)); |
perkj | 650fdae | 2017-08-25 12:00:11 | [diff] [blame] | 385 | EventAssign(&timer->ev, event_base_, -1, 0, &TaskQueue::Impl::RunTimer, |
| 386 | timer); |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 387 | QueueContext* ctx = |
| 388 | static_cast<QueueContext*>(pthread_getspecific(GetQueuePtrTls())); |
| 389 | ctx->pending_timers_.push_back(timer); |
kwiberg | 5b9746e | 2017-08-16 11:52:35 | [diff] [blame] | 390 | timeval tv = {rtc::dchecked_cast<int>(milliseconds / 1000), |
| 391 | rtc::dchecked_cast<int>(milliseconds % 1000) * 1000}; |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 392 | event_add(&timer->ev, &tv); |
| 393 | } else { |
| 394 | PostTask(std::unique_ptr<QueuedTask>( |
| 395 | new SetTimerTask(std::move(task), milliseconds))); |
| 396 | } |
| 397 | } |
| 398 | |
perkj | 650fdae | 2017-08-25 12:00:11 | [diff] [blame] | 399 | void TaskQueue::Impl::PostTaskAndReply(std::unique_ptr<QueuedTask> task, |
| 400 | std::unique_ptr<QueuedTask> reply, |
| 401 | TaskQueue::Impl* reply_queue) { |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 402 | std::unique_ptr<QueuedTask> wrapper_task( |
tommi | 8c80c6e | 2017-02-23 08:34:52 | [diff] [blame] | 403 | new PostAndReplyTask(std::move(task), std::move(reply), reply_queue, |
| 404 | reply_queue->wakeup_pipe_in_)); |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 405 | PostTask(std::move(wrapper_task)); |
| 406 | } |
| 407 | |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 408 | // static |
perkj | 650fdae | 2017-08-25 12:00:11 | [diff] [blame] | 409 | void TaskQueue::Impl::ThreadMain(void* context) { |
| 410 | TaskQueue::Impl* me = static_cast<TaskQueue::Impl*>(context); |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 411 | |
| 412 | QueueContext queue_context(me); |
| 413 | pthread_setspecific(GetQueuePtrTls(), &queue_context); |
| 414 | |
| 415 | while (queue_context.is_active) |
| 416 | event_base_loop(me->event_base_, 0); |
| 417 | |
| 418 | pthread_setspecific(GetQueuePtrTls(), nullptr); |
| 419 | |
| 420 | for (TimerEvent* timer : queue_context.pending_timers_) |
| 421 | delete timer; |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | // static |
perkj | 650fdae | 2017-08-25 12:00:11 | [diff] [blame] | 425 | void TaskQueue::Impl::OnWakeup(int socket, |
| 426 | short flags, |
| 427 | void* context) { // NOLINT |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 428 | QueueContext* ctx = |
| 429 | static_cast<QueueContext*>(pthread_getspecific(GetQueuePtrTls())); |
| 430 | RTC_DCHECK(ctx->queue->wakeup_pipe_out_ == socket); |
| 431 | char buf; |
| 432 | RTC_CHECK(sizeof(buf) == read(socket, &buf, sizeof(buf))); |
| 433 | switch (buf) { |
| 434 | case kQuit: |
| 435 | ctx->is_active = false; |
| 436 | event_base_loopbreak(ctx->queue->event_base_); |
| 437 | break; |
| 438 | case kRunTask: { |
| 439 | std::unique_ptr<QueuedTask> task; |
| 440 | { |
| 441 | CritScope lock(&ctx->queue->pending_lock_); |
| 442 | RTC_DCHECK(!ctx->queue->pending_.empty()); |
| 443 | task = std::move(ctx->queue->pending_.front()); |
| 444 | ctx->queue->pending_.pop_front(); |
| 445 | RTC_DCHECK(task.get()); |
| 446 | } |
| 447 | if (!task->Run()) |
| 448 | task.release(); |
| 449 | break; |
| 450 | } |
tommi | 8c80c6e | 2017-02-23 08:34:52 | [diff] [blame] | 451 | case kRunReplyTask: { |
| 452 | scoped_refptr<ReplyTaskOwnerRef> reply_task; |
| 453 | { |
| 454 | CritScope lock(&ctx->queue->pending_lock_); |
| 455 | for (auto it = ctx->queue->pending_replies_.begin(); |
| 456 | it != ctx->queue->pending_replies_.end(); ++it) { |
| 457 | if ((*it)->HasOneRef()) { |
| 458 | reply_task = std::move(*it); |
| 459 | ctx->queue->pending_replies_.erase(it); |
| 460 | break; |
| 461 | } |
| 462 | } |
| 463 | } |
| 464 | reply_task->Run(); |
| 465 | break; |
| 466 | } |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 467 | default: |
| 468 | RTC_NOTREACHED(); |
| 469 | break; |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | // static |
perkj | 650fdae | 2017-08-25 12:00:11 | [diff] [blame] | 474 | void TaskQueue::Impl::RunTask(int fd, short flags, void* context) { // NOLINT |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 475 | auto* task = static_cast<QueuedTask*>(context); |
| 476 | if (task->Run()) |
| 477 | delete task; |
| 478 | } |
| 479 | |
| 480 | // static |
perkj | 650fdae | 2017-08-25 12:00:11 | [diff] [blame] | 481 | void TaskQueue::Impl::RunTimer(int fd, short flags, void* context) { // NOLINT |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 482 | TimerEvent* timer = static_cast<TimerEvent*>(context); |
| 483 | if (!timer->task->Run()) |
| 484 | timer->task.release(); |
| 485 | QueueContext* ctx = |
| 486 | static_cast<QueueContext*>(pthread_getspecific(GetQueuePtrTls())); |
| 487 | ctx->pending_timers_.remove(timer); |
| 488 | delete timer; |
| 489 | } |
| 490 | |
perkj | 650fdae | 2017-08-25 12:00:11 | [diff] [blame] | 491 | void TaskQueue::Impl::PrepareReplyTask( |
| 492 | scoped_refptr<ReplyTaskOwnerRef> reply_task) { |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 493 | RTC_DCHECK(reply_task); |
| 494 | CritScope lock(&pending_lock_); |
tommi | 8c80c6e | 2017-02-23 08:34:52 | [diff] [blame] | 495 | pending_replies_.push_back(std::move(reply_task)); |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 496 | } |
| 497 | |
perkj | 650fdae | 2017-08-25 12:00:11 | [diff] [blame] | 498 | TaskQueue::TaskQueue(const char* queue_name, Priority priority) |
| 499 | : impl_(new RefCountedObject<TaskQueue::Impl>(queue_name, this, priority)) { |
| 500 | } |
| 501 | |
| 502 | TaskQueue::~TaskQueue() {} |
| 503 | |
| 504 | // static |
| 505 | TaskQueue* TaskQueue::Current() { |
| 506 | return TaskQueue::Impl::CurrentQueue(); |
| 507 | } |
| 508 | |
| 509 | // Used for DCHECKing the current queue. |
perkj | 650fdae | 2017-08-25 12:00:11 | [diff] [blame] | 510 | bool TaskQueue::IsCurrent() const { |
| 511 | return impl_->IsCurrent(); |
| 512 | } |
| 513 | |
| 514 | void TaskQueue::PostTask(std::unique_ptr<QueuedTask> task) { |
| 515 | return TaskQueue::impl_->PostTask(std::move(task)); |
| 516 | } |
| 517 | |
| 518 | void TaskQueue::PostTaskAndReply(std::unique_ptr<QueuedTask> task, |
| 519 | std::unique_ptr<QueuedTask> reply, |
| 520 | TaskQueue* reply_queue) { |
| 521 | return TaskQueue::impl_->PostTaskAndReply(std::move(task), std::move(reply), |
| 522 | reply_queue->impl_.get()); |
| 523 | } |
| 524 | |
| 525 | void TaskQueue::PostTaskAndReply(std::unique_ptr<QueuedTask> task, |
| 526 | std::unique_ptr<QueuedTask> reply) { |
| 527 | return TaskQueue::impl_->PostTaskAndReply(std::move(task), std::move(reply), |
| 528 | impl_.get()); |
| 529 | } |
| 530 | |
| 531 | void TaskQueue::PostDelayedTask(std::unique_ptr<QueuedTask> task, |
| 532 | uint32_t milliseconds) { |
| 533 | return TaskQueue::impl_->PostDelayedTask(std::move(task), milliseconds); |
| 534 | } |
| 535 | |
tommi | c06b133 | 2016-05-14 18:31:40 | [diff] [blame] | 536 | } // namespace rtc |