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/json/json_writer.h" | 7 #include "base/json/json_writer.h" |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "base/values.h" | 9 #include "base/values.h" |
10 | 10 |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
73 case GROUP_PASSIVE: | 73 case GROUP_PASSIVE: |
74 return "GROUP_PASSIVE"; | 74 return "GROUP_PASSIVE"; |
75 case GROUP_PASSWORD: | 75 case GROUP_PASSWORD: |
76 return "GROUP_PASSWORD"; | 76 return "GROUP_PASSWORD"; |
77 default: | 77 default: |
78 NOTREACHED(); | 78 NOTREACHED(); |
79 return "INVALID"; | 79 return "INVALID"; |
80 } | 80 } |
81 } | 81 } |
82 | 82 |
| 83 ModelSafeWorker::ModelSafeWorker(WorkerLoopDestructionObserver* observer) |
| 84 : stopped_(false), |
| 85 work_done_or_stopped_(false, false), |
| 86 observer_(observer) {} |
| 87 |
83 ModelSafeWorker::~ModelSafeWorker() {} | 88 ModelSafeWorker::~ModelSafeWorker() {} |
84 | 89 |
| 90 void ModelSafeWorker::RequestStop() { |
| 91 base::AutoLock al(stopped_lock_); |
| 92 |
| 93 // Set stop flag but don't signal work_done_or_stopped_ to unblock sync loop |
| 94 // because the worker may be working and depending on sync command object |
| 95 // living on sync thread. his prevents any *further* tasks from being posted |
| 96 // to worker threads (see DoWorkAndWaitUntilDone below), but note that one |
| 97 // may already be posted. |
| 98 stopped_ = true; |
| 99 } |
| 100 |
| 101 SyncerError ModelSafeWorker::DoWorkAndWaitUntilDone(const WorkCallback& work) { |
| 102 { |
| 103 base::AutoLock al(stopped_lock_); |
| 104 if (stopped_) |
| 105 return CANNOT_DO_WORK; |
| 106 |
| 107 CHECK(!work_done_or_stopped_.IsSignaled()); |
| 108 } |
| 109 |
| 110 return DoWorkAndWaitUntilDoneImpl(work); |
| 111 } |
| 112 |
| 113 bool ModelSafeWorker::IsStopped() { |
| 114 base::AutoLock al(stopped_lock_); |
| 115 return stopped_; |
| 116 } |
| 117 |
| 118 void ModelSafeWorker::WillDestroyCurrentMessageLoop() { |
| 119 { |
| 120 base::AutoLock al(stopped_lock_); |
| 121 stopped_ = true; |
| 122 |
| 123 // Must signal to unblock syncer if it's waiting for a posted task to |
| 124 // finish. At this point, all pending tasks posted to the loop have been |
| 125 // destroyed (see MessageLoop::~MessageLoop). So syncer will be blocked |
| 126 // indefinitely without signaling here. |
| 127 work_done_or_stopped_.Signal(); |
| 128 |
| 129 DVLOG(1) << ModelSafeGroupToString(GetModelSafeGroup()) |
| 130 << " worker stops on destruction of its working thread."; |
| 131 } |
| 132 |
| 133 if (observer_) |
| 134 observer_->OnWorkerLoopDestroyed(GetModelSafeGroup()); |
| 135 } |
| 136 |
85 } // namespace syncer | 137 } // namespace syncer |
OLD | NEW |