Chromium Code Reviews| 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..4b69425d35610296ae6582fee1dc5b6763516066 100644 |
| --- a/sync/internal_api/public/engine/model_safe_worker.cc |
| +++ b/sync/internal_api/public/engine/model_safe_worker.cc |
| @@ -80,6 +80,56 @@ std::string ModelSafeGroupToString(ModelSafeGroup group) { |
| } |
| } |
| +ModelSafeWorker::ModelSafeWorker(WorkerLoopDestructionObserver* observer) |
| + : stopped_(false), |
| + work_done_or_stopped_(false, false), |
| + observer_(observer) {} |
| + |
| ModelSafeWorker::~ModelSafeWorker() {} |
| +void ModelSafeWorker::RequestStop() { |
| + base::AutoLock al(stopped_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. |
|
tim (not reviewing)
2013/05/20 17:22:39
Add to comment: "This prevents any *further* task
haitaol1
2013/05/23 16:07:29
Done.
|
| + stopped_ = true; |
| +} |
| + |
| +SyncerError ModelSafeWorker::DoWorkAndWaitUntilDone(const WorkCallback& work) { |
| + { |
| + base::AutoLock al(stopped_lock_); |
| + if (stopped_) |
| + return CANNOT_DO_WORK; |
| + |
| + CHECK(!work_done_or_stopped_.IsSignaled()); |
| + } |
| + |
| + return DoWorkAndWaitUntilDoneImpl(work); |
| +} |
| + |
| +bool ModelSafeWorker::IsStopped() { |
| + base::AutoLock al(stopped_lock_); |
| + return stopped_; |
| +} |
| + |
| +void ModelSafeWorker::WillDestroyCurrentMessageLoop() { |
| + { |
| + base::AutoLock al(stopped_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. |
|
tim (not reviewing)
2013/05/20 17:22:39
This is a subtle expectation. For example, it's n
haitaol1
2013/05/23 16:07:29
Resolved per offline discussion. Updated comments.
|
| + work_done_or_stopped_.Signal(); |
| + |
| + DVLOG(1) << ModelSafeGroupToString(GetModelSafeGroup()) |
| + << " worker stops on destruction of its working thread."; |
| + } |
| + |
| + if (observer_) |
| + observer_->OnWorkerLoopDestroyed(GetModelSafeGroup()); |
| +} |
| + |
| } // namespace syncer |