Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 Loading... | |
| 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(stopped_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. | |
|
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.
| |
| 96 stopped_ = true; | |
| 97 } | |
| 98 | |
| 99 SyncerError ModelSafeWorker::DoWorkAndWaitUntilDone(const WorkCallback& work) { | |
| 100 { | |
| 101 base::AutoLock al(stopped_lock_); | |
| 102 if (stopped_) | |
| 103 return CANNOT_DO_WORK; | |
| 104 | |
| 105 CHECK(!work_done_or_stopped_.IsSignaled()); | |
| 106 } | |
| 107 | |
| 108 return DoWorkAndWaitUntilDoneImpl(work); | |
| 109 } | |
| 110 | |
| 111 bool ModelSafeWorker::IsStopped() { | |
| 112 base::AutoLock al(stopped_lock_); | |
| 113 return stopped_; | |
| 114 } | |
| 115 | |
| 116 void ModelSafeWorker::WillDestroyCurrentMessageLoop() { | |
| 117 { | |
| 118 base::AutoLock al(stopped_lock_); | |
| 119 stopped_ = true; | |
| 120 | |
| 121 // Safe to signal because pending work (if any) will not be executed. So | |
| 122 // there should be no harm even the work holds a pointer to the | |
| 123 // ModelChangingSyncerCommand that will be destroyed on sync loop after | |
| 124 // 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.
| |
| 125 work_done_or_stopped_.Signal(); | |
| 126 | |
| 127 DVLOG(1) << ModelSafeGroupToString(GetModelSafeGroup()) | |
| 128 << " worker stops on destruction of its working thread."; | |
| 129 } | |
| 130 | |
| 131 if (observer_) | |
| 132 observer_->OnWorkerLoopDestroyed(GetModelSafeGroup()); | |
| 133 } | |
| 134 | |
| 85 } // namespace syncer | 135 } // namespace syncer |
| OLD | NEW |