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 SYNC_INTERNAL_API_PUBLIC_ENGINE_MODEL_SAFE_WORKER_H_ | 5 #ifndef SYNC_INTERNAL_API_PUBLIC_ENGINE_MODEL_SAFE_WORKER_H_ |
6 #define SYNC_INTERNAL_API_PUBLIC_ENGINE_MODEL_SAFE_WORKER_H_ | 6 #define SYNC_INTERNAL_API_PUBLIC_ENGINE_MODEL_SAFE_WORKER_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/callback.h" | 12 #include "base/callback.h" |
13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
14 #include "base/message_loop.h" | |
15 #include "base/synchronization/lock.h" | |
16 #include "base/synchronization/waitable_event.h" | |
14 #include "sync/base/sync_export.h" | 17 #include "sync/base/sync_export.h" |
15 #include "sync/internal_api/public/base/model_type.h" | 18 #include "sync/internal_api/public/base/model_type.h" |
16 #include "sync/internal_api/public/base/model_type_invalidation_map.h" | 19 #include "sync/internal_api/public/base/model_type_invalidation_map.h" |
17 #include "sync/internal_api/public/util/syncer_error.h" | 20 #include "sync/internal_api/public/util/syncer_error.h" |
18 | 21 |
19 namespace base { | 22 namespace base { |
20 class DictionaryValue; | 23 class DictionaryValue; |
21 } // namespace | 24 } // namespace |
22 | 25 |
23 namespace syncer { | 26 namespace syncer { |
(...skipping 13 matching lines...) Expand all Loading... | |
37 GROUP_HISTORY, // Models that live on history thread and are being | 40 GROUP_HISTORY, // Models that live on history thread and are being |
38 // synced. | 41 // synced. |
39 GROUP_PASSWORD, // Models that live on the password thread and are | 42 GROUP_PASSWORD, // Models that live on the password thread and are |
40 // being synced. On windows and linux, this runs on the | 43 // being synced. On windows and linux, this runs on the |
41 // DB thread. | 44 // DB thread. |
42 MODEL_SAFE_GROUP_COUNT, | 45 MODEL_SAFE_GROUP_COUNT, |
43 }; | 46 }; |
44 | 47 |
45 SYNC_EXPORT std::string ModelSafeGroupToString(ModelSafeGroup group); | 48 SYNC_EXPORT std::string ModelSafeGroupToString(ModelSafeGroup group); |
46 | 49 |
50 // WorkerLoopDestructionObserver is notified when the thread where it works | |
51 // is going to be destroyed. | |
52 class WorkerLoopDestructionObserver { | |
53 public: | |
54 virtual void OnWorkerLoopDestroyed(ModelSafeGroup group) = 0; | |
55 }; | |
56 | |
47 // The Syncer uses a ModelSafeWorker for all tasks that could potentially | 57 // The Syncer uses a ModelSafeWorker for all tasks that could potentially |
48 // modify syncable entries (e.g under a WriteTransaction). The ModelSafeWorker | 58 // modify syncable entries (e.g under a WriteTransaction). The ModelSafeWorker |
49 // only knows how to do one thing, and that is take some work (in a fully | 59 // only knows how to do one thing, and that is take some work (in a fully |
50 // pre-bound callback) and have it performed (as in Run()) from a thread which | 60 // pre-bound callback) and have it performed (as in Run()) from a thread which |
51 // is guaranteed to be "model-safe", where "safe" refers to not allowing us to | 61 // is guaranteed to be "model-safe", where "safe" refers to not allowing us to |
52 // cause an embedding application model to fall out of sync with the | 62 // cause an embedding application model to fall out of sync with the |
53 // syncable::Directory due to a race. | 63 // syncable::Directory due to a race. Each ModelSafeWorker is affiliated with |
64 // a thread and does actual work on that thread. On the destruction of that | |
65 // thread, the affiliated worker is effectively disabled to do more | |
66 // work and will notify its observer. | |
54 class SYNC_EXPORT ModelSafeWorker | 67 class SYNC_EXPORT ModelSafeWorker |
55 : public base::RefCountedThreadSafe<ModelSafeWorker> { | 68 : public base::RefCountedThreadSafe<ModelSafeWorker>, |
69 public MessageLoop::DestructionObserver { | |
56 public: | 70 public: |
71 // Subclass should implement to observe destruction of the loop where | |
72 // it actually does work. | |
73 virtual void RegisterForLoopDestruction() = 0; | |
74 | |
75 // If not stopped, call DoWorkAndWaitUntilDoneImpl() to do work. Otherwise | |
76 // return CANNOT_DO_WORK. | |
77 SyncerError DoWorkAndWaitUntilDone(const WorkCallback& work); | |
78 | |
79 // Soft stop worker by setting stopped_ flag. Called when sync is disabled | |
80 // or browser is shutting down. | |
81 void RequestStop(); | |
tim (not reviewing)
2013/05/16 18:15:42
If this is called from the UI thread, and we alway
haitaol1
2013/05/17 16:24:59
lock_ is also used to serialize signal/reset of wo
| |
82 | |
83 virtual ModelSafeGroup GetModelSafeGroup() = 0; | |
84 | |
85 // MessageLoop::DestructionObserver implementation. | |
86 virtual void WillDestroyCurrentMessageLoop() OVERRIDE; | |
87 | |
88 protected: | |
89 friend class base::RefCountedThreadSafe<ModelSafeWorker>; | |
90 | |
91 explicit ModelSafeWorker(WorkerLoopDestructionObserver* observer); | |
92 virtual ~ModelSafeWorker(); | |
93 | |
57 // Any time the Syncer performs model modifications (e.g employing a | 94 // Any time the Syncer performs model modifications (e.g employing a |
58 // WriteTransaction), it should be done by this method to ensure it is done | 95 // WriteTransaction), it should be done by this method to ensure it is done |
59 // from a model-safe thread. | 96 // from a model-safe thread. |
60 virtual SyncerError DoWorkAndWaitUntilDone(const WorkCallback& work) = 0; | 97 virtual SyncerError DoWorkAndWaitUntilDoneImpl( |
98 const WorkCallback& work, base::WaitableEvent* done_or_stopped) = 0; | |
61 | 99 |
62 virtual ModelSafeGroup GetModelSafeGroup() = 0; | 100 // Return true if the worker was stopped. Thread safe. |
63 | 101 bool IsStopped(); |
64 protected: | |
65 virtual ~ModelSafeWorker(); | |
66 | 102 |
67 private: | 103 private: |
68 friend class base::RefCountedThreadSafe<ModelSafeWorker>; | 104 // Whether the worker should/can do more work. Set when sync is disabled or |
105 // when the worker's working thread is to be destroyed. | |
tim (not reviewing)
2013/05/16 18:15:42
Mention what |lock_| protects. I think stopped_lo
haitaol1
2013/05/17 16:24:59
Done.
| |
106 base::Lock lock_; | |
107 bool stopped_; | |
108 | |
109 // Signal set when work on native thread is finished or when native thread | |
110 // is to be destroyed so no more work can be done. | |
111 base::WaitableEvent work_done_or_stopped_; | |
112 | |
113 // Notified when working thread of the worker is to be destroyed. | |
114 WorkerLoopDestructionObserver* observer_; | |
69 }; | 115 }; |
70 | 116 |
71 // A map that details which ModelSafeGroup each ModelType | 117 // A map that details which ModelSafeGroup each ModelType |
72 // belongs to. Routing info can change in response to the user enabling / | 118 // belongs to. Routing info can change in response to the user enabling / |
73 // disabling sync for certain types, as well as model association completions. | 119 // disabling sync for certain types, as well as model association completions. |
74 typedef std::map<ModelType, ModelSafeGroup> ModelSafeRoutingInfo; | 120 typedef std::map<ModelType, ModelSafeGroup> ModelSafeRoutingInfo; |
75 | 121 |
76 // Caller takes ownership of return value. | 122 // Caller takes ownership of return value. |
77 SYNC_EXPORT_PRIVATE base::DictionaryValue* ModelSafeRoutingInfoToValue( | 123 SYNC_EXPORT_PRIVATE base::DictionaryValue* ModelSafeRoutingInfoToValue( |
78 const ModelSafeRoutingInfo& routing_info); | 124 const ModelSafeRoutingInfo& routing_info); |
(...skipping 11 matching lines...) Expand all Loading... | |
90 SYNC_EXPORT ModelTypeSet GetRoutingInfoTypes( | 136 SYNC_EXPORT ModelTypeSet GetRoutingInfoTypes( |
91 const ModelSafeRoutingInfo& routing_info); | 137 const ModelSafeRoutingInfo& routing_info); |
92 | 138 |
93 SYNC_EXPORT ModelSafeGroup GetGroupForModelType( | 139 SYNC_EXPORT ModelSafeGroup GetGroupForModelType( |
94 const ModelType type, | 140 const ModelType type, |
95 const ModelSafeRoutingInfo& routes); | 141 const ModelSafeRoutingInfo& routes); |
96 | 142 |
97 } // namespace syncer | 143 } // namespace syncer |
98 | 144 |
99 #endif // SYNC_INTERNAL_API_PUBLIC_ENGINE_MODEL_SAFE_WORKER_H_ | 145 #endif // SYNC_INTERNAL_API_PUBLIC_ENGINE_MODEL_SAFE_WORKER_H_ |
OLD | NEW |