Index: sync/internal_api/public/engine/model_safe_worker.cc |
diff --git a/sync/internal_api/public/engine/model_safe_worker.cc b/sync/internal_api/public/engine/model_safe_worker.cc |
index d7bf906ef9f007feec43925789b22a3367a3e15c..5504bf4a4db210a9d17b94984d450656f924904d 100644 |
--- a/sync/internal_api/public/engine/model_safe_worker.cc |
+++ b/sync/internal_api/public/engine/model_safe_worker.cc |
@@ -80,6 +80,62 @@ std::string ModelSafeGroupToString(ModelSafeGroup group) { |
} |
} |
+ModelSafeWorker::ModelSafeWorker(WorkerObserver* observer) |
+ : stopped_(false), |
+ work_done_or_stopped_(false, false), |
+ observer_(observer) {} |
+ |
ModelSafeWorker::~ModelSafeWorker() {} |
+void ModelSafeWorker::RequestStop() { |
tim (not reviewing)
2013/05/14 04:14:29
Who calls RequestStop?
haitaol1
2013/05/15 23:39:21
SyncBackendRegistrar will call this from UI thread
|
+ base::AutoLock al(lock_); |
+ |
+ // Set stop flag but don't signal work_done_or_stopped_ to unblock sync loop |
+ // because the worker may be working and depending on sync command object |
+ // living on sync thread. |
+ stopped_ = true; |
+} |
+ |
+void ModelSafeWorker::Restart() { |
+ base::AutoLock al(lock_); |
+ stopped_ = false; |
+ work_done_or_stopped_.Reset(); |
+} |
+ |
+SyncerError ModelSafeWorker::HandleWork(const WorkCallback& work) { |
+ { |
+ base::AutoLock al(lock_); |
+ if (stopped_) |
+ return CANNOT_DO_WORK; |
+ |
+ // Clear signal before starting work. |
+ work_done_or_stopped_.Reset(); |
+ } |
+ |
+ return DoWorkAndWaitUntilDone(work, &work_done_or_stopped_); |
+} |
+ |
+bool ModelSafeWorker::Stopped() { |
+ base::AutoLock al(lock_); |
+ return stopped_; |
+} |
+ |
+void ModelSafeWorker::WillDestroyCurrentMessageLoop() { |
+ { |
+ base::AutoLock al(lock_); |
+ stopped_ = true; |
+ |
+ // Safe to signal because pending work (if any) will not be executed. So |
+ // there should be no harm even the work holds a pointer to the |
+ // ModelChangingSyncerCommand that will be destroyed on sync loop after |
+ // the signal is set. |
+ work_done_or_stopped_.Signal(); |
tim (not reviewing)
2013/05/14 04:14:29
Still trying to get the hang of the new thread coo
haitaol1
2013/05/15 23:39:21
Signal can be called before Wait. In that case, wa
|
+ |
+ DLOG(INFO) << ModelSafeGroupToString(GetModelSafeGroup()) |
+ << " worker stops on destruction of its working thread."; |
+ } |
+ |
+ observer_->OnWorkerDisabled(GetModelSafeGroup()); |
+} |
+ |
} // namespace syncer |