| Index: base/task_scheduler/worker_thread.h
|
| diff --git a/base/task_scheduler/worker_thread.h b/base/task_scheduler/worker_thread.h
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..7ca9c5736e058b01c3d9e8a3186b392ddaac6ab9
|
| --- /dev/null
|
| +++ b/base/task_scheduler/worker_thread.h
|
| @@ -0,0 +1,145 @@
|
| +// Copyright 2016 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#ifndef BASE_TASK_SCHEDULER_WORKER_THREAD_H_
|
| +#define BASE_TASK_SCHEDULER_WORKER_THREAD_H_
|
| +
|
| +#include "base/base_export.h"
|
| +#include "base/callback_forward.h"
|
| +#include "base/macros.h"
|
| +#include "base/memory/ref_counted.h"
|
| +#include "base/single_thread_task_runner.h"
|
| +#include "base/synchronization/waitable_event.h"
|
| +#include "base/task_scheduler/priority_queue.h"
|
| +#include "base/task_scheduler/sequence.h"
|
| +#include "base/task_scheduler/task_traits.h"
|
| +#include "base/threading/platform_thread.h"
|
| +
|
| +namespace base {
|
| +namespace internal {
|
| +
|
| +class DelayedTaskManager;
|
| +class ShutdownManager;
|
| +
|
| +// A thread that runs tasks from a shared and a single-threaded priority queue.
|
| +// This public methods of this class are thread-safe.
|
| +class BASE_EXPORT WorkerThread : public PlatformThread::Delegate {
|
| + public:
|
| + // Callback invoked when the thread enters its main function.
|
| + using MainEntryCallback = Callback<void()>;
|
| +
|
| + // Callback to reinsert |sequence| in the appropriate priority queue after one
|
| + // of its tasks has been executed. |worker_thread| is the worker thread that
|
| + // invokes the callback.
|
| + using ReinsertSequenceCallback =
|
| + Callback<void(scoped_refptr<Sequence> sequence,
|
| + const WorkerThread* worker_thread)>;
|
| +
|
| + // Callback invoked when the worker thread becomes idle. |worker_thread| is
|
| + // the worker thread that invokes the callback.
|
| + using BecomesIdleCallback = Callback<void(WorkerThread* worker_thread)>;
|
| +
|
| + // Creates a worker thread that runs tasks from a single-thread priority queue
|
| + // and from |shared_priority_queue| with |thread_priority|.
|
| + // |reinsert_sequence_callback| is invoked to reinsert a sequence in the
|
| + // appropriate priority queue after one of its tasks has been executed.
|
| + // |delayed_task_manager| is used to handle delayed tasks. |shutdown_manager|
|
| + // is used to handle shutdown behavior of tasks.
|
| + static scoped_ptr<WorkerThread> CreateWorkerThread(
|
| + ThreadPriority thread_priority,
|
| + PriorityQueue* shared_priority_queue,
|
| + const MainEntryCallback& main_entry_callback,
|
| + const ReinsertSequenceCallback& reinsert_sequence_callback,
|
| + const BecomesIdleCallback& becomes_idle_callback,
|
| + DelayedTaskManager* delayed_task_manager,
|
| + ShutdownManager* shutdown_manager);
|
| +
|
| + ~WorkerThread() override;
|
| +
|
| + // Wakes up the worker thread. Soon after this is called, the thread starts
|
| + // running tasks from its priority queue. It continues to run tasks until its
|
| + // priority queue is empty. Has no effect if the worker thread is already
|
| + // running tasks.
|
| + void WakeUp();
|
| +
|
| + // Returns a SingleThreadTaskRunner whose PostTask invocations will result in
|
| + // scheduling tasks on this worker thread with traits |traits|.
|
| + scoped_refptr<SingleThreadTaskRunner> CreateTaskRunnerWithTraits(
|
| + const TaskTraits& traits,
|
| + ExecutionMode execution_mode);
|
| +
|
| + // Returns true if the worker thread has single-threaded tasks (running or
|
| + // pending). The returned result can be invalidated at any time by single-
|
| + // threaded tasks being posted or executed.
|
| + bool HasSingleThreadedTasks() const;
|
| +
|
| + // Initiates shutdown and waits until the thread exits. This method must not
|
| + // be called more than once in the lifetime of the WorkerThread.
|
| + void ShutdownAndJoinForTesting();
|
| +
|
| + const PriorityQueue* shared_priority_queue() const {
|
| + return shared_priority_queue_;
|
| + }
|
| +
|
| + private:
|
| + WorkerThread(ThreadPriority thread_priority,
|
| + PriorityQueue* shared_priority_queue,
|
| + const MainEntryCallback& main_entry_callback,
|
| + const ReinsertSequenceCallback& reinsert_sequence_callback,
|
| + const BecomesIdleCallback& becomes_idle_callback,
|
| + DelayedTaskManager* delayed_task_manager,
|
| + ShutdownManager* shutdown_manager);
|
| +
|
| + // Returns true if a thread has been successfully created by the constructor.
|
| + bool IsValid() const;
|
| +
|
| + // Extracts the sequence with the highest priority from
|
| + // |shared_priority_queue_| or |single_thread_priority_queue_|.
|
| + scoped_refptr<Sequence> GetWork();
|
| +
|
| + // Reinserts |sequence| in |single_thread_priority_queue_|.
|
| + void ReinsertSequenceInSingleThreadPriorityQueue(
|
| + scoped_refptr<Sequence> sequence);
|
| +
|
| + // Waits until either |wakeup_event_| is signaled or a task from
|
| + // |delayed_task_manager_| becomes ready for execution.
|
| + void WaitUntilWorkIsAvailable();
|
| +
|
| + // PlatformThread::Delegate:
|
| + void ThreadMain() override;
|
| +
|
| + // Platform thread managed by this object.
|
| + PlatformThreadHandle thread_handle_;
|
| +
|
| + // Event signaled to wake up the thread.
|
| + mutable WaitableEvent wakeup_event_;
|
| +
|
| + // True when the worker thread is running a single-threaded task.
|
| + bool is_running_single_threaded_task_;
|
| +
|
| + // The single-threaded priority queue from which the worker thread gets work.
|
| + // Tasks posted to a SINGLE_THREADED* task runner returned by this
|
| + // WorkerThread end up in this priority queue.
|
| + PriorityQueue single_thread_priority_queue_;
|
| +
|
| + // The shared priority queue from which the worker thread gets work.
|
| + PriorityQueue* const shared_priority_queue_;
|
| +
|
| + const MainEntryCallback main_entry_callback_;
|
| +
|
| + const ReinsertSequenceCallback reinsert_sequence_callback_;
|
| +
|
| + const BecomesIdleCallback becomes_idle_callback_;
|
| +
|
| + DelayedTaskManager* const delayed_task_manager_;
|
| +
|
| + ShutdownManager* const shutdown_manager_;
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(WorkerThread);
|
| +};
|
| +
|
| +} // namespace internal
|
| +} // namespace base
|
| +
|
| +#endif // BASE_TASK_SCHEDULER_WORKER_THREAD_H_
|
|
|