task_queue_base.h 3.4 KB

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