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

Unified Diff: components/sync/engine/model_safe_worker.h

Issue 2782573002: [Sync] Refactor ModelSafeWorker::DoWorkAndWaitUntilDone() to avoid code duplication. (Closed)
Patch Set: self-review Created 3 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 side-by-side diff with in-line comments
Download patch
Index: components/sync/engine/model_safe_worker.h
diff --git a/components/sync/engine/model_safe_worker.h b/components/sync/engine/model_safe_worker.h
index 1565aa279d66eabc45ea9ba71b43c592e7bba767..620297fb2951cdbfa88c4442daef16b850cfb44f 100644
--- a/components/sync/engine/model_safe_worker.h
+++ b/components/sync/engine/model_safe_worker.h
@@ -9,10 +9,12 @@
#include <memory>
#include <string>
-#include "base/callback_forward.h"
+#include "base/callback.h"
+#include "base/callback_helpers.h"
#include "base/macros.h"
#include "base/memory/ref_counted.h"
-#include "base/synchronization/atomic_flag.h"
+#include "base/synchronization/lock.h"
+#include "base/synchronization/waitable_event.h"
#include "components/sync/base/model_type.h"
#include "components/sync/base/syncer_error.h"
@@ -22,7 +24,7 @@ class DictionaryValue;
namespace syncer {
-using WorkCallback = base::Callback<enum SyncerError(void)>;
+using WorkCallback = base::OnceCallback<enum SyncerError(void)>;
enum ModelSafeGroup {
GROUP_PASSIVE = 0, // Models that are just "passively" being synced; e.g.
@@ -54,17 +56,14 @@ std::string ModelSafeGroupToString(ModelSafeGroup group);
// a thread and does actual work on that thread.
class ModelSafeWorker : public base::RefCountedThreadSafe<ModelSafeWorker> {
public:
- // If not stopped, call DoWorkAndWaitUntilDoneImpl() to do work. Otherwise
- // return CANNOT_DO_WORK.
- SyncerError DoWorkAndWaitUntilDone(const WorkCallback& work);
+ // If not stopped, calls ScheduleWork() to schedule |work| and waits until it
+ // is done or abandoned. Otherwise, returns CANNOT_DO_WORK.
+ SyncerError DoWorkAndWaitUntilDone(WorkCallback work);
// Soft stop worker by setting stopped_ flag. Called when sync is disabled
// or browser is shutting down. Called on UI loop.
virtual void RequestStop();
- // Return true if the worker was stopped. Thread safe.
- bool IsStopped();
-
virtual ModelSafeGroup GetModelSafeGroup() = 0;
// Returns true if called on the thread this worker works on.
@@ -74,16 +73,31 @@ class ModelSafeWorker : public base::RefCountedThreadSafe<ModelSafeWorker> {
ModelSafeWorker();
virtual ~ModelSafeWorker();
- // Any time the Syncer performs model modifications (e.g employing a
- // WriteTransaction), it should be done by this method to ensure it is done
- // from a model-safe thread.
- virtual SyncerError DoWorkAndWaitUntilDoneImpl(const WorkCallback& work) = 0;
-
private:
friend class base::RefCountedThreadSafe<ModelSafeWorker>;
- // Whether the worker should do more work. Set when sync is disabled.
- base::AtomicFlag stopped_;
+ // Schedules |work| on the appropriate thread.
+ virtual void ScheduleWork(base::OnceClosure work) = 0;
+
+ void DoWork(WorkCallback work,
+ base::ScopedClosureRunner scoped_closure_runner,
+ SyncerError* error,
+ bool* did_run);
+
+ // Synchronizes access to all members.
+ base::Lock lock_;
+
+ // Signaled when DoWorkAndWaitUntilDone() can return, either because the work
+ // is done, the work has been abandoned or RequestStop() was called while no
+ // work was running. Reset at the beginning of DoWorkAndWaitUntilDone().
+ base::WaitableEvent work_done_or_abandoned_;
+
+ // Whether a WorkCallback is currently running.
+ bool is_work_running_ = false;
+
+ // Whether the worker was stopped. No WorkCallback can start running when this
+ // is true.
+ bool stopped_ = false;
DISALLOW_COPY_AND_ASSIGN(ModelSafeWorker);
};
« no previous file with comments | « components/sync/engine/browser_thread_model_worker_unittest.cc ('k') | components/sync/engine/model_safe_worker.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698