Chromium Code Reviews| 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..067500451a5efd6753004c80105fe011229ff860 |
| --- /dev/null |
| +++ b/base/task_scheduler/worker_thread.h |
| @@ -0,0 +1,140 @@ |
| +// 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 task_scheduler { |
| + |
| +class DelayedTaskManager; |
| +class ShutdownManager; |
| + |
| +// A thread that runs tasks from a shared and a single-threaded priority queue. |
| +// Unless otherwise noted, all public methods of this class are thread-safe. |
| +class BASE_EXPORT WorkerThread : public PlatformThread::Delegate { |
| + public: |
| + // 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 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). Use a memory barrier before calling this to avoid getting a stale |
| + // result. The returned result can be invalidated at any time by single- |
| + // threaded tasks being posted or run. |
| + bool HasSingleThreadedTasks() const; |
|
robliao
2016/02/11 21:56:27
Since we're claiming this is thread safe, maybe we
fdoray
2016/02/12 04:16:20
Done.
|
| + |
| + // Waits until the thread exits. This must only be called after shutdown is |
| + // complete (shutdown_manager_->shutdown_completed() returns true). This |
| + // method is not thread-safe. |
|
fdoray
2016/02/11 17:30:34
This can only be called once per WorkerThread.
robliao
2016/02/11 21:56:27
We should be able to assert this.
fdoray
2016/02/12 04:16:20
Done. I added a DCHECK, which is unfortunately not
|
| + void JoinForTesting(); |
| + |
| + const PriorityQueue* shared_priority_queue() const { |
| + return shared_priority_queue_; |
| + } |
| + |
| + private: |
| + WorkerThread(ThreadPriority thread_priority, |
| + PriorityQueue* shared_priority_queue, |
| + 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 ReinsertSequenceCallback reinsert_sequence_callback_; |
| + |
| + const BecomesIdleCallback becomes_idle_callback_; |
| + |
| + DelayedTaskManager* const delayed_task_manager_; |
| + |
| + ShutdownManager* const shutdown_manager_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(WorkerThread); |
| +}; |
| + |
| +} // namespace task_scheduler |
| +} // namespace base |
| + |
| +#endif // BASE_TASK_SCHEDULER_WORKER_THREAD_H_ |