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

Side by Side Diff: sync/internal_api/public/engine/model_safe_worker.h

Issue 14046031: Worker changes to prepare for lock-free shutdown. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 8 months 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 | Annotate | Revision Log
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 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
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 // WorkerObserver is notified when worker can't do more work because the thread
51 // it works on is going to be destroyed.
52 class WorkerObserver {
53 public:
54 virtual void OnWorkerDisabled(ModelSafeGroup group) = 0;
tim (not reviewing) 2013/05/14 04:14:29 OnWorkerThreadDestroyed would be a better name.
haitaol1 2013/05/15 23:39:21 Done.
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 browser thread and does actual work on that thread. On the destruction
65 // of that thread, the affiliated worker is effectively disabled to do more
66 // work and will notify its observer (SyncBackendRegistrar).
tim (not reviewing) 2013/05/14 04:14:29 Don't mention 'browser' or components from glue li
haitaol1 2013/05/15 23:39:21 Done.
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 observer destruction of the loop where
72 // it actually does work.
73 virtual void RegisterForLoopDestruction() = 0;
tim (not reviewing) 2013/05/14 04:14:29 Who calls this? and when?
haitaol1 2013/05/15 23:39:21 SyncBackendHost will call this after creating work
74
75 // If not stopped, call DoWorkAndWaitUntilDone() to do work. Otherwise
76 // return CANNOT_DO_WORK.
77 SyncerError HandleWork(const WorkCallback& work);
tim (not reviewing) 2013/05/14 04:14:29 DoWorkAndWaitUntilDone is a better, more descripti
haitaol1 2013/05/15 23:39:21 Done.
78
79 // Soft stop worker by setting stopped_ flag. Called when sync is disabled
80 // or browser is shutting down.
81 void RequestStop();
82
83 // Reset state when sync is enabled.
tim (not reviewing) 2013/05/14 04:14:29 When is this used? Is this for when sync was disa
haitaol1 2013/05/15 23:39:21 Removed. Worker will be destroyed when sync is dis
84 void Restart();
85
86 virtual ModelSafeGroup GetModelSafeGroup() = 0;
87
88 // MessageLoop::DestructionObserver implementation.
89 virtual void WillDestroyCurrentMessageLoop() OVERRIDE;
90
91 protected:
92 friend class base::RefCountedThreadSafe<ModelSafeWorker>;
93
94 ModelSafeWorker(WorkerObserver* worker_host);
tim (not reviewing) 2013/05/14 04:14:29 Single argument constructors should be explicit.
haitaol1 2013/05/15 23:39:21 Done.
95 virtual ~ModelSafeWorker();
96
57 // Any time the Syncer performs model modifications (e.g employing a 97 // 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 98 // WriteTransaction), it should be done by this method to ensure it is done
59 // from a model-safe thread. 99 // from a model-safe thread.
60 virtual SyncerError DoWorkAndWaitUntilDone(const WorkCallback& work) = 0; 100 virtual SyncerError DoWorkAndWaitUntilDone(
tim (not reviewing) 2013/05/14 04:14:29 You can call this DoWorkAndWaitIUntilDoneImpl.
haitaol1 2013/05/15 23:39:21 Done.
101 const WorkCallback& work, base::WaitableEvent* done_or_stopped) = 0;
61 102
62 virtual ModelSafeGroup GetModelSafeGroup() = 0; 103 // Return true if the worker was stopped. Thread safe.
63 104 bool Stopped();
tim (not reviewing) 2013/05/14 04:14:29 IsStopped()
haitaol1 2013/05/15 23:39:21 Done.
64 protected:
65 virtual ~ModelSafeWorker();
66 105
67 private: 106 private:
68 friend class base::RefCountedThreadSafe<ModelSafeWorker>; 107 // Whether the worker should/can do more work. Set when sync is disabled or
108 // when the worker's working thread is to be destroyed.
109 base::Lock lock_;
110 bool stopped_;
111
112 // Signal set when work on native thread is finished or when native thread
113 // is to be destroyed so no more work can be done.
114 base::WaitableEvent work_done_or_stopped_;
115
116 // Notified when working thread of the worker is to be destroyed.
117 WorkerObserver* observer_;
69 }; 118 };
70 119
71 // A map that details which ModelSafeGroup each ModelType 120 // A map that details which ModelSafeGroup each ModelType
72 // belongs to. Routing info can change in response to the user enabling / 121 // belongs to. Routing info can change in response to the user enabling /
73 // disabling sync for certain types, as well as model association completions. 122 // disabling sync for certain types, as well as model association completions.
74 typedef std::map<ModelType, ModelSafeGroup> ModelSafeRoutingInfo; 123 typedef std::map<ModelType, ModelSafeGroup> ModelSafeRoutingInfo;
75 124
76 // Caller takes ownership of return value. 125 // Caller takes ownership of return value.
77 SYNC_EXPORT_PRIVATE base::DictionaryValue* ModelSafeRoutingInfoToValue( 126 SYNC_EXPORT_PRIVATE base::DictionaryValue* ModelSafeRoutingInfoToValue(
78 const ModelSafeRoutingInfo& routing_info); 127 const ModelSafeRoutingInfo& routing_info);
(...skipping 11 matching lines...) Expand all
90 SYNC_EXPORT ModelTypeSet GetRoutingInfoTypes( 139 SYNC_EXPORT ModelTypeSet GetRoutingInfoTypes(
91 const ModelSafeRoutingInfo& routing_info); 140 const ModelSafeRoutingInfo& routing_info);
92 141
93 SYNC_EXPORT ModelSafeGroup GetGroupForModelType( 142 SYNC_EXPORT ModelSafeGroup GetGroupForModelType(
94 const ModelType type, 143 const ModelType type,
95 const ModelSafeRoutingInfo& routes); 144 const ModelSafeRoutingInfo& routes);
96 145
97 } // namespace syncer 146 } // namespace syncer
98 147
99 #endif // SYNC_INTERNAL_API_PUBLIC_ENGINE_MODEL_SAFE_WORKER_H_ 148 #endif // SYNC_INTERNAL_API_PUBLIC_ENGINE_MODEL_SAFE_WORKER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698