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(WorkerObserver* 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() { | |
tim (not reviewing)
2013/05/14 04:14:29
Who calls RequestStop?
haitaol1
2013/05/15 23:39:21
SyncBackendRegistrar will call this from UI thread
| |
91 base::AutoLock al(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. | |
96 stopped_ = true; | |
97 } | |
98 | |
99 void ModelSafeWorker::Restart() { | |
100 base::AutoLock al(lock_); | |
101 stopped_ = false; | |
102 work_done_or_stopped_.Reset(); | |
103 } | |
104 | |
105 SyncerError ModelSafeWorker::HandleWork(const WorkCallback& work) { | |
106 { | |
107 base::AutoLock al(lock_); | |
108 if (stopped_) | |
109 return CANNOT_DO_WORK; | |
110 | |
111 // Clear signal before starting work. | |
112 work_done_or_stopped_.Reset(); | |
113 } | |
114 | |
115 return DoWorkAndWaitUntilDone(work, &work_done_or_stopped_); | |
116 } | |
117 | |
118 bool ModelSafeWorker::Stopped() { | |
119 base::AutoLock al(lock_); | |
120 return stopped_; | |
121 } | |
122 | |
123 void ModelSafeWorker::WillDestroyCurrentMessageLoop() { | |
124 { | |
125 base::AutoLock al(lock_); | |
126 stopped_ = true; | |
127 | |
128 // Safe to signal because pending work (if any) will not be executed. So | |
129 // there should be no harm even the work holds a pointer to the | |
130 // ModelChangingSyncerCommand that will be destroyed on sync loop after | |
131 // the signal is set. | |
132 work_done_or_stopped_.Signal(); | |
tim (not reviewing)
2013/05/14 04:14:29
Still trying to get the hang of the new thread coo
haitaol1
2013/05/15 23:39:21
Signal can be called before Wait. In that case, wa
| |
133 | |
134 DLOG(INFO) << ModelSafeGroupToString(GetModelSafeGroup()) | |
135 << " worker stops on destruction of its working thread."; | |
136 } | |
137 | |
138 observer_->OnWorkerDisabled(GetModelSafeGroup()); | |
139 } | |
140 | |
85 } // namespace syncer | 141 } // namespace syncer |
OLD | NEW |