#include "base/task/sequence_manager/task_queue_impl.h"
#include <inttypes.h>
#include <memory>
#include <optional>
#include <utility>
#include "base/check.h"
#include "base/compiler_specific.h"
#include "base/feature_list.h"
#include "base/logging.h"
#include "base/memory/scoped_refptr.h"
#include "base/metrics/histogram_macros.h"
#include "base/notreached.h"
#include "base/observer_list.h"
#include "base/ranges/algorithm.h"
#include "base/sequence_token.h"
#include "base/strings/stringprintf.h"
#include "base/task/common/scoped_defer_task_posting.h"
#include "base/task/default_delayed_task_handle_delegate.h"
#include "base/task/sequence_manager/associated_thread_id.h"
#include "base/task/sequence_manager/delayed_task_handle_delegate.h"
#include "base/task/sequence_manager/fence.h"
#include "base/task/sequence_manager/sequence_manager_impl.h"
#include "base/task/sequence_manager/task_order.h"
#include "base/task/sequence_manager/wake_up_queue.h"
#include "base/task/sequence_manager/work_queue.h"
#include "base/task/single_thread_task_runner.h"
#include "base/task/task_features.h"
#include "base/task/task_observer.h"
#include "base/threading/thread_restrictions.h"
#include "base/time/time.h"
#include "base/trace_event/base_tracing.h"
#include "build/build_config.h"
#include "third_party/abseil-cpp/absl/container/inlined_vector.h"
namespace base {
namespace sequence_manager {
namespace internal {
class CurrentDefaultHandleOverrideForRunOrPostTask { … };
namespace {
std::atomic<base::TimeDelta> g_max_precise_delay{ … };
#if BUILDFLAG(IS_WIN)
std::atomic_bool g_explicit_high_resolution_timer_win{true};
#endif
void RunTaskSynchronously(AssociatedThreadId* associated_thread,
scoped_refptr<SingleThreadTaskRunner> task_runner,
OnceClosure closure) { … }
}
TaskQueueImpl::GuardedTaskPoster::GuardedTaskPoster(TaskQueueImpl* outer)
: … { … }
TaskQueueImpl::GuardedTaskPoster::~GuardedTaskPoster() { … }
bool TaskQueueImpl::GuardedTaskPoster::PostTask(PostedTask task) { … }
DelayedTaskHandle TaskQueueImpl::GuardedTaskPoster::PostCancelableTask(
PostedTask task) { … }
bool TaskQueueImpl::GuardedTaskPoster::RunOrPostTask(PostedTask task) { … }
TaskQueueImpl::TaskRunner::TaskRunner(
scoped_refptr<GuardedTaskPoster> task_poster,
scoped_refptr<AssociatedThreadId> associated_thread,
TaskType task_type)
: … { … }
TaskQueueImpl::TaskRunner::~TaskRunner() { … }
bool TaskQueueImpl::TaskRunner::PostDelayedTask(const Location& location,
OnceClosure callback,
TimeDelta delay) { … }
bool TaskQueueImpl::TaskRunner::PostDelayedTaskAt(
subtle::PostDelayedTaskPassKey,
const Location& location,
OnceClosure callback,
TimeTicks delayed_run_time,
base::subtle::DelayPolicy delay_policy) { … }
DelayedTaskHandle TaskQueueImpl::TaskRunner::PostCancelableDelayedTaskAt(
subtle::PostDelayedTaskPassKey pass_key,
const Location& location,
OnceClosure callback,
TimeTicks delayed_run_time,
base::subtle::DelayPolicy delay_policy) { … }
DelayedTaskHandle TaskQueueImpl::TaskRunner::PostCancelableDelayedTask(
subtle::PostDelayedTaskPassKey pass_key,
const Location& location,
OnceClosure callback,
TimeDelta delay) { … }
bool TaskQueueImpl::TaskRunner::PostNonNestableDelayedTask(
const Location& location,
OnceClosure callback,
TimeDelta delay) { … }
bool TaskQueueImpl::TaskRunner::RunOrPostTask(subtle::RunOrPostTaskPassKey,
const Location& location,
OnceClosure callback) { … }
bool TaskQueueImpl::TaskRunner::BelongsToCurrentThread() const { … }
bool TaskQueueImpl::TaskRunner::RunsTasksInCurrentSequence() const { … }
void TaskQueueImpl::InitializeFeatures() { … }
TaskQueueImpl::TaskQueueImpl(SequenceManagerImpl* sequence_manager,
WakeUpQueue* wake_up_queue,
const TaskQueue::Spec& spec)
: … { … }
TaskQueueImpl::~TaskQueueImpl() { … }
TaskQueueImpl::AnyThread::AnyThread() = default;
TaskQueueImpl::AnyThread::~AnyThread() = default;
TaskQueueImpl::AnyThread::TracingOnly::TracingOnly() = default;
TaskQueueImpl::AnyThread::TracingOnly::~TracingOnly() = default;
TaskQueueImpl::MainThreadOnly::MainThreadOnly(TaskQueueImpl* task_queue,
WakeUpQueue* wake_up_queue)
: … { … }
TaskQueueImpl::MainThreadOnly::~MainThreadOnly() = default;
scoped_refptr<SingleThreadTaskRunner> TaskQueueImpl::CreateTaskRunner(
TaskType task_type) const { … }
const scoped_refptr<SingleThreadTaskRunner>& TaskQueueImpl::task_runner()
const { … }
void TaskQueueImpl::UnregisterTaskQueue() { … }
const char* TaskQueueImpl::GetName() const { … }
QueueName TaskQueueImpl::GetProtoName() const { … }
void TaskQueueImpl::PostTask(PostedTask task) { … }
void TaskQueueImpl::RemoveCancelableTask(HeapHandle heap_handle) { … }
TimeDelta TaskQueueImpl::GetTaskDelayAdjustment(CurrentThread current_thread) { … }
void TaskQueueImpl::PostImmediateTaskImpl(PostedTask task,
CurrentThread current_thread) { … }
void TaskQueueImpl::PostDelayedTaskImpl(PostedTask posted_task,
CurrentThread current_thread) { … }
void TaskQueueImpl::PushOntoDelayedIncomingQueueFromMainThread(
Task pending_task,
LazyNow* lazy_now,
bool notify_task_annotator) { … }
void TaskQueueImpl::PushOntoDelayedIncomingQueue(Task pending_task) { … }
void TaskQueueImpl::ScheduleDelayedWorkTask(Task pending_task) { … }
void TaskQueueImpl::ReloadEmptyImmediateWorkQueue() { … }
void TaskQueueImpl::TakeImmediateIncomingQueueTasks(TaskDeque* queue) { … }
bool TaskQueueImpl::IsEmpty() const { … }
size_t TaskQueueImpl::GetNumberOfPendingTasks() const { … }
bool TaskQueueImpl::HasTaskToRunImmediatelyOrReadyDelayedTask() const { … }
std::optional<WakeUp> TaskQueueImpl::GetNextDesiredWakeUp() { … }
void TaskQueueImpl::OnWakeUp(LazyNow* lazy_now, EnqueueOrder enqueue_order) { … }
bool TaskQueueImpl::RemoveAllCanceledDelayedTasksFromFront(LazyNow* lazy_now) { … }
void TaskQueueImpl::MoveReadyDelayedTasksToWorkQueue(
LazyNow* lazy_now,
EnqueueOrder enqueue_order) { … }
void TaskQueueImpl::TraceQueueSize() const { … }
void TaskQueueImpl::SetQueuePriority(TaskQueue::QueuePriority priority) { … }
TaskQueue::QueuePriority TaskQueueImpl::GetQueuePriority() const { … }
Value::Dict TaskQueueImpl::AsValue(TimeTicks now, bool force_verbose) const { … }
void TaskQueueImpl::AddTaskObserver(TaskObserver* task_observer) { … }
void TaskQueueImpl::RemoveTaskObserver(TaskObserver* task_observer) { … }
void TaskQueueImpl::NotifyWillProcessTask(const Task& task,
bool was_blocked_or_low_priority) { … }
void TaskQueueImpl::NotifyDidProcessTask(const Task& task) { … }
void TaskQueueImpl::InsertFence(TaskQueue::InsertFencePosition position) { … }
void TaskQueueImpl::InsertFence(Fence current_fence) { … }
void TaskQueueImpl::InsertFenceAt(TimeTicks time) { … }
void TaskQueueImpl::RemoveFence() { … }
bool TaskQueueImpl::BlockedByFence() const { … }
bool TaskQueueImpl::HasActiveFence() { … }
bool TaskQueueImpl::CouldTaskRun(EnqueueOrder enqueue_order) const { … }
bool TaskQueueImpl::WasBlockedOrLowPriority(EnqueueOrder enqueue_order) const { … }
Value::List TaskQueueImpl::QueueAsValue(const TaskDeque& queue, TimeTicks now) { … }
Value::Dict TaskQueueImpl::TaskAsValue(const Task& task, TimeTicks now) { … }
Task TaskQueueImpl::MakeDelayedTask(PostedTask delayed_task,
LazyNow* lazy_now) const { … }
bool TaskQueueImpl::IsQueueEnabled() const { … }
void TaskQueueImpl::SetQueueEnabled(bool enabled) { … }
void TaskQueueImpl::SetShouldReportPostedTasksWhenDisabled(bool should_report) { … }
void TaskQueueImpl::UpdateCrossThreadQueueStateLocked() { … }
void TaskQueueImpl::ReclaimMemory(TimeTicks now) { … }
void TaskQueueImpl::PushImmediateIncomingTaskForTest(Task task) { … }
void TaskQueueImpl::RequeueDeferredNonNestableTask(
DeferredNonNestableTask task) { … }
void TaskQueueImpl::SetThrottler(TaskQueue::Throttler* throttler) { … }
void TaskQueueImpl::ResetThrottler() { … }
void TaskQueueImpl::UpdateWakeUp(LazyNow* lazy_now) { … }
void TaskQueueImpl::SetNextWakeUp(LazyNow* lazy_now,
std::optional<WakeUp> wake_up) { … }
bool TaskQueueImpl::HasTaskToRunImmediately() const { … }
bool TaskQueueImpl::HasTaskToRunImmediatelyLocked() const { … }
void TaskQueueImpl::SetOnTaskStartedHandler(
TaskQueueImpl::OnTaskStartedHandler handler) { … }
void TaskQueueImpl::OnTaskStarted(const Task& task,
const TaskQueue::TaskTiming& task_timing) { … }
void TaskQueueImpl::SetOnTaskCompletedHandler(
TaskQueueImpl::OnTaskCompletedHandler handler) { … }
void TaskQueueImpl::OnTaskCompleted(const Task& task,
TaskQueue::TaskTiming* task_timing,
LazyNow* lazy_now) { … }
bool TaskQueueImpl::RequiresTaskTiming() const { … }
std::unique_ptr<TaskQueue::OnTaskPostedCallbackHandle>
TaskQueueImpl::AddOnTaskPostedHandler(OnTaskPostedHandler handler) { … }
void TaskQueueImpl::RemoveOnTaskPostedHandler(
TaskQueueImpl::OnTaskPostedCallbackHandleImpl*
on_task_posted_callback_handle) { … }
void TaskQueueImpl::SetTaskExecutionTraceLogger(
TaskExecutionTraceLogger logger) { … }
bool TaskQueueImpl::IsUnregistered() const { … }
WeakPtr<SequenceManagerImpl> TaskQueueImpl::GetSequenceManagerWeakPtr() { … }
void TaskQueueImpl::ActivateDelayedFenceIfNeeded(const Task& task) { … }
void TaskQueueImpl::MaybeReportIpcTaskQueuedFromMainThread(
const Task& pending_task) { … }
bool TaskQueueImpl::ShouldReportIpcTaskQueuedFromAnyThreadLocked(
base::TimeDelta* time_since_disabled) { … }
void TaskQueueImpl::MaybeReportIpcTaskQueuedFromAnyThreadLocked(
const Task& pending_task) { … }
void TaskQueueImpl::MaybeReportIpcTaskQueuedFromAnyThreadUnlocked(
const Task& pending_task) { … }
void TaskQueueImpl::ReportIpcTaskQueued(
const Task& pending_task,
const base::TimeDelta& time_since_disabled) { … }
void TaskQueueImpl::OnQueueUnblocked() { … }
std::unique_ptr<TaskQueue::QueueEnabledVoter>
TaskQueueImpl::CreateQueueEnabledVoter() { … }
void TaskQueueImpl::AddQueueEnabledVoter(bool voter_is_enabled,
TaskQueue::QueueEnabledVoter& voter) { … }
void TaskQueueImpl::RemoveQueueEnabledVoter(
bool voter_is_enabled,
TaskQueue::QueueEnabledVoter& voter) { … }
void TaskQueueImpl::OnQueueEnabledVoteChanged(bool enabled) { … }
void TaskQueueImpl::CompleteInitializationOnBoundThread() { … }
TaskQueue::QueuePriority TaskQueueImpl::DefaultPriority() const { … }
bool TaskQueueImpl::IsQueueEnabledFromAnyThread() const { … }
TaskQueueImpl::DelayedIncomingQueue::DelayedIncomingQueue() = default;
TaskQueueImpl::DelayedIncomingQueue::~DelayedIncomingQueue() = default;
void TaskQueueImpl::DelayedIncomingQueue::push(Task task) { … }
void TaskQueueImpl::DelayedIncomingQueue::remove(HeapHandle heap_handle) { … }
Task TaskQueueImpl::DelayedIncomingQueue::take_top() { … }
void TaskQueueImpl::DelayedIncomingQueue::swap(DelayedIncomingQueue* rhs) { … }
void TaskQueueImpl::DelayedIncomingQueue::SweepCancelledTasks(
SequenceManagerImpl* sequence_manager) { … }
Value::List TaskQueueImpl::DelayedIncomingQueue::AsValue(TimeTicks now) const { … }
bool TaskQueueImpl::DelayedIncomingQueue::Compare::operator()(
const Task& lhs,
const Task& rhs) const { … }
TaskQueueImpl::OnTaskPostedCallbackHandleImpl::OnTaskPostedCallbackHandleImpl(
TaskQueueImpl* task_queue_impl,
scoped_refptr<const AssociatedThreadId> associated_thread)
: … { … }
TaskQueueImpl::OnTaskPostedCallbackHandleImpl::
~OnTaskPostedCallbackHandleImpl() { … }
}
}
}