| OLD | NEW |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 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 #ifndef COMPONENTS_SYNC_ENGINE_MODEL_SAFE_WORKER_H_ | 5 #ifndef COMPONENTS_SYNC_ENGINE_MODEL_SAFE_WORKER_H_ |
| 6 #define COMPONENTS_SYNC_ENGINE_MODEL_SAFE_WORKER_H_ | 6 #define COMPONENTS_SYNC_ENGINE_MODEL_SAFE_WORKER_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <string> | 10 #include <string> |
| 11 #include <vector> | 11 #include <vector> |
| 12 | 12 |
| 13 #include "base/callback.h" | 13 #include "base/callback.h" |
| 14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 15 #include "base/message_loop/message_loop.h" | 15 #include "base/message_loop/message_loop.h" |
| 16 #include "base/single_thread_task_runner.h" | 16 #include "base/single_thread_task_runner.h" |
| 17 #include "base/synchronization/lock.h" | 17 #include "base/synchronization/lock.h" |
| 18 #include "base/synchronization/waitable_event.h" | |
| 19 #include "components/sync/base/model_type.h" | 18 #include "components/sync/base/model_type.h" |
| 20 #include "components/sync/base/syncer_error.h" | 19 #include "components/sync/base/syncer_error.h" |
| 21 | 20 |
| 22 namespace base { | 21 namespace base { |
| 23 class DictionaryValue; | 22 class DictionaryValue; |
| 24 } // namespace base | 23 } // namespace base |
| 25 | 24 |
| 26 namespace syncer { | 25 namespace syncer { |
| 27 | 26 |
| 28 typedef base::Callback<enum SyncerError(void)> WorkCallback; | 27 typedef base::Callback<enum SyncerError(void)> WorkCallback; |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 93 | 92 |
| 94 protected: | 93 protected: |
| 95 explicit ModelSafeWorker(WorkerLoopDestructionObserver* observer); | 94 explicit ModelSafeWorker(WorkerLoopDestructionObserver* observer); |
| 96 ~ModelSafeWorker() override; | 95 ~ModelSafeWorker() override; |
| 97 | 96 |
| 98 // Any time the Syncer performs model modifications (e.g employing a | 97 // Any time the Syncer performs model modifications (e.g employing a |
| 99 // WriteTransaction), it should be done by this method to ensure it is done | 98 // WriteTransaction), it should be done by this method to ensure it is done |
| 100 // from a model-safe thread. | 99 // from a model-safe thread. |
| 101 virtual SyncerError DoWorkAndWaitUntilDoneImpl(const WorkCallback& work) = 0; | 100 virtual SyncerError DoWorkAndWaitUntilDoneImpl(const WorkCallback& work) = 0; |
| 102 | 101 |
| 103 base::WaitableEvent* work_done_or_stopped() { return &work_done_or_stopped_; } | |
| 104 | |
| 105 // Return true if the worker was stopped. Thread safe. | 102 // Return true if the worker was stopped. Thread safe. |
| 106 bool IsStopped(); | 103 bool IsStopped(); |
| 107 | 104 |
| 108 // Subclass should call this in RegisterForLoopDestruction() from the loop | 105 // Subclass should call this in RegisterForLoopDestruction() from the loop |
| 109 // where work is done. | 106 // where work is done. |
| 110 void SetWorkingLoopToCurrent(); | 107 void SetWorkingLoopToCurrent(); |
| 111 | 108 |
| 112 private: | 109 private: |
| 113 friend class base::RefCountedThreadSafe<ModelSafeWorker>; | 110 friend class base::RefCountedThreadSafe<ModelSafeWorker>; |
| 114 | 111 |
| 115 void UnregisterForLoopDestructionAsync( | 112 void UnregisterForLoopDestructionAsync( |
| 116 base::Callback<void(ModelSafeGroup)> unregister_done_callback); | 113 base::Callback<void(ModelSafeGroup)> unregister_done_callback); |
| 117 | 114 |
| 118 // Whether the worker should/can do more work. Set when sync is disabled or | 115 // Whether the worker should/can do more work. Set when sync is disabled or |
| 119 // when the worker's working thread is to be destroyed. | 116 // when the worker's working thread is to be destroyed. |
| 120 base::Lock stopped_lock_; | 117 base::Lock stopped_lock_; |
| 121 bool stopped_; | 118 bool stopped_; |
| 122 | 119 |
| 123 // Signal set when work on native thread is finished or when native thread | |
| 124 // is to be destroyed so no more work can be done. | |
| 125 base::WaitableEvent work_done_or_stopped_; | |
| 126 | |
| 127 // Notified when working thread of the worker is to be destroyed. | 120 // Notified when working thread of the worker is to be destroyed. |
| 128 WorkerLoopDestructionObserver* observer_; | 121 WorkerLoopDestructionObserver* observer_; |
| 129 | 122 |
| 130 // Remember working loop for posting task to unregister destruction | 123 // Remember working loop for posting task to unregister destruction |
| 131 // observation from sync thread when shutting down sync. | 124 // observation from sync thread when shutting down sync. |
| 132 base::Lock working_task_runner_lock_; | 125 base::Lock working_task_runner_lock_; |
| 133 scoped_refptr<base::SingleThreadTaskRunner> working_task_runner_; | 126 scoped_refptr<base::SingleThreadTaskRunner> working_task_runner_; |
| 134 | 127 |
| 135 // Callback passed with UnregisterForLoopDestruction. Normally this | 128 // Callback passed with UnregisterForLoopDestruction. Normally this |
| 136 // remains unset/unused and is stored only if |working_task_runner_| isn't | 129 // remains unset/unused and is stored only if |working_task_runner_| isn't |
| (...skipping 16 matching lines...) Expand all Loading... |
| 153 const ModelSafeRoutingInfo& routing_info); | 146 const ModelSafeRoutingInfo& routing_info); |
| 154 | 147 |
| 155 ModelTypeSet GetRoutingInfoTypes(const ModelSafeRoutingInfo& routing_info); | 148 ModelTypeSet GetRoutingInfoTypes(const ModelSafeRoutingInfo& routing_info); |
| 156 | 149 |
| 157 ModelSafeGroup GetGroupForModelType(const ModelType type, | 150 ModelSafeGroup GetGroupForModelType(const ModelType type, |
| 158 const ModelSafeRoutingInfo& routes); | 151 const ModelSafeRoutingInfo& routes); |
| 159 | 152 |
| 160 } // namespace syncer | 153 } // namespace syncer |
| 161 | 154 |
| 162 #endif // COMPONENTS_SYNC_ENGINE_MODEL_SAFE_WORKER_H_ | 155 #endif // COMPONENTS_SYNC_ENGINE_MODEL_SAFE_WORKER_H_ |
| OLD | NEW |