| 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/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/json/json_writer.h" | 8 #include "base/json/json_writer.h" |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/values.h" | 10 #include "base/values.h" |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 68 default: | 68 default: |
| 69 NOTREACHED(); | 69 NOTREACHED(); |
| 70 return "INVALID"; | 70 return "INVALID"; |
| 71 } | 71 } |
| 72 } | 72 } |
| 73 | 73 |
| 74 ModelSafeWorker::ModelSafeWorker(WorkerLoopDestructionObserver* observer) | 74 ModelSafeWorker::ModelSafeWorker(WorkerLoopDestructionObserver* observer) |
| 75 : stopped_(false), | 75 : stopped_(false), |
| 76 work_done_or_stopped_(false, false), | 76 work_done_or_stopped_(false, false), |
| 77 observer_(observer), | 77 observer_(observer), |
| 78 working_loop_(NULL) { | 78 working_loop_(NULL), |
| 79 } | 79 working_loop_set_wait_(true, false) {} |
| 80 | 80 |
| 81 ModelSafeWorker::~ModelSafeWorker() {} | 81 ModelSafeWorker::~ModelSafeWorker() {} |
| 82 | 82 |
| 83 void ModelSafeWorker::RequestStop() { | 83 void ModelSafeWorker::RequestStop() { |
| 84 base::AutoLock al(stopped_lock_); | 84 base::AutoLock al(stopped_lock_); |
| 85 | 85 |
| 86 // Set stop flag but don't signal work_done_or_stopped_ to unblock sync loop | 86 // Set stop flag but don't signal work_done_or_stopped_ to unblock sync loop |
| 87 // because the worker may be working and depending on sync command object | 87 // because the worker may be working and depending on sync command object |
| 88 // living on sync thread. his prevents any *further* tasks from being posted | 88 // living on sync thread. his prevents any *further* tasks from being posted |
| 89 // to worker threads (see DoWorkAndWaitUntilDone below), but note that one | 89 // to worker threads (see DoWorkAndWaitUntilDone below), but note that one |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 128 working_loop_ = NULL; | 128 working_loop_ = NULL; |
| 129 } | 129 } |
| 130 | 130 |
| 131 if (observer_) | 131 if (observer_) |
| 132 observer_->OnWorkerLoopDestroyed(GetModelSafeGroup()); | 132 observer_->OnWorkerLoopDestroyed(GetModelSafeGroup()); |
| 133 } | 133 } |
| 134 | 134 |
| 135 void ModelSafeWorker::SetWorkingLoopToCurrent() { | 135 void ModelSafeWorker::SetWorkingLoopToCurrent() { |
| 136 base::AutoLock l(working_loop_lock_); | 136 base::AutoLock l(working_loop_lock_); |
| 137 DCHECK(!working_loop_); | 137 DCHECK(!working_loop_); |
| 138 | 138 working_loop_ = base::MessageLoop::current(); |
| 139 if (unregister_done_callback_.is_null()) { | 139 working_loop_set_wait_.Signal(); |
| 140 // Expected case - UnregisterForLoopDestruction hasn't been called yet. | |
| 141 base::MessageLoop::current()->AddDestructionObserver(this); | |
| 142 working_loop_ = base::MessageLoop::current(); | |
| 143 } else { | |
| 144 // Rare case which is possible when the model type thread remains | |
| 145 // blocked for the entire session and UnregisterForLoopDestruction ends | |
| 146 // up being called before this method. This method is posted unlike | |
| 147 // UnregisterForLoopDestruction - that's why they can end up being called | |
| 148 // out of order. | |
| 149 // In this case we skip the destruction observer registration | |
| 150 // and just invoke the callback stored at UnregisterForLoopDestruction. | |
| 151 DCHECK(stopped_); | |
| 152 unregister_done_callback_.Run(GetModelSafeGroup()); | |
| 153 } | |
| 154 } | 140 } |
| 155 | 141 |
| 156 void ModelSafeWorker::UnregisterForLoopDestruction( | 142 void ModelSafeWorker::UnregisterForLoopDestruction( |
| 157 base::Callback<void(ModelSafeGroup)> unregister_done_callback) { | 143 base::Callback<void(ModelSafeGroup)> unregister_done_callback) { |
| 158 base::AutoLock l(working_loop_lock_); | 144 // Ok to wait until |working_loop_| is set because this is called on sync |
| 159 if (working_loop_ != NULL) { | 145 // loop. |
| 160 // Normal case - observer registration has been already done. | 146 working_loop_set_wait_.Wait(); |
| 161 // Delegate to the sync thread to do the actual unregistration in | 147 |
| 162 // UnregisterForLoopDestructionAsync. | 148 { |
| 163 DCHECK_NE(base::MessageLoop::current(), working_loop_); | 149 base::AutoLock l(working_loop_lock_); |
| 164 working_loop_->PostTask( | 150 if (working_loop_ != NULL) { |
| 165 FROM_HERE, | 151 // Should be called on sync loop. |
| 166 base::Bind(&ModelSafeWorker::UnregisterForLoopDestructionAsync, | 152 DCHECK_NE(base::MessageLoop::current(), working_loop_); |
| 167 this, | 153 working_loop_->PostTask( |
| 168 unregister_done_callback)); | 154 FROM_HERE, |
| 169 } else { | 155 base::Bind(&ModelSafeWorker::UnregisterForLoopDestructionAsync, |
| 170 // The working loop is still unknown, probably because the model type | 156 this, unregister_done_callback)); |
| 171 // thread is blocked. Store the callback to be called from | 157 } |
| 172 // SetWorkingLoopToCurrent. | |
| 173 unregister_done_callback_ = unregister_done_callback; | |
| 174 } | 158 } |
| 175 } | 159 } |
| 176 | 160 |
| 177 void ModelSafeWorker::UnregisterForLoopDestructionAsync( | 161 void ModelSafeWorker::UnregisterForLoopDestructionAsync( |
| 178 base::Callback<void(ModelSafeGroup)> unregister_done_callback) { | 162 base::Callback<void(ModelSafeGroup)> unregister_done_callback) { |
| 179 { | 163 { |
| 180 base::AutoLock l(working_loop_lock_); | 164 base::AutoLock l(working_loop_lock_); |
| 181 if (!working_loop_) | 165 if (!working_loop_) |
| 182 return; | 166 return; |
| 183 DCHECK_EQ(base::MessageLoop::current(), working_loop_); | 167 DCHECK_EQ(base::MessageLoop::current(), working_loop_); |
| 184 } | 168 } |
| 185 | 169 |
| 186 DCHECK(stopped_); | 170 DCHECK(stopped_); |
| 187 base::MessageLoop::current()->RemoveDestructionObserver(this); | 171 base::MessageLoop::current()->RemoveDestructionObserver(this); |
| 188 unregister_done_callback.Run(GetModelSafeGroup()); | 172 unregister_done_callback.Run(GetModelSafeGroup()); |
| 189 } | 173 } |
| 190 | 174 |
| 191 } // namespace syncer | 175 } // namespace syncer |
| OLD | NEW |