| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 CHROME_BROWSER_SYNC_GLUE_BOOKMARK_MODEL_WORKER_H_ | 5 #ifndef CHROME_BROWSER_SYNC_GLUE_UI_MODEL_WORKER_H_ |
| 6 #define CHROME_BROWSER_SYNC_GLUE_BOOKMARK_MODEL_WORKER_H_ | 6 #define CHROME_BROWSER_SYNC_GLUE_UI_MODEL_WORKER_H_ |
| 7 | 7 |
| 8 #include "base/condition_variable.h" | 8 #include "base/condition_variable.h" |
| 9 #include "base/lock.h" | 9 #include "base/lock.h" |
| 10 #include "base/task.h" | 10 #include "base/task.h" |
| 11 #include "base/waitable_event.h" | 11 #include "base/waitable_event.h" |
| 12 #include "chrome/browser/sync/engine/syncapi.h" | 12 #include "chrome/browser/sync/engine/syncapi.h" |
| 13 #include "chrome/browser/sync/engine/model_safe_worker.h" |
| 14 #include "chrome/browser/sync/util/closure.h" |
| 13 | 15 |
| 14 class MessageLoop; | 16 class MessageLoop; |
| 15 | 17 |
| 16 namespace browser_sync { | 18 namespace browser_sync { |
| 17 | 19 |
| 18 // A ModelSafeWorker for bookmarks that accepts work requests from the syncapi | 20 // A ModelSafeWorker for UI models (e.g. bookmarks) that accepts work requests |
| 19 // that need to be fulfilled from the MessageLoop home to the BookmarkModel | 21 // from the syncapi that need to be fulfilled from the MessageLoop home to the |
| 20 // (this is typically the "main" UI thread). | 22 // native model. |
| 21 // | 23 // |
| 22 // Lifetime note: Instances of this class will generally be owned by the | 24 // Lifetime note: Instances of this class will generally be owned by the |
| 23 // SyncerThread. When the SyncerThread _object_ is destroyed, the | 25 // SyncerThread. When the SyncerThread _object_ is destroyed, the |
| 24 // BookmarkModelWorker will be destroyed. The SyncerThread object is destroyed | 26 // UIModelWorker will be destroyed. The SyncerThread object is destroyed |
| 25 // after the actual syncer pthread has exited. | 27 // after the actual syncer pthread has exited. |
| 26 class BookmarkModelWorker | 28 class UIModelWorker : public browser_sync::ModelSafeWorker { |
| 27 : public sync_api::ModelSafeWorkerInterface { | |
| 28 public: | 29 public: |
| 29 explicit BookmarkModelWorker(MessageLoop* bookmark_model_loop) | 30 explicit UIModelWorker(MessageLoop* ui_loop) |
| 30 : state_(WORKING), | 31 : state_(WORKING), |
| 31 pending_work_(NULL), | 32 pending_work_(NULL), |
| 32 syncapi_has_shutdown_(false), | 33 syncapi_has_shutdown_(false), |
| 33 bookmark_model_loop_(bookmark_model_loop), | 34 ui_loop_(ui_loop), |
| 34 syncapi_event_(&lock_) { | 35 syncapi_event_(&lock_) { |
| 35 } | 36 } |
| 36 virtual ~BookmarkModelWorker(); | 37 virtual ~UIModelWorker(); |
| 37 | 38 |
| 38 // A simple task to signal a waitable event after calling DoWork on a visitor. | 39 // A simple task to signal a waitable event after Run()ning a Closure. |
| 39 class CallDoWorkAndSignalTask : public Task { | 40 class CallDoWorkAndSignalTask : public Task { |
| 40 public: | 41 public: |
| 41 CallDoWorkAndSignalTask(ModelSafeWorkerInterface::Visitor* visitor, | 42 CallDoWorkAndSignalTask(Closure* work, |
| 42 base::WaitableEvent* work_done, | 43 base::WaitableEvent* work_done, |
| 43 BookmarkModelWorker* scheduler) | 44 UIModelWorker* scheduler) |
| 44 : visitor_(visitor), work_done_(work_done), scheduler_(scheduler) { | 45 : work_(work), work_done_(work_done), scheduler_(scheduler) { |
| 45 } | 46 } |
| 46 virtual ~CallDoWorkAndSignalTask() { } | 47 virtual ~CallDoWorkAndSignalTask() { } |
| 47 | 48 |
| 48 // Task implementation. | 49 // Task implementation. |
| 49 virtual void Run(); | 50 virtual void Run(); |
| 50 | 51 |
| 51 private: | 52 private: |
| 52 // Task data - a visitor that knows how to DoWork, and a waitable event | 53 // Task data - a closure and a waitable event to signal after the work has |
| 53 // to signal after the work has been done. | 54 // been done. |
| 54 ModelSafeWorkerInterface::Visitor* visitor_; | 55 Closure* work_; |
| 55 base::WaitableEvent* work_done_; | 56 base::WaitableEvent* work_done_; |
| 56 | 57 |
| 57 // The BookmarkModelWorker responsible for scheduling us. | 58 // The UIModelWorker responsible for scheduling us. |
| 58 BookmarkModelWorker* const scheduler_; | 59 UIModelWorker* const scheduler_; |
| 59 | 60 |
| 60 DISALLOW_COPY_AND_ASSIGN(CallDoWorkAndSignalTask); | 61 DISALLOW_COPY_AND_ASSIGN(CallDoWorkAndSignalTask); |
| 61 }; | 62 }; |
| 62 | 63 |
| 63 // Called by the UI thread on shutdown of the sync service. Blocks until | 64 // Called by the UI thread on shutdown of the sync service. Blocks until |
| 64 // the BookmarkModelWorker has safely met termination conditions, namely that | 65 // the UIModelWorker has safely met termination conditions, namely that |
| 65 // no task scheduled by CallDoWorkFromModelSafeThreadAndWait remains un- | 66 // no task scheduled by CallDoWorkFromModelSafeThreadAndWait remains un- |
| 66 // processed and that syncapi will not schedule any further work for us to do. | 67 // processed and that syncapi will not schedule any further work for us to do. |
| 67 void Stop(); | 68 void Stop(); |
| 68 | 69 |
| 69 // ModelSafeWorkerInterface implementation. Called on syncapi SyncerThread. | 70 // ModelSafeWorker implementation. Called on syncapi SyncerThread. |
| 70 virtual void CallDoWorkFromModelSafeThreadAndWait( | 71 virtual void DoWorkAndWaitUntilDone(Closure* work); |
| 71 ModelSafeWorkerInterface::Visitor* visitor); | 72 virtual ModelSafeGroup GetModelSafeGroup() { return GROUP_UI; } |
| 72 | 73 |
| 73 // Upon receiving this idempotent call, the ModelSafeWorkerInterface can | 74 // Upon receiving this idempotent call, the ModelSafeWorker can |
| 74 // assume no work will ever be scheduled again from now on. If it has any work | 75 // assume no work will ever be scheduled again from now on. If it has any work |
| 75 // that it has not yet completed, it must make sure to run it as soon as | 76 // that it has not yet completed, it must make sure to run it as soon as |
| 76 // possible as the Syncer is trying to shut down. Called from the CoreThread. | 77 // possible as the Syncer is trying to shut down. Called from the CoreThread. |
| 77 void OnSyncerShutdownComplete(); | 78 void OnSyncerShutdownComplete(); |
| 78 | 79 |
| 79 // Callback from |pending_work_| to notify us that it has been run. | 80 // Callback from |pending_work_| to notify us that it has been run. |
| 80 // Called on |bookmark_model_loop_|. | 81 // Called on |ui_loop_|. |
| 81 void OnTaskCompleted() { pending_work_ = NULL; } | 82 void OnTaskCompleted() { pending_work_ = NULL; } |
| 82 | 83 |
| 83 private: | 84 private: |
| 84 // The life-cycle of a BookmarkModelWorker in three states. | 85 // The life-cycle of a UIModelWorker in three states. |
| 85 enum State { | 86 enum State { |
| 86 // We hit the ground running in this state and remain until | 87 // We hit the ground running in this state and remain until |
| 87 // the UI loop calls Stop(). | 88 // the UI loop calls Stop(). |
| 88 WORKING, | 89 WORKING, |
| 89 // Stop() sequence has been initiated, but we have not received word that | 90 // Stop() sequence has been initiated, but we have not received word that |
| 90 // the SyncerThread has terminated and doesn't need us anymore. Since the | 91 // the SyncerThread has terminated and doesn't need us anymore. Since the |
| 91 // UI MessageLoop is not running at this point, we manually process any | 92 // UI MessageLoop is not running at this point, we manually process any |
| 92 // last pending_task_ that the Syncer throws at us, effectively dedicating | 93 // last pending_task_ that the Syncer throws at us, effectively dedicating |
| 93 // the UI thread to terminating the Syncer. | 94 // the UI thread to terminating the Syncer. |
| 94 RUNNING_MANUAL_SHUTDOWN_PUMP, | 95 RUNNING_MANUAL_SHUTDOWN_PUMP, |
| (...skipping 10 matching lines...) Expand all Loading... |
| 105 | 106 |
| 106 // We keep a reference to any task we have scheduled so we can gracefully | 107 // We keep a reference to any task we have scheduled so we can gracefully |
| 107 // force them to run if the syncer is trying to shutdown. | 108 // force them to run if the syncer is trying to shutdown. |
| 108 Task* pending_work_; | 109 Task* pending_work_; |
| 109 | 110 |
| 110 // Set by the SyncCoreThread when Syncapi shutdown has completed and the | 111 // Set by the SyncCoreThread when Syncapi shutdown has completed and the |
| 111 // SyncerThread has terminated, so no more work will be scheduled. Read by | 112 // SyncerThread has terminated, so no more work will be scheduled. Read by |
| 112 // the UI thread in Stop(). | 113 // the UI thread in Stop(). |
| 113 bool syncapi_has_shutdown_; | 114 bool syncapi_has_shutdown_; |
| 114 | 115 |
| 115 // The BookmarkModel's home-sweet-home MessageLoop. | 116 // The UI model home-sweet-home MessageLoop. |
| 116 MessageLoop* const bookmark_model_loop_; | 117 MessageLoop* const ui_loop_; |
| 117 | 118 |
| 118 // We use a Lock for all data members and a ConditionVariable to synchronize. | 119 // We use a Lock for all data members and a ConditionVariable to synchronize. |
| 119 // We do this instead of using a WaitableEvent and a bool condition in order | 120 // We do this instead of using a WaitableEvent and a bool condition in order |
| 120 // to guard against races that could arise due to the fact that the lack of a | 121 // to guard against races that could arise due to the fact that the lack of a |
| 121 // barrier permits instructions to be reordered by compiler optimizations. | 122 // barrier permits instructions to be reordered by compiler optimizations. |
| 122 // Possible or not, that route makes for very fragile code due to existence | 123 // Possible or not, that route makes for very fragile code due to existence |
| 123 // of theoretical races. | 124 // of theoretical races. |
| 124 Lock lock_; | 125 Lock lock_; |
| 125 | 126 |
| 126 // Used as a barrier at shutdown to ensure the SyncerThread terminates before | 127 // Used as a barrier at shutdown to ensure the SyncerThread terminates before |
| 127 // we allow the UI thread to return from Stop(). This gets signalled whenever | 128 // we allow the UI thread to return from Stop(). This gets signalled whenever |
| 128 // one of two events occur: a new pending_work_ task was scheduled, or the | 129 // one of two events occur: a new pending_work_ task was scheduled, or the |
| 129 // SyncerThread has terminated. We only care about (1) when we are in Stop(), | 130 // SyncerThread has terminated. We only care about (1) when we are in Stop(), |
| 130 // because we have to manually Run() the task. | 131 // because we have to manually Run() the task. |
| 131 ConditionVariable syncapi_event_; | 132 ConditionVariable syncapi_event_; |
| 132 | 133 |
| 133 DISALLOW_COPY_AND_ASSIGN(BookmarkModelWorker); | 134 DISALLOW_COPY_AND_ASSIGN(UIModelWorker); |
| 134 }; | 135 }; |
| 135 | 136 |
| 136 } // namespace browser_sync | 137 } // namespace browser_sync |
| 137 | 138 |
| 138 #endif // CHROME_BROWSER_SYNC_GLUE_BOOKMARK_MODEL_WORKER_H_ | 139 #endif // CHROME_BROWSER_SYNC_GLUE_UI_MODEL_WORKER_H_ |
| OLD | NEW |