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

Side by Side Diff: sync/internal_api/public/engine/model_safe_worker.cc

Issue 14046031: Worker changes to prepare for lock-free shutdown. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 7 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "sync/internal_api/public/engine/model_safe_worker.h" 5 #include "sync/internal_api/public/engine/model_safe_worker.h"
6 6
7 #include "base/json/json_writer.h" 7 #include "base/json/json_writer.h"
8 #include "base/memory/scoped_ptr.h" 8 #include "base/memory/scoped_ptr.h"
9 #include "base/values.h" 9 #include "base/values.h"
10 10
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
73 case GROUP_PASSIVE: 73 case GROUP_PASSIVE:
74 return "GROUP_PASSIVE"; 74 return "GROUP_PASSIVE";
75 case GROUP_PASSWORD: 75 case GROUP_PASSWORD:
76 return "GROUP_PASSWORD"; 76 return "GROUP_PASSWORD";
77 default: 77 default:
78 NOTREACHED(); 78 NOTREACHED();
79 return "INVALID"; 79 return "INVALID";
80 } 80 }
81 } 81 }
82 82
83 ModelSafeWorker::ModelSafeWorker(WorkerLoopDestructionObserver* observer)
84 : stopped_(false),
85 work_done_or_stopped_(false, false),
86 observer_(observer) {}
87
83 ModelSafeWorker::~ModelSafeWorker() {} 88 ModelSafeWorker::~ModelSafeWorker() {}
84 89
90 void ModelSafeWorker::RequestStop() {
91 base::AutoLock al(lock_);
92
93 // Set stop flag but don't signal work_done_or_stopped_ to unblock sync loop
94 // because the worker may be working and depending on sync command object
95 // living on sync thread.
96 stopped_ = true;
97 }
98
99 SyncerError ModelSafeWorker::DoWorkAndWaitUntilDone(const WorkCallback& work) {
100 {
101 base::AutoLock al(lock_);
102 if (stopped_)
103 return CANNOT_DO_WORK;
104
105 // Clear signal before starting work.
106 work_done_or_stopped_.Reset();
107 }
108
109 return DoWorkAndWaitUntilDoneImpl(work, &work_done_or_stopped_);
110 }
111
112 bool ModelSafeWorker::IsStopped() {
113 base::AutoLock al(lock_);
114 return stopped_;
115 }
116
117 void ModelSafeWorker::WillDestroyCurrentMessageLoop() {
118 {
119 base::AutoLock al(lock_);
120 stopped_ = true;
121
122 // Safe to signal because pending work (if any) will not be executed. So
123 // there should be no harm even the work holds a pointer to the
124 // ModelChangingSyncerCommand that will be destroyed on sync loop after
125 // the signal is set.
126 work_done_or_stopped_.Signal();
127
128 DLOG(INFO) << ModelSafeGroupToString(GetModelSafeGroup())
129 << " worker stops on destruction of its working thread.";
130 }
131
132 if (observer_)
133 observer_->OnWorkerLoopDestroyed(GetModelSafeGroup());
134 }
135
85 } // namespace syncer 136 } // namespace syncer
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698