Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef BASE_TASK_SCHEDULER_WORKER_THREAD_H_ | |
| 6 #define BASE_TASK_SCHEDULER_WORKER_THREAD_H_ | |
| 7 | |
| 8 #include "base/base_export.h" | |
| 9 #include "base/callback_forward.h" | |
| 10 #include "base/macros.h" | |
| 11 #include "base/memory/ref_counted.h" | |
| 12 #include "base/single_thread_task_runner.h" | |
| 13 #include "base/synchronization/waitable_event.h" | |
| 14 #include "base/task_scheduler/priority_queue.h" | |
| 15 #include "base/task_scheduler/sequence.h" | |
| 16 #include "base/task_scheduler/task_traits.h" | |
| 17 #include "base/threading/platform_thread.h" | |
| 18 | |
| 19 namespace base { | |
| 20 namespace task_scheduler { | |
| 21 | |
| 22 class DelayedTaskManager; | |
| 23 class ShutdownManager; | |
| 24 | |
| 25 // A thread that runs tasks from a shared and a single-threaded priority queue. | |
| 26 // Unless otherwise noted, all public methods of this class are thread-safe. | |
| 27 class BASE_EXPORT WorkerThread : public PlatformThread::Delegate { | |
| 28 public: | |
| 29 // Callback to reinsert |sequence| in the appropriate priority queue after one | |
| 30 // of its tasks has been executed. |worker_thread| is the worker thread that | |
| 31 // invokes the callback. | |
| 32 using ReinsertSequenceCallback = | |
| 33 Callback<void(scoped_refptr<Sequence> sequence, | |
| 34 const WorkerThread* worker_thread)>; | |
| 35 | |
| 36 // Callback invoked when the worker thread becomes idle. |worker_thread| is | |
| 37 // the worker thread that invokes the callback. | |
| 38 using BecomesIdleCallback = Callback<void(WorkerThread* worker_thread)>; | |
| 39 | |
| 40 // Creates a worker thread that runs tasks from a single-thread priority queue | |
| 41 // and from |shared_priority_queue| with |thread_priority|. | |
| 42 // |reinsert_sequence_callback| is invoked to reinsert a sequence in the | |
| 43 // appropriate priority queue after one of its tasks has been executed. | |
| 44 // |delayed_task_manager| is used to handle delayed tasks. |shutdown_manager| | |
| 45 // is used to handle shutdown behavior of tasks. | |
| 46 static scoped_ptr<WorkerThread> CreateWorkerThread( | |
| 47 ThreadPriority thread_priority, | |
| 48 PriorityQueue* shared_priority_queue, | |
| 49 const ReinsertSequenceCallback& reinsert_sequence_callback, | |
| 50 const BecomesIdleCallback& becomes_idle_callback, | |
| 51 DelayedTaskManager* delayed_task_manager, | |
| 52 ShutdownManager* shutdown_manager); | |
| 53 | |
| 54 ~WorkerThread() override; | |
| 55 | |
| 56 // Wakes up the worker thread. Soon after this is called, the thread starts | |
| 57 // running tasks from its priority queue. It continues to run tasks until its | |
| 58 // priority queue is empty. Has no effect if the worker thread is already | |
| 59 // running tasks. | |
| 60 void WakeUp(); | |
| 61 | |
| 62 // Returns a SingleThreadTaskRunner whose PostTask invocations will result in | |
| 63 // scheduling tasks on this worker thread with traits |traits|. | |
| 64 scoped_refptr<SingleThreadTaskRunner> CreateTaskRunnerWithTraits( | |
| 65 const TaskTraits& traits, | |
| 66 ExecutionMode execution_mode); | |
| 67 | |
| 68 // Returns true if the worker thread has single-threaded tasks (running or | |
| 69 // pending). Use a memory barrier before calling this to avoid getting a stale | |
| 70 // result. The returned result can be invalidated at any time by single- | |
| 71 // threaded tasks being posted or run. | |
| 72 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.
| |
| 73 | |
| 74 // Waits until the thread exits. This must only be called after shutdown is | |
| 75 // complete (shutdown_manager_->shutdown_completed() returns true). This | |
| 76 // 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
| |
| 77 void JoinForTesting(); | |
| 78 | |
| 79 const PriorityQueue* shared_priority_queue() const { | |
| 80 return shared_priority_queue_; | |
| 81 } | |
| 82 | |
| 83 private: | |
| 84 WorkerThread(ThreadPriority thread_priority, | |
| 85 PriorityQueue* shared_priority_queue, | |
| 86 const ReinsertSequenceCallback& reinsert_sequence_callback, | |
| 87 const BecomesIdleCallback& becomes_idle_callback, | |
| 88 DelayedTaskManager* delayed_task_manager, | |
| 89 ShutdownManager* shutdown_manager); | |
| 90 | |
| 91 // Returns true if a thread has been successfully created by the constructor. | |
| 92 bool IsValid() const; | |
| 93 | |
| 94 // Extracts the sequence with the highest priority from | |
| 95 // |shared_priority_queue_| or |single_thread_priority_queue_|. | |
| 96 scoped_refptr<Sequence> GetWork(); | |
| 97 | |
| 98 // Reinserts |sequence| in |single_thread_priority_queue_|. | |
| 99 void ReinsertSequenceInSingleThreadPriorityQueue( | |
| 100 scoped_refptr<Sequence> sequence); | |
| 101 | |
| 102 // Waits until either |wakeup_event_| is signaled or a task from | |
| 103 // |delayed_task_manager_| becomes ready for execution. | |
| 104 void WaitUntilWorkIsAvailable(); | |
| 105 | |
| 106 // PlatformThread::Delegate: | |
| 107 void ThreadMain() override; | |
| 108 | |
| 109 // Platform thread managed by this object. | |
| 110 PlatformThreadHandle thread_handle_; | |
| 111 | |
| 112 // Event signaled to wake up the thread. | |
| 113 mutable WaitableEvent wakeup_event_; | |
| 114 | |
| 115 // True when the worker thread is running a single-threaded task. | |
| 116 bool is_running_single_threaded_task_; | |
| 117 | |
| 118 // The single-threaded priority queue from which the worker thread gets work. | |
| 119 // Tasks posted to a SINGLE_THREADED* task runner returned by this | |
| 120 // WorkerThread end up in this priority queue. | |
| 121 PriorityQueue single_thread_priority_queue_; | |
| 122 | |
| 123 // The shared priority queue from which the worker thread gets work. | |
| 124 PriorityQueue* const shared_priority_queue_; | |
| 125 | |
| 126 const ReinsertSequenceCallback reinsert_sequence_callback_; | |
| 127 | |
| 128 const BecomesIdleCallback becomes_idle_callback_; | |
| 129 | |
| 130 DelayedTaskManager* const delayed_task_manager_; | |
| 131 | |
| 132 ShutdownManager* const shutdown_manager_; | |
| 133 | |
| 134 DISALLOW_COPY_AND_ASSIGN(WorkerThread); | |
| 135 }; | |
| 136 | |
| 137 } // namespace task_scheduler | |
| 138 } // namespace base | |
| 139 | |
| 140 #endif // BASE_TASK_SCHEDULER_WORKER_THREAD_H_ | |
| OLD | NEW |