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

Unified Diff: base/task_scheduler/scheduler_worker.cc

Issue 2806413002: Separate the create and start phases in SchedulerSingleThreadTaskRunnerManager. (Closed)
Patch Set: CR-robliao-45-initial-state-comment Created 3 years, 8 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
« no previous file with comments | « base/task_scheduler/scheduler_worker.h ('k') | base/task_scheduler/scheduler_worker_pool_impl.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/task_scheduler/scheduler_worker.cc
diff --git a/base/task_scheduler/scheduler_worker.cc b/base/task_scheduler/scheduler_worker.cc
index 4084c527a73c17e6ba4fa80398e792b1c268566b..409d5e15aee0ef77ecf95d07582ee0a392ff7486 100644
--- a/base/task_scheduler/scheduler_worker.cc
+++ b/base/task_scheduler/scheduler_worker.cc
@@ -201,25 +201,39 @@ void SchedulerWorker::Delegate::WaitForWork(WaitableEvent* wake_up_event) {
wake_up_event->Reset();
}
-scoped_refptr<SchedulerWorker> SchedulerWorker::Create(
+SchedulerWorker::SchedulerWorker(
ThreadPriority priority_hint,
std::unique_ptr<Delegate> delegate,
TaskTracker* task_tracker,
- InitialState initial_state,
- SchedulerBackwardCompatibility backward_compatibility) {
- scoped_refptr<SchedulerWorker> worker(
- new SchedulerWorker(priority_hint, std::move(delegate), task_tracker,
- backward_compatibility));
- // Creation happens before any other thread can reference this one, so no
- // synchronization is necessary.
- if (initial_state == SchedulerWorker::InitialState::ALIVE) {
- worker->CreateThread();
- if (!worker->thread_) {
- return nullptr;
- }
+ SchedulerBackwardCompatibility backward_compatibility,
+ InitialState initial_state)
+ : priority_hint_(priority_hint),
+ delegate_(std::move(delegate)),
+ task_tracker_(task_tracker),
+#if defined(OS_WIN)
+ backward_compatibility_(backward_compatibility),
+#endif
+ initial_state_(initial_state) {
+ DCHECK(delegate_);
+ DCHECK(task_tracker_);
+}
+
+bool SchedulerWorker::Start() {
+ AutoSchedulerLock auto_lock(thread_lock_);
+ DCHECK(!started_);
+ DCHECK(!thread_);
+
+ if (should_exit_.IsSet())
+ return true;
+
+ started_ = true;
+
+ if (initial_state_ == InitialState::ALIVE) {
+ CreateThread();
+ return !!thread_;
}
- return worker;
+ return true;
}
void SchedulerWorker::WakeUp() {
@@ -228,13 +242,14 @@ void SchedulerWorker::WakeUp() {
DCHECK(!join_called_for_testing_.IsSet());
if (!thread_)
- CreateThreadAssertSynchronized();
+ CreateThread();
if (thread_)
thread_->WakeUp();
}
void SchedulerWorker::JoinForTesting() {
+ DCHECK(started_);
DCHECK(!join_called_for_testing_.IsSet());
join_called_for_testing_.Set();
@@ -266,27 +281,9 @@ void SchedulerWorker::Cleanup() {
// away |thread_| for destruction.
AutoSchedulerLock auto_lock(thread_lock_);
DCHECK(!should_exit_.IsSet());
- if (thread_) {
- should_exit_.Set();
+ should_exit_.Set();
+ if (thread_)
thread_->WakeUp();
- }
-}
-
-SchedulerWorker::SchedulerWorker(
- ThreadPriority priority_hint,
- std::unique_ptr<Delegate> delegate,
- TaskTracker* task_tracker,
- SchedulerBackwardCompatibility backward_compatibility)
- : priority_hint_(priority_hint),
- delegate_(std::move(delegate)),
- task_tracker_(task_tracker)
-#if defined(OS_WIN)
- ,
- backward_compatibility_(backward_compatibility)
-#endif
-{
- DCHECK(delegate_);
- DCHECK(task_tracker_);
}
SchedulerWorker::~SchedulerWorker() {
@@ -322,12 +319,9 @@ std::unique_ptr<SchedulerWorker::Thread> SchedulerWorker::DetachThreadObject(
}
void SchedulerWorker::CreateThread() {
- thread_ = Thread::Create(make_scoped_refptr(this));
-}
-
-void SchedulerWorker::CreateThreadAssertSynchronized() {
thread_lock_.AssertAcquired();
- CreateThread();
+ if (started_)
+ thread_ = Thread::Create(make_scoped_refptr(this));
}
bool SchedulerWorker::ShouldExit() {
« no previous file with comments | « base/task_scheduler/scheduler_worker.h ('k') | base/task_scheduler/scheduler_worker_pool_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698