Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(761)

Side by Side Diff: sync/internal_api/public/engine/model_safe_worker.cc

Issue 640853008: Sync: Avoid deadlock in SyncBackendRegistrar / ModelSafeWorker on sync backend shutdown. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixed linux_clang_tsan warning about lock-order-inversion Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
126 { 126 {
127 base::AutoLock l(working_loop_lock_); 127 base::AutoLock l(working_loop_lock_);
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::Callback<void(ModelSafeGroup)> unregister_done_callback;
137 DCHECK(!working_loop_); 137
138 working_loop_ = base::MessageLoop::current(); 138 {
139 working_loop_set_wait_.Signal(); 139 base::AutoLock l(working_loop_lock_);
140 DCHECK(!working_loop_);
141
142 if (unregister_done_callback_.is_null()) {
143 // Expected case - UnregisterForLoopDestruction hasn't been called yet.
144 base::MessageLoop::current()->AddDestructionObserver(this);
145 working_loop_ = base::MessageLoop::current();
146 } else {
147 // Rare case which is possible when the model type thread remains
148 // blocked for the entire session and UnregisterForLoopDestruction ends
149 // up being called before this method. This method is posted unlike
150 // UnregisterForLoopDestruction - that's why they can end up being called
151 // out of order.
152 // In this case we skip the destruction observer registration
153 // and just invoke the callback stored at UnregisterForLoopDestruction.
154 DCHECK(stopped_);
155 unregister_done_callback = unregister_done_callback_;
156 unregister_done_callback_.Reset();
157 }
158 }
159
160 if (!unregister_done_callback.is_null()) {
161 unregister_done_callback.Run(GetModelSafeGroup());
162 }
140 } 163 }
141 164
142 void ModelSafeWorker::UnregisterForLoopDestruction( 165 void ModelSafeWorker::UnregisterForLoopDestruction(
143 base::Callback<void(ModelSafeGroup)> unregister_done_callback) { 166 base::Callback<void(ModelSafeGroup)> unregister_done_callback) {
144 // Ok to wait until |working_loop_| is set because this is called on sync 167 base::AutoLock l(working_loop_lock_);
145 // loop. 168 if (working_loop_ != NULL) {
146 working_loop_set_wait_.Wait(); 169 // Normal case - observer registration has been already done.
147 170 // Delegate to the sync thread to do the actual unregistration in
148 { 171 // UnregisterForLoopDestructionAsync.
149 base::AutoLock l(working_loop_lock_); 172 DCHECK_NE(base::MessageLoop::current(), working_loop_);
150 if (working_loop_ != NULL) { 173 working_loop_->PostTask(
151 // Should be called on sync loop. 174 FROM_HERE,
152 DCHECK_NE(base::MessageLoop::current(), working_loop_); 175 base::Bind(&ModelSafeWorker::UnregisterForLoopDestructionAsync,
153 working_loop_->PostTask( 176 this,
154 FROM_HERE, 177 unregister_done_callback));
155 base::Bind(&ModelSafeWorker::UnregisterForLoopDestructionAsync, 178 } else {
156 this, unregister_done_callback)); 179 // The working loop is still unknown, probably because the model type
157 } 180 // thread is blocked. Store the callback to be called from
181 // SetWorkingLoopToCurrent.
182 unregister_done_callback_ = unregister_done_callback;
158 } 183 }
159 } 184 }
160 185
161 void ModelSafeWorker::UnregisterForLoopDestructionAsync( 186 void ModelSafeWorker::UnregisterForLoopDestructionAsync(
162 base::Callback<void(ModelSafeGroup)> unregister_done_callback) { 187 base::Callback<void(ModelSafeGroup)> unregister_done_callback) {
163 { 188 {
164 base::AutoLock l(working_loop_lock_); 189 base::AutoLock l(working_loop_lock_);
165 if (!working_loop_) 190 if (!working_loop_)
166 return; 191 return;
167 DCHECK_EQ(base::MessageLoop::current(), working_loop_); 192 DCHECK_EQ(base::MessageLoop::current(), working_loop_);
168 } 193 }
169 194
170 DCHECK(stopped_); 195 DCHECK(stopped_);
171 base::MessageLoop::current()->RemoveDestructionObserver(this); 196 base::MessageLoop::current()->RemoveDestructionObserver(this);
172 unregister_done_callback.Run(GetModelSafeGroup()); 197 unregister_done_callback.Run(GetModelSafeGroup());
173 } 198 }
174 199
175 } // namespace syncer 200 } // namespace syncer
OLDNEW
« no previous file with comments | « sync/internal_api/public/engine/model_safe_worker.h ('k') | sync/internal_api/public/engine/passive_model_worker.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698