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 // 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 } |
140 } | 154 } |
141 | 155 |
142 void ModelSafeWorker::UnregisterForLoopDestruction( | 156 void ModelSafeWorker::UnregisterForLoopDestruction( |
143 base::Callback<void(ModelSafeGroup)> unregister_done_callback) { | 157 base::Callback<void(ModelSafeGroup)> unregister_done_callback) { |
144 // Ok to wait until |working_loop_| is set because this is called on sync | 158 base::AutoLock l(working_loop_lock_); |
145 // loop. | 159 if (working_loop_ != NULL) { |
146 working_loop_set_wait_.Wait(); | 160 // Normal case - observer registration has been already done. |
147 | 161 // Delegate to the sync thread to do the actual unregistration in |
148 { | 162 // UnregisterForLoopDestructionAsync. |
149 base::AutoLock l(working_loop_lock_); | 163 DCHECK_NE(base::MessageLoop::current(), working_loop_); |
150 if (working_loop_ != NULL) { | 164 working_loop_->PostTask( |
151 // Should be called on sync loop. | 165 FROM_HERE, |
152 DCHECK_NE(base::MessageLoop::current(), working_loop_); | 166 base::Bind(&ModelSafeWorker::UnregisterForLoopDestructionAsync, |
153 working_loop_->PostTask( | 167 this, |
154 FROM_HERE, | 168 unregister_done_callback)); |
155 base::Bind(&ModelSafeWorker::UnregisterForLoopDestructionAsync, | 169 } else { |
156 this, unregister_done_callback)); | 170 // The working loop is still unknown, probably because the model type |
157 } | 171 // thread is blocked. Store the callback to be called from |
| 172 // SetWorkingLoopToCurrent. |
| 173 unregister_done_callback_ = unregister_done_callback; |
158 } | 174 } |
159 } | 175 } |
160 | 176 |
161 void ModelSafeWorker::UnregisterForLoopDestructionAsync( | 177 void ModelSafeWorker::UnregisterForLoopDestructionAsync( |
162 base::Callback<void(ModelSafeGroup)> unregister_done_callback) { | 178 base::Callback<void(ModelSafeGroup)> unregister_done_callback) { |
163 { | 179 { |
164 base::AutoLock l(working_loop_lock_); | 180 base::AutoLock l(working_loop_lock_); |
165 if (!working_loop_) | 181 if (!working_loop_) |
166 return; | 182 return; |
167 DCHECK_EQ(base::MessageLoop::current(), working_loop_); | 183 DCHECK_EQ(base::MessageLoop::current(), working_loop_); |
168 } | 184 } |
169 | 185 |
170 DCHECK(stopped_); | 186 DCHECK(stopped_); |
171 base::MessageLoop::current()->RemoveDestructionObserver(this); | 187 base::MessageLoop::current()->RemoveDestructionObserver(this); |
172 unregister_done_callback.Run(GetModelSafeGroup()); | 188 unregister_done_callback.Run(GetModelSafeGroup()); |
173 } | 189 } |
174 | 190 |
175 } // namespace syncer | 191 } // namespace syncer |
OLD | NEW |