OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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 #include "chrome/browser/sync/glue/ui_model_worker.h" | 5 #include "chrome/browser/sync/glue/ui_model_worker.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "base/synchronization/waitable_event.h" | 10 #include "base/synchronization/waitable_event.h" |
(...skipping 26 matching lines...) Expand all Loading... |
37 *error_info = work.Run(); | 37 *error_info = work.Run(); |
38 | 38 |
39 // Notify the UIModelWorker that scheduled us that we have run | 39 // Notify the UIModelWorker that scheduled us that we have run |
40 // successfully. | 40 // successfully. |
41 scheduler->OnTaskCompleted(); | 41 scheduler->OnTaskCompleted(); |
42 work_done->Signal(); // Unblock the syncer thread that scheduled us. | 42 work_done->Signal(); // Unblock the syncer thread that scheduled us. |
43 } | 43 } |
44 | 44 |
45 } // namespace | 45 } // namespace |
46 | 46 |
| 47 UIModelWorker::UIModelWorker() |
| 48 : state_(WORKING), |
| 49 syncapi_has_shutdown_(false), |
| 50 syncapi_event_(&lock_) { |
| 51 } |
| 52 |
| 53 void UIModelWorker::Stop() { |
| 54 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 55 |
| 56 base::AutoLock lock(lock_); |
| 57 DCHECK_EQ(state_, WORKING); |
| 58 |
| 59 // We're on our own now, the beloved UI MessageLoop is no longer running. |
| 60 // Any tasks scheduled or to be scheduled on the UI MessageLoop will not run. |
| 61 state_ = RUNNING_MANUAL_SHUTDOWN_PUMP; |
| 62 |
| 63 // Drain any final tasks manually until the SyncerThread tells us it has |
| 64 // totally finished. There should only ever be 0 or 1 tasks Run() here. |
| 65 while (!syncapi_has_shutdown_) { |
| 66 if (!pending_work_.is_null()) |
| 67 pending_work_.Run(); // OnTaskCompleted will set reset |pending_work_|. |
| 68 |
| 69 // Wait for either a new task or SyncerThread termination. |
| 70 syncapi_event_.Wait(); |
| 71 } |
| 72 |
| 73 state_ = STOPPED; |
| 74 } |
| 75 |
47 SyncerError UIModelWorker::DoWorkAndWaitUntilDone( | 76 SyncerError UIModelWorker::DoWorkAndWaitUntilDone( |
48 const WorkCallback& work) { | 77 const WorkCallback& work) { |
49 // In most cases, this method is called in WORKING state. It is possible this | 78 // In most cases, this method is called in WORKING state. It is possible this |
50 // gets called when we are in the RUNNING_MANUAL_SHUTDOWN_PUMP state, because | 79 // gets called when we are in the RUNNING_MANUAL_SHUTDOWN_PUMP state, because |
51 // the UI loop has initiated shutdown but the syncer hasn't got the memo yet. | 80 // the UI loop has initiated shutdown but the syncer hasn't got the memo yet. |
52 // This is fine, the work will get scheduled and run normally or run by our | 81 // This is fine, the work will get scheduled and run normally or run by our |
53 // code handling this case in Stop(). Note there _no_ way we can be in here | 82 // code handling this case in Stop(). Note there _no_ way we can be in here |
54 // with state_ = STOPPED, so it is safe to read / compare in this case. | 83 // with state_ = STOPPED, so it is safe to read / compare in this case. |
55 CHECK_NE(ANNOTATE_UNPROTECTED_READ(state_), STOPPED); | 84 CHECK_NE(ANNOTATE_UNPROTECTED_READ(state_), STOPPED); |
56 SyncerError error_info; | 85 SyncerError error_info; |
(...skipping 18 matching lines...) Expand all Loading... |
75 pending_work_.Reset(); | 104 pending_work_.Reset(); |
76 syncapi_event_.Signal(); | 105 syncapi_event_.Signal(); |
77 return error_info; | 106 return error_info; |
78 } | 107 } |
79 } | 108 } |
80 syncapi_event_.Signal(); // Notify that the syncapi produced work for us. | 109 syncapi_event_.Signal(); // Notify that the syncapi produced work for us. |
81 work_done.Wait(); | 110 work_done.Wait(); |
82 return error_info; | 111 return error_info; |
83 } | 112 } |
84 | 113 |
85 UIModelWorker::UIModelWorker() | 114 ModelSafeGroup UIModelWorker::GetModelSafeGroup() { |
86 : state_(WORKING), | 115 return GROUP_UI; |
87 syncapi_has_shutdown_(false), | |
88 syncapi_event_(&lock_) { | |
89 } | |
90 | |
91 UIModelWorker::~UIModelWorker() { | |
92 DCHECK_EQ(state_, STOPPED); | |
93 } | 116 } |
94 | 117 |
95 void UIModelWorker::OnSyncerShutdownComplete() { | 118 void UIModelWorker::OnSyncerShutdownComplete() { |
96 base::AutoLock lock(lock_); | 119 base::AutoLock lock(lock_); |
97 // The SyncerThread has terminated and we are no longer needed by syncapi. | 120 // The SyncerThread has terminated and we are no longer needed by syncapi. |
98 // The UI loop initiated shutdown and is (or will be) waiting in Stop(). | 121 // The UI loop initiated shutdown and is (or will be) waiting in Stop(). |
99 // We could either be WORKING or RUNNING_MANUAL_SHUTDOWN_PUMP, depending | 122 // We could either be WORKING or RUNNING_MANUAL_SHUTDOWN_PUMP, depending |
100 // on where we timeslice the UI thread in Stop; but we can't be STOPPED, | 123 // on where we timeslice the UI thread in Stop; but we can't be STOPPED, |
101 // because that would imply OnSyncerShutdownComplete already signaled. | 124 // because that would imply OnSyncerShutdownComplete already signaled. |
102 DCHECK_NE(state_, STOPPED); | 125 DCHECK_NE(state_, STOPPED); |
103 | 126 |
104 syncapi_has_shutdown_ = true; | 127 syncapi_has_shutdown_ = true; |
105 syncapi_event_.Signal(); | 128 syncapi_event_.Signal(); |
106 } | 129 } |
107 | 130 |
108 void UIModelWorker::Stop() { | 131 UIModelWorker::~UIModelWorker() { |
109 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 132 DCHECK_EQ(state_, STOPPED); |
110 | |
111 base::AutoLock lock(lock_); | |
112 DCHECK_EQ(state_, WORKING); | |
113 | |
114 // We're on our own now, the beloved UI MessageLoop is no longer running. | |
115 // Any tasks scheduled or to be scheduled on the UI MessageLoop will not run. | |
116 state_ = RUNNING_MANUAL_SHUTDOWN_PUMP; | |
117 | |
118 // Drain any final tasks manually until the SyncerThread tells us it has | |
119 // totally finished. There should only ever be 0 or 1 tasks Run() here. | |
120 while (!syncapi_has_shutdown_) { | |
121 if (!pending_work_.is_null()) | |
122 pending_work_.Run(); // OnTaskCompleted will set reset |pending_work_|. | |
123 | |
124 // Wait for either a new task or SyncerThread termination. | |
125 syncapi_event_.Wait(); | |
126 } | |
127 | |
128 state_ = STOPPED; | |
129 } | |
130 | |
131 ModelSafeGroup UIModelWorker::GetModelSafeGroup() { | |
132 return GROUP_UI; | |
133 } | 133 } |
134 | 134 |
135 } // namespace browser_sync | 135 } // namespace browser_sync |
OLD | NEW |