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

Unified Diff: base/task_scheduler/scheduler_worker_thread.h

Issue 2044023003: Virtualize The Existence of a Scheduler Worker Thread (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@detach
Patch Set: CR Feedback Created 4 years, 6 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/scheduler_worker_thread.h
diff --git a/base/task_scheduler/scheduler_worker_thread.h b/base/task_scheduler/scheduler_worker_thread.h
index b6f0860da0ee83cc5f3d3c30ac4f908384185585..5199858149934e4bb2dafe1a60215e25fe67c7d5 100644
--- a/base/task_scheduler/scheduler_worker_thread.h
+++ b/base/task_scheduler/scheduler_worker_thread.h
@@ -17,11 +17,13 @@
#include "base/time/time.h"
namespace base {
+
gab 2016/06/10 16:15:21 rm empty line
robliao 2016/06/10 18:03:41 Done.
namespace internal {
class TaskTracker;
-// A thread that runs Tasks from Sequences returned by a delegate.
+// A worker that manages at most one thread to run Tasks from Sequences returned
+// by a delegate.
//
// A SchedulerWorkerThread starts out sleeping. It is woken up by a call to
// WakeUp(). After a wake-up, a SchedulerWorkerThread runs Tasks from Sequences
@@ -29,8 +31,11 @@ class TaskTracker;
// nullptr. It also periodically checks with its TaskTracker whether shutdown
// has completed and exits when it has.
//
+// The worker is free to release and reallocate thread suggestions with guidance
gab 2016/06/10 16:15:20 "thread suggestions"?
robliao 2016/06/10 18:03:41 Fixed!
+// from the delegate
+//
// This class is thread-safe.
-class BASE_EXPORT SchedulerWorkerThread : public PlatformThread::Delegate {
+class BASE_EXPORT SchedulerWorkerThread {
public:
// Delegate interface for SchedulerWorkerThread. The methods are always called
// from the thread managed by the SchedulerWorkerThread instance.
@@ -38,7 +43,8 @@ class BASE_EXPORT SchedulerWorkerThread : public PlatformThread::Delegate {
public:
virtual ~Delegate() = default;
- // Called by |worker_thread| when it enters its main function.
+ // Called when |worker_thread|'s worker enters the main function.
+ // If a thread is recreated after detachment, this call will occur again.
virtual void OnMainEntry(SchedulerWorkerThread* worker_thread) = 0;
// Called by |worker_thread| to get a Sequence from which to run a Task.
@@ -53,25 +59,43 @@ class BASE_EXPORT SchedulerWorkerThread : public PlatformThread::Delegate {
// call to GetWork(). GetWork() may be called before this timeout expires
// if the thread's WakeUp() method is called.
virtual TimeDelta GetSleepTimeout() = 0;
+
+ // Called by |worker_thread| to see if it is allowed to detach if the last
+ // call to GetWork() returned nullptr.
+ // A SchedulerWorkerThread is free to ignore the request to detach.
gab 2016/06/10 16:15:21 Which "request to detach"? Is a return true here a
robliao 2016/06/10 18:03:41 Clarified. Today, it is.
+ //
+ // It is the responsibility of the delegate to determine if detachment is
+ // safe. If |worker_thread| is responsible for thread-affine work,
+ // detachment is generally not safe.
+ //
+ // - Future tasks may run on a different PlatformThread after detachment.
gab 2016/06/10 16:15:21 I don't think this is worth mentioning (i.e. it's
robliao 2016/06/10 18:03:41 sgtm, since threads are indeed virtualized now.
+ // - The next WakeUp() could be more costly due to new thread creation.
+ // - The worker will free resources if it can.
+ virtual bool CanDetach(SchedulerWorkerThread* worker_thread) = 0;
};
+ enum class InitialWorkerState { ALIVE, DETACHED };
gab 2016/06/10 16:15:21 "InitialState" is good enough IMO, "Worker" is red
robliao 2016/06/10 18:03:42 sgtm. Done.
+
// Creates a SchedulerWorkerThread with priority |thread_priority| that runs
// Tasks from Sequences returned by |delegate|. |task_tracker| is used to
- // handle shutdown behavior of Tasks. Returns nullptr if creating the
- // underlying platform thread fails.
+ // handle shutdown behavior of Tasks. If |worker_state| is DETACHED, the
gab 2016/06/10 16:15:21 Is the idea that we would create |max_threads| SWT
robliao 2016/06/10 18:03:41 Yup. The new CL I have only creates one in ALIVE m
+ // worker will be created upon a WakeUp. Returns nullptr if creating the
+ // underlying platform thread fails during Create..
gab 2016/06/10 16:15:21 s/.././
robliao 2016/06/10 18:03:41 Done.
static std::unique_ptr<SchedulerWorkerThread> Create(
ThreadPriority thread_priority,
std::unique_ptr<Delegate> delegate,
- TaskTracker* task_tracker);
+ TaskTracker* task_tracker,
+ InitialWorkerState worker_state);
// Destroying a SchedulerWorkerThread in production is not allowed; it is
// always leaked. In tests, it can only be destroyed after JoinForTesting()
// has returned.
- ~SchedulerWorkerThread() override;
+ ~SchedulerWorkerThread();
// Wakes up this SchedulerWorkerThread if it wasn't already awake. After this
// is called, this SchedulerWorkerThread will run Tasks from Sequences
// returned by the GetWork() method of its delegate until it returns nullptr.
+ // May fail if the worker is detached.
gab 2016/06/10 16:15:21 Document what failure means since this is a void r
robliao 2016/06/10 18:03:41 Done.
void WakeUp();
SchedulerWorkerThread::Delegate* delegate() { return delegate_.get(); }
@@ -80,22 +104,33 @@ class BASE_EXPORT SchedulerWorkerThread : public PlatformThread::Delegate {
// allowed to complete its execution. This can only be called once.
void JoinForTesting();
gab 2016/06/10 16:15:21 Document that Detach() must always return false wh
robliao 2016/06/10 18:03:42 Doc'ed in Delegate::CanDetach().
+ // Returns ture if the worker is alive.
+ bool WorkerAliveForTesting() const;
+
private:
+ class Worker;
+
SchedulerWorkerThread(ThreadPriority thread_priority,
std::unique_ptr<Delegate> delegate,
TaskTracker* task_tracker);
- // PlatformThread::Delegate:
- void ThreadMain() override;
+ // Returns the worker for ownership by the worker thread if the detach was
gab 2016/06/10 16:15:21 "for ownership by" => I don't understand this sent
robliao 2016/06/10 18:03:41 Clarified.
+ // successful. If the detach is not possible, returns nullptr.
+ std::unique_ptr<SchedulerWorkerThread::Worker> Detach();
+
+ void CreateWorker();
+
+ void CreateWorkerAssertSynchronized();
bool ShouldExitForTesting() const;
- // Platform thread managed by this SchedulerWorkerThread.
- PlatformThreadHandle thread_handle_;
+ // Synchronizes access to |worker_|
+ mutable SchedulerLock worker_lock_;
- // Event signaled to wake up this SchedulerWorkerThread.
- WaitableEvent wake_up_event_;
+ // The underlying thread for this SchedulerWorkerThread.
+ std::unique_ptr<Worker> worker_;
+ const ThreadPriority thread_priority_;
const std::unique_ptr<Delegate> delegate_;
TaskTracker* const task_tracker_;

Powered by Google App Engine
This is Rietveld 408576698