| 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..ab98813bcc5336311e1a8ee9737b34306ac912f2
|
| --- /dev/null
|
| +++ b/base/task_scheduler/worker_thread.h
|
| @@ -0,0 +1,143 @@
|
| +// 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 <iosfwd>
|
| +
|
| +#include "base/atomicops.h"
|
| +#include "base/base_export.h"
|
| +#include "base/callback.h"
|
| +#include "base/macros.h"
|
| +#include "base/memory/ref_counted.h"
|
| +#include "base/memory/scoped_ptr.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 TaskTracker;
|
| +
|
| +// A thread that runs Tasks from a shared and a single-threaded PriorityQueue.
|
| +//
|
| +// There is no guarantee that a WorkerThread will run work from its shared
|
| +// PriorityQueue unless WakeUp() is called after the work is added. This allows
|
| +// a ThreadPool to choose which WorkerThread should run the new work. On the
|
| +// other hand, a WorkerThread is woken up automatically when Tasks are added to
|
| +// its single-threaded PriorityQueue.
|
| +//
|
| +// This class is thread-safe.
|
| +class BASE_EXPORT WorkerThread : public PlatformThread::Delegate {
|
| + public:
|
| + // Possible states for a WorkerThread.
|
| + enum class State {
|
| + BUSY,
|
| + IDLE,
|
| + };
|
| +
|
| + // Callback invoked to indicate that a shared |sequence| is not empty after
|
| + // |worker_thread| has popped a Task from it.
|
| + using SharedSequenceStillHasTasksCallback =
|
| + Callback<void(const WorkerThread* worker_thread,
|
| + scoped_refptr<Sequence> sequence)>;
|
| +
|
| + // Callback invoked when the state of |worker_thread| becomes |state|. This is
|
| + // always invoked within the scope of an active |shared_priority_queue_|
|
| + // Transaction. If |state| is IDLE, it is guaranteed that
|
| + // |shared_priority_queue_| is empty (this can't change during the callback
|
| + // invocation because of the active Transaction).
|
| + using StateChangedCallback =
|
| + Callback<void(WorkerThread* worker_thread, State state)>;
|
| +
|
| + // Creates a WorkerThread with priority |thread_priority| that runs Tasks from
|
| + // its single-threaded PriorityQueue and from |shared_priority_queue|.
|
| + // |shared_sequence_still_has_tasks_callback| is invoked when a Sequence
|
| + // extracted from |shared_priority_queue| isn't empty after the WorkerThread
|
| + // has popped a task from it. |state_changed_callback| is invoked when the
|
| + // WorkerThread becomes IDLE or BUSY. |task_tracker| is used to handle
|
| + // shutdown behavior of Tasks.
|
| + static scoped_ptr<WorkerThread> CreateWorkerThread(
|
| + ThreadPriority thread_priority,
|
| + PriorityQueue* shared_priority_queue,
|
| + const SharedSequenceStillHasTasksCallback&
|
| + shared_sequence_still_has_tasks_callback,
|
| + const StateChangedCallback& state_changed_callback,
|
| + TaskTracker* task_tracker);
|
| +
|
| + // Destroying a WorkerThread in production is not allowed; it is always
|
| + // leaked. In tests, it can only be destroyed after JoinForTesting() has
|
| + // returned.
|
| + ~WorkerThread() override;
|
| +
|
| + // Wakes up this WorkerThread. When this is called, this WorkerThread will run
|
| + // until both its PriorityQueues become empty.
|
| + void WakeUp();
|
| +
|
| + // Returns a SingleThreadTaskRunner whose PostTask invocations will result in
|
| + // scheduling Tasks with |traits| on this WorkerThread.
|
| + scoped_refptr<SingleThreadTaskRunner> CreateTaskRunnerWithTraits(
|
| + const TaskTraits& traits);
|
| +
|
| + // Joins this WorkerThread. If a Task is already running, it will be allowed
|
| + // to complete its execution. This can only be called once.
|
| + void JoinForTesting();
|
| +
|
| + private:
|
| + WorkerThread(ThreadPriority thread_priority,
|
| + PriorityQueue* shared_priority_queue,
|
| + const SharedSequenceStillHasTasksCallback&
|
| + shared_sequence_still_has_tasks_callback,
|
| + const StateChangedCallback& state_changed_callback,
|
| + TaskTracker* task_tracker);
|
| +
|
| + // Sets the state of this WorkerThread.
|
| + void SetState(State state);
|
| +
|
| + // PlatformThread::Delegate:
|
| + void ThreadMain() override;
|
| +
|
| + bool should_exit_for_testing() const {
|
| + base::subtle::MemoryBarrier();
|
| + return should_exit_for_testing_;
|
| + }
|
| +
|
| + // Platform thread managed by this WorkerThread.
|
| + PlatformThreadHandle thread_handle_;
|
| +
|
| + // Current state of this WorkerThread.
|
| + State state_ = State::BUSY;
|
| +
|
| + // Event signaled to wake up this WorkerThread.
|
| + WaitableEvent wake_up_event_;
|
| +
|
| + // The single-threaded PriorityQueue from which this WorkerThread gets work.
|
| + PriorityQueue single_thread_priority_queue_;
|
| +
|
| + // The shared PriorityQueue from which this WorkerThread gets work.
|
| + PriorityQueue* const shared_priority_queue_;
|
| +
|
| + const SharedSequenceStillHasTasksCallback
|
| + shared_sequence_still_has_tasks_callback_;
|
| + const StateChangedCallback state_changed_callback_;
|
| + TaskTracker* const task_tracker_;
|
| +
|
| + // True once JoinForTesting() has been called.
|
| + bool should_exit_for_testing_ = false;
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(WorkerThread);
|
| +};
|
| +
|
| +BASE_EXPORT std::ostream& operator<<(std::ostream& os,
|
| + WorkerThread::State state);
|
| +
|
| +} // namespace internal
|
| +} // namespace base
|
| +
|
| +#endif // BASE_TASK_SCHEDULER_WORKER_THREAD_H_
|
|
|