| 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 "components/sync/engine/model_safe_worker.h" | 5 #include "components/sync/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/threading/thread_task_runner_handle.h" | 9 #include "base/threading/thread_task_runner_handle.h" |
| 10 #include "base/values.h" | 10 #include "base/values.h" |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 66 case GROUP_NON_BLOCKING: | 66 case GROUP_NON_BLOCKING: |
| 67 return "GROUP_NON_BLOCKING"; | 67 return "GROUP_NON_BLOCKING"; |
| 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_(base::WaitableEvent::ResetPolicy::AUTOMATIC, | |
| 77 base::WaitableEvent::InitialState::NOT_SIGNALED), | |
| 78 observer_(observer) {} | 76 observer_(observer) {} |
| 79 | 77 |
| 80 ModelSafeWorker::~ModelSafeWorker() {} | 78 ModelSafeWorker::~ModelSafeWorker() {} |
| 81 | 79 |
| 82 void ModelSafeWorker::RequestStop() { | 80 void ModelSafeWorker::RequestStop() { |
| 83 base::AutoLock al(stopped_lock_); | 81 base::AutoLock al(stopped_lock_); |
| 84 | 82 |
| 85 // Set stop flag but don't signal work_done_or_stopped_ to unblock sync loop | 83 // Set stop flag. This prevents any *further* tasks from being posted to |
| 86 // because the worker may be working and depending on sync command object | 84 // worker threads (see DoWorkAndWaitUntilDone below), but note that one may |
| 87 // living on sync thread. This prevents any *further* tasks from being posted | 85 // already be posted. |
| 88 // to worker threads (see DoWorkAndWaitUntilDone below), but note that one | |
| 89 // may already be posted. | |
| 90 stopped_ = true; | 86 stopped_ = true; |
| 91 } | 87 } |
| 92 | 88 |
| 93 SyncerError ModelSafeWorker::DoWorkAndWaitUntilDone(const WorkCallback& work) { | 89 SyncerError ModelSafeWorker::DoWorkAndWaitUntilDone(const WorkCallback& work) { |
| 94 { | 90 { |
| 95 base::AutoLock al(stopped_lock_); | 91 base::AutoLock al(stopped_lock_); |
| 96 if (stopped_) | 92 if (stopped_) |
| 97 return CANNOT_DO_WORK; | 93 return CANNOT_DO_WORK; |
| 98 | |
| 99 CHECK(!work_done_or_stopped_.IsSignaled()); | |
| 100 } | 94 } |
| 101 | 95 |
| 102 return DoWorkAndWaitUntilDoneImpl(work); | 96 return DoWorkAndWaitUntilDoneImpl(work); |
| 103 } | 97 } |
| 104 | 98 |
| 105 bool ModelSafeWorker::IsStopped() { | 99 bool ModelSafeWorker::IsStopped() { |
| 106 base::AutoLock al(stopped_lock_); | 100 base::AutoLock al(stopped_lock_); |
| 107 return stopped_; | 101 return stopped_; |
| 108 } | 102 } |
| 109 | 103 |
| 110 void ModelSafeWorker::WillDestroyCurrentMessageLoop() { | 104 void ModelSafeWorker::WillDestroyCurrentMessageLoop() { |
| 111 { | 105 { |
| 112 base::AutoLock al(stopped_lock_); | 106 base::AutoLock al(stopped_lock_); |
| 113 stopped_ = true; | 107 stopped_ = true; |
| 114 | 108 |
| 115 // Must signal to unblock syncer if it's waiting for a posted task to | |
| 116 // finish. At this point, all pending tasks posted to the loop have been | |
| 117 // destroyed (see MessageLoop::~MessageLoop). So syncer will be blocked | |
| 118 // indefinitely without signaling here. | |
| 119 work_done_or_stopped_.Signal(); | |
| 120 | |
| 121 DVLOG(1) << ModelSafeGroupToString(GetModelSafeGroup()) | 109 DVLOG(1) << ModelSafeGroupToString(GetModelSafeGroup()) |
| 122 << " worker stops on destruction of its working thread."; | 110 << " worker stops on destruction of its working thread."; |
| 123 } | 111 } |
| 124 | 112 |
| 125 { | 113 { |
| 126 base::AutoLock l(working_task_runner_lock_); | 114 base::AutoLock l(working_task_runner_lock_); |
| 127 working_task_runner_ = nullptr; | 115 working_task_runner_ = nullptr; |
| 128 } | 116 } |
| 129 | 117 |
| 130 if (observer_) | 118 if (observer_) |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 189 return; | 177 return; |
| 190 DCHECK(working_task_runner_->BelongsToCurrentThread()); | 178 DCHECK(working_task_runner_->BelongsToCurrentThread()); |
| 191 } | 179 } |
| 192 | 180 |
| 193 DCHECK(stopped_); | 181 DCHECK(stopped_); |
| 194 base::MessageLoop::current()->RemoveDestructionObserver(this); | 182 base::MessageLoop::current()->RemoveDestructionObserver(this); |
| 195 unregister_done_callback.Run(GetModelSafeGroup()); | 183 unregister_done_callback.Run(GetModelSafeGroup()); |
| 196 } | 184 } |
| 197 | 185 |
| 198 } // namespace syncer | 186 } // namespace syncer |
| OLD | NEW |