Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(278)

Unified Diff: base/task_scheduler/worker_thread.h

Issue 1704113002: TaskScheduler [6] SchedulerWorkerThread (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@s_4_shutdown
Patch Set: rebase Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..271d40a08c17ee92cf73c1d167298dbd63446b06
--- /dev/null
+++ b/base/task_scheduler/worker_thread.h
@@ -0,0 +1,117 @@
+// 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.h"
+#include "base/macros.h"
+#include "base/memory/ref_counted.h"
+#include "base/single_thread_task_runner.h"
+#include "base/synchronization/condition_variable.h"
+#include "base/task_scheduler/priority_queue.h"
+#include "base/task_scheduler/scheduler_lock.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 priority queue.
+// This class is thread-safe.
+class BASE_EXPORT WorkerThread : public PlatformThread::Delegate {
+ public:
+ // Callback invoked by |worker_thread| to reinsert |sequence| in the
+ // appropriate priority queue it has executed one of its tasks.
+ using ReinsertSequenceCallback =
+ Callback<void(scoped_refptr<Sequence> sequence,
+ const WorkerThread* worker_thread)>;
+
+ // Callback invoked when |worker_thread| becomes idle.
+ using BecomesIdleCallback = Callback<void(WorkerThread* worker_thread)>;
+
+ // Creates a WorkerThread with priority |thread_priority| that runs tasks from
+ // a single-thread priority queue and from |shared_priority_queue|.
+ // |reinsert_sequence_callback| is invoked to reinsert a sequence in the
+ // appropriate priority queue after one of its tasks has been executed.
+ // |task_tracker| 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,
+ TaskTracker* task_tracker);
+
+ // A WorkerThread must only be destroyed after JoinForTesting() has returned.
+ ~WorkerThread() override;
+
+ // Wakes up the WorkerThread. Returns false if it was already awake.
+ bool WakeUp();
+
+ // Returns a SingleThreadTaskRunner whose PostTask invocations will result in
+ // scheduling tasks on this WorkerThread with |traits|.
+ scoped_refptr<SingleThreadTaskRunner> CreateTaskRunnerWithTraits(
+ const TaskTraits& traits);
+
+ // Joins the 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 ReinsertSequenceCallback& reinsert_sequence_callback,
+ const BecomesIdleCallback& becomes_idle_callback,
+ TaskTracker* task_tracker);
+
+ // Extracts the sequence with the highest priority from
+ // |shared_priority_queue_| or |single_thread_priority_queue_|.
+ // |single_thread| is set to true if the returned task comes from
+ // |single_thread_priority_queue_|.
+ scoped_refptr<Sequence> GetWork(bool* single_thread);
+
+ // Waits until |is_awake_| becomes true.
+ void WaitUntilWakeUp();
+
+ // PlatformThread::Delegate:
+ void ThreadMain() override;
+
+ // Platform thread managed by this WorkerThread.
+ PlatformThreadHandle thread_handle_;
+
+ // Synchronizes access to |wake_up_cv_|, |is_awake_| and
+ // |should_exit_for_testing_|.
+ SchedulerLock lock_;
+
+ // Condition variable signaled when |is_awake_| becomes true.
+ mutable scoped_ptr<ConditionVariable> wake_up_cv_;
+
+ // True from the time WakeUp() is called to the time the WorkerThread doesn't
+ // have any remaining work to do.
+ bool is_awake_;
+
+ // True once JoinForTesting() has been called.
+ bool should_exit_for_testing_;
+
+ // The single-threaded priority queue from which the WorkerThread gets work.
+ PriorityQueue single_thread_priority_queue_;
+
+ // The shared priority queue from which the WorkerThread gets work.
+ PriorityQueue* const shared_priority_queue_;
+
+ const ReinsertSequenceCallback reinsert_sequence_callback_;
+ const BecomesIdleCallback becomes_idle_callback_;
+ TaskTracker* const task_tracker_;
+
+ DISALLOW_COPY_AND_ASSIGN(WorkerThread);
+};
+
+} // namespace internal
+} // namespace base
+
+#endif // BASE_TASK_SCHEDULER_WORKER_THREAD_H_

Powered by Google App Engine
This is Rietveld 408576698