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 working_loop_set_wait_(true, false) {} | 79 } |
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 working_loop_ = base::MessageLoop::current(); | 138 |
139 working_loop_set_wait_.Signal(); | 139 if (unregister_done_callback_.is_null()) { |
140 // Normal case - UnregisterForLoopDestruction hasn't been | |
141 // called yet (which is possible because UnregisterForLoopDestruction | |
Nicolas Zea
2014/10/17 00:04:02
The comment that this is possible "because Unregis
stanisc
2014/10/17 06:58:02
OK. Clarified/simplified these two comments.
| |
142 // is a direct call while this method comes from a posted call). | |
143 // Add the observer and remember the message loop to be used later | |
144 // to post the unregister callback from UnregisterForLoopDestructionAsync. | |
145 base::MessageLoop::current()->AddDestructionObserver(this); | |
146 working_loop_ = base::MessageLoop::current(); | |
147 } else { | |
148 // Rare case which is possible when the model type thread remains | |
149 // blocked for the entire session and UnregisterForLoopDestruction ends | |
150 // up being called before this method. | |
151 // In this case we skip the destruction observer registration | |
152 // and just invoke the callback postponed at UnregisterForLoopDestruction. | |
153 DCHECK(stopped_); | |
154 unregister_done_callback_.Run(GetModelSafeGroup()); | |
155 } | |
140 } | 156 } |
141 | 157 |
142 void ModelSafeWorker::UnregisterForLoopDestruction( | 158 void ModelSafeWorker::UnregisterForLoopDestruction( |
143 base::Callback<void(ModelSafeGroup)> unregister_done_callback) { | 159 base::Callback<void(ModelSafeGroup)> unregister_done_callback) { |
144 // Ok to wait until |working_loop_| is set because this is called on sync | 160 base::AutoLock l(working_loop_lock_); |
145 // loop. | 161 if (working_loop_ != NULL) { |
146 working_loop_set_wait_.Wait(); | 162 // Normal case - observer registratio has been already done. |
Nicolas Zea
2014/10/17 00:04:01
nit: registratio -> registration
stanisc
2014/10/17 06:58:02
Done.
| |
147 | 163 // Delegate to the sync thread to do the actual unregistration in |
148 { | 164 // UnregisterForLoopDestructionAsync. |
149 base::AutoLock l(working_loop_lock_); | 165 DCHECK_NE(base::MessageLoop::current(), working_loop_); |
150 if (working_loop_ != NULL) { | 166 working_loop_->PostTask( |
151 // Should be called on sync loop. | 167 FROM_HERE, |
152 DCHECK_NE(base::MessageLoop::current(), working_loop_); | 168 base::Bind(&ModelSafeWorker::UnregisterForLoopDestructionAsync, |
153 working_loop_->PostTask( | 169 this, |
154 FROM_HERE, | 170 unregister_done_callback)); |
155 base::Bind(&ModelSafeWorker::UnregisterForLoopDestructionAsync, | 171 } else { |
156 this, unregister_done_callback)); | 172 // The working loop is still unknown, probably because the model type |
157 } | 173 // thread is blocked. Store the callback to be called from |
174 // SetWorkingLoopToCurrent. | |
175 unregister_done_callback_ = unregister_done_callback; | |
158 } | 176 } |
159 } | 177 } |
160 | 178 |
161 void ModelSafeWorker::UnregisterForLoopDestructionAsync( | 179 void ModelSafeWorker::UnregisterForLoopDestructionAsync( |
162 base::Callback<void(ModelSafeGroup)> unregister_done_callback) { | 180 base::Callback<void(ModelSafeGroup)> unregister_done_callback) { |
163 { | 181 { |
164 base::AutoLock l(working_loop_lock_); | 182 base::AutoLock l(working_loop_lock_); |
165 if (!working_loop_) | 183 if (!working_loop_) |
166 return; | 184 return; |
167 DCHECK_EQ(base::MessageLoop::current(), working_loop_); | 185 DCHECK_EQ(base::MessageLoop::current(), working_loop_); |
168 } | 186 } |
169 | 187 |
170 DCHECK(stopped_); | 188 DCHECK(stopped_); |
171 base::MessageLoop::current()->RemoveDestructionObserver(this); | 189 base::MessageLoop::current()->RemoveDestructionObserver(this); |
172 unregister_done_callback.Run(GetModelSafeGroup()); | 190 unregister_done_callback.Run(GetModelSafeGroup()); |
173 } | 191 } |
174 | 192 |
175 } // namespace syncer | 193 } // namespace syncer |
OLD | NEW |