Chromium Code Reviews| 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..12519415a7386794a9252967aee28cd4b60a28be 100644 |
| --- a/components/sync/engine/model_safe_worker.h |
| +++ b/components/sync/engine/model_safe_worker.h |
| @@ -9,10 +9,11 @@ |
| #include <memory> |
| #include <string> |
| -#include "base/callback_forward.h" |
| +#include "base/callback.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" |
| @@ -54,17 +55,16 @@ 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. |
| + class ScopedSignalWorkDoneOrAbandoned; |
| + |
| + // If not stopped, calls ScheduleWork() to schedule |work| and waits until it |
| + // is done or abandoned. Otherwise, returns CANNOT_DO_WORK. |
| SyncerError DoWorkAndWaitUntilDone(const 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 +74,32 @@ 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::Closure work) = 0; |
| + |
| + void DoWork( |
| + const WorkCallback& work, |
|
skym
2017/04/06 00:34:54
The const& is back!
fdoray
2017/04/06 18:10:55
Done.
|
| + ScopedSignalWorkDoneOrAbandoned scoped_signal_work_done_or_abandoned, |
| + 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); |
| }; |