blob: 7e42bbaeadf3d2298460f25cbc136bb492dda891 [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
Danil Chapovalov348b08a2019-01-17 13:07:25 +010018namespace webrtc {
19
20// Asynchronously executes tasks in a way that guarantees that they're executed
21// in FIFO order and that tasks never overlap. Tasks may always execute on the
22// same worker thread and they may not. To DCHECK that tasks are executing on a
23// known task queue, use IsCurrent().
Danil Chapovalov4423c362019-03-06 18:41:39 +010024class RTC_LOCKABLE TaskQueueBase {
Danil Chapovalov348b08a2019-01-17 13:07:25 +010025 public:
26 // Starts destruction of the task queue.
27 // On return ensures no task are running and no new tasks are able to start
28 // on the task queue.
29 // Responsible for deallocation. Deallocation may happen syncrhoniously during
30 // Delete or asynchronously after Delete returns.
31 // Code not running on the TaskQueue should not make any assumption when
32 // TaskQueue is deallocated and thus should not call any methods after Delete.
33 // Code running on the TaskQueue should not call Delete, but can assume
34 // TaskQueue still exists and may call other methods, e.g. PostTask.
Danil Chapovalovd00405f2019-02-25 15:06:13 +010035 virtual void Delete() = 0;
Danil Chapovalov348b08a2019-01-17 13:07:25 +010036
37 // Schedules a task to execute. Tasks are executed in FIFO order.
38 // If |task->Run()| returns true, task is deleted on the task queue
39 // before next QueuedTask starts executing.
40 // When a TaskQueue is deleted, pending tasks will not be executed but they
41 // will be deleted. The deletion of tasks may happen synchronously on the
42 // TaskQueue or it may happen asynchronously after TaskQueue is deleted.
43 // This may vary from one implementation to the next so assumptions about
44 // lifetimes of pending tasks should not be made.
Danil Chapovalovd00405f2019-02-25 15:06:13 +010045 virtual void PostTask(std::unique_ptr<QueuedTask> task) = 0;
Danil Chapovalov348b08a2019-01-17 13:07:25 +010046
47 // Schedules a task to execute a specified number of milliseconds from when
48 // the call is made. The precision should be considered as "best effort"
49 // and in some cases, such as on Windows when all high precision timers have
50 // been used up, can be off by as much as 15 millseconds.
Danil Chapovalovd00405f2019-02-25 15:06:13 +010051 virtual void PostDelayedTask(std::unique_ptr<QueuedTask> task,
52 uint32_t milliseconds) = 0;
Danil Chapovalov348b08a2019-01-17 13:07:25 +010053
Danil Chapovalovd00405f2019-02-25 15:06:13 +010054 // Returns the task queue that is running the current thread.
55 // Returns nullptr if this thread is not associated with any task queue.
Danil Chapovalov348b08a2019-01-17 13:07:25 +010056 static TaskQueueBase* Current();
57 bool IsCurrent() const { return Current() == this; }
58
59 protected:
60 class CurrentTaskQueueSetter {
61 public:
62 explicit CurrentTaskQueueSetter(TaskQueueBase* task_queue);
63 CurrentTaskQueueSetter(const CurrentTaskQueueSetter&) = delete;
64 CurrentTaskQueueSetter& operator=(const CurrentTaskQueueSetter&) = delete;
65 ~CurrentTaskQueueSetter();
66
67 private:
68 TaskQueueBase* const previous_;
69 };
70
71 // Users of the TaskQueue should call Delete instead of directly deleting
72 // this object.
Danil Chapovalovd00405f2019-02-25 15:06:13 +010073 virtual ~TaskQueueBase() = default;
Danil Chapovalov348b08a2019-01-17 13:07:25 +010074};
75
76struct TaskQueueDeleter {
77 void operator()(TaskQueueBase* task_queue) const { task_queue->Delete(); }
78};
79
80} // namespace webrtc
81
82#endif // API_TASK_QUEUE_TASK_QUEUE_BASE_H_