webrtc/api/task_queue/task_queue_base.h
Danil Chapovalov ad89528051 Reland "Delete rtc::TaskQueue::Current in favor of webrtc::TaskQueueBase::Current"
This reverts commit 42d8c93ec3.

Reason for revert: Got Aliby for FEC test flakes

Original change's description:
> Revert "Delete rtc::TaskQueue::Current in favor of webrtc::TaskQueueBase::Current"
> 
> This reverts commit 304e9d2df3.
> 
> Reason for revert: Breaks downstream projects.
> Seems to make VideoSendStreamTest.SupportsFlexfecSimulcastVp8 flaky.
> 
> Original change's description:
> > Delete rtc::TaskQueue::Current in favor of webrtc::TaskQueueBase::Current
> > 
> > Bug: webrtc:10191
> > Change-Id: I506cc50a90c73a6a4f6a3de36de0999cca72f5ba
> > Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/126230
> > Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
> > Commit-Queue: Danil Chapovalov <danilchap@webrtc.org>
> > Cr-Commit-Position: refs/heads/master@{#27035}
> 
> TBR=danilchap@webrtc.org,kwiberg@webrtc.org
> 
> Change-Id: If98324f88e4b3d18bf2fe33597dfb9711867c243
> No-Presubmit: true
> No-Tree-Checks: true
> No-Try: true
> Bug: webrtc:10191
> Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/126484
> Reviewed-by: Yves Gerey <yvesg@webrtc.org>
> Commit-Queue: Yves Gerey <yvesg@webrtc.org>
> Cr-Commit-Position: refs/heads/master@{#27041}

TBR=danilchap@webrtc.org,kwiberg@webrtc.org,yvesg@webrtc.org

# Not skipping CQ checks because original CL landed > 1 day ago.

Bug: webrtc:10191
Change-Id: Id87a17ae415142b8e0b11ba03ae7bad84a473fb0
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/126720
Reviewed-by: Danil Chapovalov <danilchap@webrtc.org>
Reviewed-by: Yves Gerey <yvesg@webrtc.org>
Commit-Queue: Yves Gerey <yvesg@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#27056}
2019-03-11 12:32:49 +00:00

82 lines
3.4 KiB
C++

/*
* Copyright 2019 The WebRTC Project Authors. All rights reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#ifndef API_TASK_QUEUE_TASK_QUEUE_BASE_H_
#define API_TASK_QUEUE_TASK_QUEUE_BASE_H_
#include <memory>
#include "api/task_queue/queued_task.h"
#include "rtc_base/thread_annotations.h"
namespace webrtc {
// Asynchronously executes tasks in a way that guarantees that they're executed
// in FIFO order and that tasks never overlap. Tasks may always execute on the
// same worker thread and they may not. To DCHECK that tasks are executing on a
// known task queue, use IsCurrent().
class RTC_LOCKABLE TaskQueueBase {
public:
// Starts destruction of the task queue.
// On return ensures no task are running and no new tasks are able to start
// on the task queue.
// Responsible for deallocation. Deallocation may happen syncrhoniously during
// Delete or asynchronously after Delete returns.
// Code not running on the TaskQueue should not make any assumption when
// TaskQueue is deallocated and thus should not call any methods after Delete.
// Code running on the TaskQueue should not call Delete, but can assume
// TaskQueue still exists and may call other methods, e.g. PostTask.
virtual void Delete() = 0;
// Schedules a task to execute. Tasks are executed in FIFO order.
// If |task->Run()| returns true, task is deleted on the task queue
// before next QueuedTask starts executing.
// When a TaskQueue is deleted, pending tasks will not be executed but they
// will be deleted. The deletion of tasks may happen synchronously on the
// TaskQueue or it may happen asynchronously after TaskQueue is deleted.
// This may vary from one implementation to the next so assumptions about
// lifetimes of pending tasks should not be made.
virtual void PostTask(std::unique_ptr<QueuedTask> task) = 0;
// Schedules a task to execute a specified number of milliseconds from when
// the call is made. The precision should be considered as "best effort"
// and in some cases, such as on Windows when all high precision timers have
// been used up, can be off by as much as 15 millseconds.
virtual void PostDelayedTask(std::unique_ptr<QueuedTask> task,
uint32_t milliseconds) = 0;
// Returns the task queue that is running the current thread.
// Returns nullptr if this thread is not associated with any task queue.
static TaskQueueBase* Current();
bool IsCurrent() const { return Current() == this; }
protected:
class CurrentTaskQueueSetter {
public:
explicit CurrentTaskQueueSetter(TaskQueueBase* task_queue);
CurrentTaskQueueSetter(const CurrentTaskQueueSetter&) = delete;
CurrentTaskQueueSetter& operator=(const CurrentTaskQueueSetter&) = delete;
~CurrentTaskQueueSetter();
private:
TaskQueueBase* const previous_;
};
// Users of the TaskQueue should call Delete instead of directly deleting
// this object.
virtual ~TaskQueueBase() = default;
};
struct TaskQueueDeleter {
void operator()(TaskQueueBase* task_queue) const { task_queue->Delete(); }
};
} // namespace webrtc
#endif // API_TASK_QUEUE_TASK_QUEUE_BASE_H_