blob: fade0057cea73bcdc901be5a9a06a365b6c0aac3 [file] [log] [blame]
Danil Chapovalov348b08a2019-01-17 13:07:25 +01001/*
2 * Copyright 2019 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#ifndef API_TASK_QUEUE_TASK_QUEUE_BASE_H_
11#define API_TASK_QUEUE_TASK_QUEUE_BASE_H_
12
13#include <memory>
14
15#include "api/task_queue/queued_task.h"
Danil Chapovalov4423c362019-03-06 18:41:39 +010016#include "rtc_base/thread_annotations.h"
Danil Chapovalovd00405f2019-02-25 15:06:13 +010017
Yves Gerey42d8c932019-03-08 16:14:47 +000018// TODO(bugs.webrtc.org/10191): Remove when
19// rtc::TaskQueue* rtc::TaskQueue::Current() is unused.
20namespace rtc {
21class TaskQueue;
22} // namespace rtc
23
Danil Chapovalov348b08a2019-01-17 13:07:25 +010024namespace webrtc {
25
26// Asynchronously executes tasks in a way that guarantees that they're executed
27// in FIFO order and that tasks never overlap. Tasks may always execute on the
28// same worker thread and they may not. To DCHECK that tasks are executing on a
29// known task queue, use IsCurrent().
Danil Chapovalov4423c362019-03-06 18:41:39 +010030class RTC_LOCKABLE TaskQueueBase {
Danil Chapovalov348b08a2019-01-17 13:07:25 +010031 public:
32 // Starts destruction of the task queue.
33 // On return ensures no task are running and no new tasks are able to start
34 // on the task queue.
35 // Responsible for deallocation. Deallocation may happen syncrhoniously during
36 // Delete or asynchronously after Delete returns.
37 // Code not running on the TaskQueue should not make any assumption when
38 // TaskQueue is deallocated and thus should not call any methods after Delete.
39 // Code running on the TaskQueue should not call Delete, but can assume
40 // TaskQueue still exists and may call other methods, e.g. PostTask.
Danil Chapovalovd00405f2019-02-25 15:06:13 +010041 virtual void Delete() = 0;
Danil Chapovalov348b08a2019-01-17 13:07:25 +010042
43 // Schedules a task to execute. Tasks are executed in FIFO order.
44 // If |task->Run()| returns true, task is deleted on the task queue
45 // before next QueuedTask starts executing.
46 // When a TaskQueue is deleted, pending tasks will not be executed but they
47 // will be deleted. The deletion of tasks may happen synchronously on the
48 // TaskQueue or it may happen asynchronously after TaskQueue is deleted.
49 // This may vary from one implementation to the next so assumptions about
50 // lifetimes of pending tasks should not be made.
Danil Chapovalovd00405f2019-02-25 15:06:13 +010051 virtual void PostTask(std::unique_ptr<QueuedTask> task) = 0;
Danil Chapovalov348b08a2019-01-17 13:07:25 +010052
53 // Schedules a task to execute a specified number of milliseconds from when
54 // the call is made. The precision should be considered as "best effort"
55 // and in some cases, such as on Windows when all high precision timers have
56 // been used up, can be off by as much as 15 millseconds.
Danil Chapovalovd00405f2019-02-25 15:06:13 +010057 virtual void PostDelayedTask(std::unique_ptr<QueuedTask> task,
58 uint32_t milliseconds) = 0;
Danil Chapovalov348b08a2019-01-17 13:07:25 +010059
Danil Chapovalovd00405f2019-02-25 15:06:13 +010060 // Returns the task queue that is running the current thread.
61 // Returns nullptr if this thread is not associated with any task queue.
Danil Chapovalov348b08a2019-01-17 13:07:25 +010062 static TaskQueueBase* Current();
63 bool IsCurrent() const { return Current() == this; }
64
65 protected:
66 class CurrentTaskQueueSetter {
67 public:
68 explicit CurrentTaskQueueSetter(TaskQueueBase* task_queue);
69 CurrentTaskQueueSetter(const CurrentTaskQueueSetter&) = delete;
70 CurrentTaskQueueSetter& operator=(const CurrentTaskQueueSetter&) = delete;
71 ~CurrentTaskQueueSetter();
72
73 private:
74 TaskQueueBase* const previous_;
75 };
76
77 // Users of the TaskQueue should call Delete instead of directly deleting
78 // this object.
Danil Chapovalovd00405f2019-02-25 15:06:13 +010079 virtual ~TaskQueueBase() = default;
Yves Gerey42d8c932019-03-08 16:14:47 +000080
81 private:
82 friend class rtc::TaskQueue;
83 rtc::TaskQueue* task_queue_ = nullptr;
Danil Chapovalov348b08a2019-01-17 13:07:25 +010084};
85
86struct TaskQueueDeleter {
87 void operator()(TaskQueueBase* task_queue) const { task_queue->Delete(); }
88};
89
90} // namespace webrtc
91
92#endif // API_TASK_QUEUE_TASK_QUEUE_BASE_H_