Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(728)

Side by Side Diff: components/sync/engine/model_safe_worker.h

Issue 2471183003: Do not observe MessageLoop destruction from ModelSafeWorker. (Closed)
Patch Set: CR maxbogue #23 Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « components/sync/driver/sync_client.h ('k') | components/sync/engine/model_safe_worker.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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>
12 11
13 #include "base/callback.h" 12 #include "base/callback_forward.h"
13 #include "base/macros.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/synchronization/atomic_flag.h"
16 #include "base/single_thread_task_runner.h"
17 #include "base/synchronization/lock.h"
18 #include "components/sync/base/model_type.h" 16 #include "components/sync/base/model_type.h"
19 #include "components/sync/base/syncer_error.h" 17 #include "components/sync/base/syncer_error.h"
20 18
21 namespace base { 19 namespace base {
22 class DictionaryValue; 20 class DictionaryValue;
23 } // namespace base 21 } // namespace base
24 22
25 namespace syncer { 23 namespace syncer {
26 24
27 typedef base::Callback<enum SyncerError(void)> WorkCallback; 25 typedef base::Callback<enum SyncerError(void)> WorkCallback;
(...skipping 11 matching lines...) Expand all
39 // being synced. On windows and linux, this runs on the 37 // being synced. On windows and linux, this runs on the
40 // DB thread. 38 // DB thread.
41 GROUP_NON_BLOCKING, // Models that correspond to non-blocking types. These 39 GROUP_NON_BLOCKING, // Models that correspond to non-blocking types. These
42 // models always stay in GROUP_NON_BLOCKING; changes are 40 // models always stay in GROUP_NON_BLOCKING; changes are
43 // forwarded to these models without ModelSafeWorker/ 41 // forwarded to these models without ModelSafeWorker/
44 // SyncBackendRegistrar involvement. 42 // SyncBackendRegistrar involvement.
45 }; 43 };
46 44
47 std::string ModelSafeGroupToString(ModelSafeGroup group); 45 std::string ModelSafeGroupToString(ModelSafeGroup group);
48 46
49 // WorkerLoopDestructionObserver is notified when the thread where it works
50 // is going to be destroyed.
51 class WorkerLoopDestructionObserver {
52 public:
53 virtual void OnWorkerLoopDestroyed(ModelSafeGroup group) = 0;
54 };
55
56 // The Syncer uses a ModelSafeWorker for all tasks that could potentially 47 // The Syncer uses a ModelSafeWorker for all tasks that could potentially
57 // modify syncable entries (e.g under a WriteTransaction). The ModelSafeWorker 48 // modify syncable entries (e.g under a WriteTransaction). The ModelSafeWorker
58 // only knows how to do one thing, and that is take some work (in a fully 49 // only knows how to do one thing, and that is take some work (in a fully
59 // pre-bound callback) and have it performed (as in Run()) from a thread which 50 // pre-bound callback) and have it performed (as in Run()) from a thread which
60 // is guaranteed to be "model-safe", where "safe" refers to not allowing us to 51 // is guaranteed to be "model-safe", where "safe" refers to not allowing us to
61 // cause an embedding application model to fall out of sync with the 52 // cause an embedding application model to fall out of sync with the
62 // syncable::Directory due to a race. Each ModelSafeWorker is affiliated with 53 // syncable::Directory due to a race. Each ModelSafeWorker is affiliated with
63 // a thread and does actual work on that thread. On the destruction of that 54 // a thread and does actual work on that thread.
64 // thread, the affiliated worker is effectively disabled to do more 55 class ModelSafeWorker : public base::RefCountedThreadSafe<ModelSafeWorker> {
65 // work and will notify its observer.
66 class ModelSafeWorker : public base::RefCountedThreadSafe<ModelSafeWorker>,
67 public base::MessageLoop::DestructionObserver {
68 public: 56 public:
69 // Subclass should implement to observe destruction of the loop where
70 // it actually does work. Called on UI thread immediately after worker is
71 // created.
72 virtual void RegisterForLoopDestruction() = 0;
73
74 // Called on sync loop from SyncBackendRegistrar::ShutDown(). Post task to
75 // working loop to stop observing loop destruction and invoke
76 // |unregister_done_callback|.
77 virtual void UnregisterForLoopDestruction(
78 base::Callback<void(ModelSafeGroup)> unregister_done_callback);
79
80 // If not stopped, call DoWorkAndWaitUntilDoneImpl() to do work. Otherwise 57 // If not stopped, call DoWorkAndWaitUntilDoneImpl() to do work. Otherwise
81 // return CANNOT_DO_WORK. 58 // return CANNOT_DO_WORK.
82 SyncerError DoWorkAndWaitUntilDone(const WorkCallback& work); 59 SyncerError DoWorkAndWaitUntilDone(const WorkCallback& work);
83 60
84 // Soft stop worker by setting stopped_ flag. Called when sync is disabled 61 // Soft stop worker by setting stopped_ flag. Called when sync is disabled
85 // or browser is shutting down. Called on UI loop. 62 // or browser is shutting down. Called on UI loop.
86 virtual void RequestStop(); 63 virtual void RequestStop();
87 64
88 virtual ModelSafeGroup GetModelSafeGroup() = 0; 65 virtual ModelSafeGroup GetModelSafeGroup() = 0;
89 66
90 // MessageLoop::DestructionObserver implementation.
91 void WillDestroyCurrentMessageLoop() override;
92
93 protected: 67 protected:
94 explicit ModelSafeWorker(WorkerLoopDestructionObserver* observer); 68 ModelSafeWorker();
95 ~ModelSafeWorker() override; 69 virtual ~ModelSafeWorker();
96 70
97 // Any time the Syncer performs model modifications (e.g employing a 71 // Any time the Syncer performs model modifications (e.g employing a
98 // WriteTransaction), it should be done by this method to ensure it is done 72 // WriteTransaction), it should be done by this method to ensure it is done
99 // from a model-safe thread. 73 // from a model-safe thread.
100 virtual SyncerError DoWorkAndWaitUntilDoneImpl(const WorkCallback& work) = 0; 74 virtual SyncerError DoWorkAndWaitUntilDoneImpl(const WorkCallback& work) = 0;
101 75
102 // Return true if the worker was stopped. Thread safe. 76 // Return true if the worker was stopped. Thread safe.
103 bool IsStopped(); 77 bool IsStopped();
104 78
105 // Subclass should call this in RegisterForLoopDestruction() from the loop
106 // where work is done.
107 void SetWorkingLoopToCurrent();
108
109 private: 79 private:
110 friend class base::RefCountedThreadSafe<ModelSafeWorker>; 80 friend class base::RefCountedThreadSafe<ModelSafeWorker>;
111 81
112 void UnregisterForLoopDestructionAsync( 82 // Whether the worker should do more work. Set when sync is disabled.
113 base::Callback<void(ModelSafeGroup)> unregister_done_callback); 83 base::AtomicFlag stopped_;
114 84
115 // Whether the worker should/can do more work. Set when sync is disabled or 85 DISALLOW_COPY_AND_ASSIGN(ModelSafeWorker);
116 // when the worker's working thread is to be destroyed.
117 base::Lock stopped_lock_;
118 bool stopped_;
119
120 // Notified when working thread of the worker is to be destroyed.
121 WorkerLoopDestructionObserver* observer_;
122
123 // Remember working loop for posting task to unregister destruction
124 // observation from sync thread when shutting down sync.
125 base::Lock working_task_runner_lock_;
126 scoped_refptr<base::SingleThreadTaskRunner> working_task_runner_;
127
128 // Callback passed with UnregisterForLoopDestruction. Normally this
129 // remains unset/unused and is stored only if |working_task_runner_| isn't
130 // initialized by the time UnregisterForLoopDestruction is called.
131 // It is safe to copy and thread safe.
132 // See comments in model_safe_worker.cc for more details.
133 base::Callback<void(ModelSafeGroup)> unregister_done_callback_;
134 }; 86 };
135 87
136 // A map that details which ModelSafeGroup each ModelType 88 // A map that details which ModelSafeGroup each ModelType
137 // belongs to. Routing info can change in response to the user enabling / 89 // belongs to. Routing info can change in response to the user enabling /
138 // disabling sync for certain types, as well as model association completions. 90 // disabling sync for certain types, as well as model association completions.
139 typedef std::map<ModelType, ModelSafeGroup> ModelSafeRoutingInfo; 91 typedef std::map<ModelType, ModelSafeGroup> ModelSafeRoutingInfo;
140 92
141 // Caller takes ownership of return value. 93 // Caller takes ownership of return value.
142 std::unique_ptr<base::DictionaryValue> ModelSafeRoutingInfoToValue( 94 std::unique_ptr<base::DictionaryValue> ModelSafeRoutingInfoToValue(
143 const ModelSafeRoutingInfo& routing_info); 95 const ModelSafeRoutingInfo& routing_info);
144 96
145 std::string ModelSafeRoutingInfoToString( 97 std::string ModelSafeRoutingInfoToString(
146 const ModelSafeRoutingInfo& routing_info); 98 const ModelSafeRoutingInfo& routing_info);
147 99
148 ModelTypeSet GetRoutingInfoTypes(const ModelSafeRoutingInfo& routing_info); 100 ModelTypeSet GetRoutingInfoTypes(const ModelSafeRoutingInfo& routing_info);
149 101
150 ModelSafeGroup GetGroupForModelType(const ModelType type, 102 ModelSafeGroup GetGroupForModelType(const ModelType type,
151 const ModelSafeRoutingInfo& routes); 103 const ModelSafeRoutingInfo& routes);
152 104
153 } // namespace syncer 105 } // namespace syncer
154 106
155 #endif // COMPONENTS_SYNC_ENGINE_MODEL_SAFE_WORKER_H_ 107 #endif // COMPONENTS_SYNC_ENGINE_MODEL_SAFE_WORKER_H_
OLDNEW
« no previous file with comments | « components/sync/driver/sync_client.h ('k') | components/sync/engine/model_safe_worker.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698