blob: 175253b3efca0e30327b371ebe03a87cb959ce26 [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 Chapovalovb4c6d1e2019-01-21 13:52:59 +010016#include "api/task_queue/task_queue_impl.h"
Danil Chapovalov348b08a2019-01-17 13:07:25 +010017
18namespace 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 Chapovalovb4c6d1e2019-01-21 13:52:59 +010024// TODO(bugs.webrtc.org/10191): Remove inheritence from rtc::TaskQueue::Impl
25// when all implementations switch to use TaskQueueFactory instead of link-time
26// injection.
27class TaskQueueBase : public rtc::TaskQueue::Impl {
Danil Chapovalov348b08a2019-01-17 13:07:25 +010028 public:
29 // Starts destruction of the task queue.
30 // On return ensures no task are running and no new tasks are able to start
31 // on the task queue.
32 // Responsible for deallocation. Deallocation may happen syncrhoniously during
33 // Delete or asynchronously after Delete returns.
34 // Code not running on the TaskQueue should not make any assumption when
35 // TaskQueue is deallocated and thus should not call any methods after Delete.
36 // Code running on the TaskQueue should not call Delete, but can assume
37 // TaskQueue still exists and may call other methods, e.g. PostTask.
Danil Chapovalovb4c6d1e2019-01-21 13:52:59 +010038 void Delete() override = 0;
Danil Chapovalov348b08a2019-01-17 13:07:25 +010039
40 // Schedules a task to execute. Tasks are executed in FIFO order.
41 // If |task->Run()| returns true, task is deleted on the task queue
42 // before next QueuedTask starts executing.
43 // When a TaskQueue is deleted, pending tasks will not be executed but they
44 // will be deleted. The deletion of tasks may happen synchronously on the
45 // TaskQueue or it may happen asynchronously after TaskQueue is deleted.
46 // This may vary from one implementation to the next so assumptions about
47 // lifetimes of pending tasks should not be made.
Danil Chapovalovb4c6d1e2019-01-21 13:52:59 +010048 void PostTask(std::unique_ptr<QueuedTask> task) override = 0;
Danil Chapovalov348b08a2019-01-17 13:07:25 +010049
50 // Schedules a task to execute a specified number of milliseconds from when
51 // the call is made. The precision should be considered as "best effort"
52 // and in some cases, such as on Windows when all high precision timers have
53 // been used up, can be off by as much as 15 millseconds.
Danil Chapovalovb4c6d1e2019-01-21 13:52:59 +010054 void PostDelayedTask(std::unique_ptr<QueuedTask> task,
55 uint32_t milliseconds) override = 0;
Danil Chapovalov348b08a2019-01-17 13:07:25 +010056
57 // Until all TaskQueue implementations switch to using CurrentTaskQueueSetter
58 // below, this function may return nullptr even if code is executed by a
59 // TaskQueue. Keep using rtc::TaskQueue::Current() until bugs.webrtc.org/10191
60 // is resolved.
61 static TaskQueueBase* Current();
62 bool IsCurrent() const { return Current() == this; }
63
64 protected:
65 class CurrentTaskQueueSetter {
66 public:
67 explicit CurrentTaskQueueSetter(TaskQueueBase* task_queue);
68 CurrentTaskQueueSetter(const CurrentTaskQueueSetter&) = delete;
69 CurrentTaskQueueSetter& operator=(const CurrentTaskQueueSetter&) = delete;
70 ~CurrentTaskQueueSetter();
71
72 private:
73 TaskQueueBase* const previous_;
74 };
75
76 // Users of the TaskQueue should call Delete instead of directly deleting
77 // this object.
Danil Chapovalovb4c6d1e2019-01-21 13:52:59 +010078 ~TaskQueueBase() override = default;
Danil Chapovalov348b08a2019-01-17 13:07:25 +010079};
80
81struct TaskQueueDeleter {
82 void operator()(TaskQueueBase* task_queue) const { task_queue->Delete(); }
83};
84
85} // namespace webrtc
86
87#endif // API_TASK_QUEUE_TASK_QUEUE_BASE_H_