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 #include "chrome/browser/sync/glue/bookmark_model_worker.h" | 5 #include "chrome/browser/sync/glue/bookmark_model_worker.h" |
6 | 6 |
7 #include "base/message_loop.h" | 7 #include "base/message_loop.h" |
8 #include "base/waitable_event.h" | 8 #include "base/waitable_event.h" |
9 | 9 |
10 namespace browser_sync { | 10 namespace browser_sync { |
(...skipping 13 matching lines...) Expand all Loading... |
24 visitor->DoWork(); | 24 visitor->DoWork(); |
25 return; | 25 return; |
26 } | 26 } |
27 | 27 |
28 // Create an unsignaled event to wait on. | 28 // Create an unsignaled event to wait on. |
29 base::WaitableEvent work_done(false, false); | 29 base::WaitableEvent work_done(false, false); |
30 { | 30 { |
31 // We lock only to avoid PostTask'ing a NULL pending_work_ (because it | 31 // We lock only to avoid PostTask'ing a NULL pending_work_ (because it |
32 // could get Run() in Stop() and call OnTaskCompleted before we post). | 32 // could get Run() in Stop() and call OnTaskCompleted before we post). |
33 // The task is owned by the message loop as per usual. | 33 // The task is owned by the message loop as per usual. |
34 AutoLock lock(pending_work_lock_); | 34 AutoLock lock(lock_); |
35 DCHECK(!pending_work_); | 35 DCHECK(!pending_work_); |
36 pending_work_ = new CallDoWorkAndSignalTask(visitor, &work_done, this); | 36 pending_work_ = new CallDoWorkAndSignalTask(visitor, &work_done, this); |
37 bookmark_model_loop_->PostTask(FROM_HERE, pending_work_); | 37 bookmark_model_loop_->PostTask(FROM_HERE, pending_work_); |
38 } | 38 } |
39 syncapi_event_.Signal(); // Notify that the syncapi produced work for us. | 39 syncapi_event_.Signal(); // Notify that the syncapi produced work for us. |
40 work_done.Wait(); | 40 work_done.Wait(); |
41 } | 41 } |
42 | 42 |
43 BookmarkModelWorker::~BookmarkModelWorker() { | 43 BookmarkModelWorker::~BookmarkModelWorker() { |
44 DCHECK_EQ(state_, STOPPED); | 44 DCHECK_EQ(state_, STOPPED); |
45 } | 45 } |
46 | 46 |
47 void BookmarkModelWorker::OnSyncerShutdownComplete() { | 47 void BookmarkModelWorker::OnSyncerShutdownComplete() { |
| 48 AutoLock lock(lock_); |
48 // The SyncerThread has terminated and we are no longer needed by syncapi. | 49 // The SyncerThread has terminated and we are no longer needed by syncapi. |
49 // The UI loop initiated shutdown and is (or will be) waiting in Stop(). | 50 // The UI loop initiated shutdown and is (or will be) waiting in Stop(). |
50 // We could either be WORKING or RUNNING_MANUAL_SHUTDOWN_PUMP, depending | 51 // We could either be WORKING or RUNNING_MANUAL_SHUTDOWN_PUMP, depending |
51 // on where we timeslice the UI thread in Stop; but we can't be STOPPED, | 52 // on where we timeslice the UI thread in Stop; but we can't be STOPPED, |
52 // because that would imply NotifySyncapiShutdownComplete already signaled. | 53 // because that would imply OnSyncerShutdownComplete already signaled. |
53 DCHECK_NE(state_, STOPPED); | 54 DCHECK_NE(state_, STOPPED); |
54 | 55 |
55 syncapi_has_shutdown_ = true; | 56 syncapi_has_shutdown_ = true; |
56 syncapi_event_.Signal(); | 57 syncapi_event_.Signal(); |
57 } | 58 } |
58 | 59 |
59 void BookmarkModelWorker::Stop() { | 60 void BookmarkModelWorker::Stop() { |
60 DCHECK_EQ(MessageLoop::current(), bookmark_model_loop_); | 61 DCHECK_EQ(MessageLoop::current(), bookmark_model_loop_); |
| 62 |
| 63 AutoLock lock(lock_); |
61 DCHECK_EQ(state_, WORKING); | 64 DCHECK_EQ(state_, WORKING); |
62 | 65 |
63 // We're on our own now, the beloved UI MessageLoop is no longer running. | 66 // We're on our own now, the beloved UI MessageLoop is no longer running. |
64 // Any tasks scheduled or to be scheduled on the UI MessageLoop will not run. | 67 // Any tasks scheduled or to be scheduled on the UI MessageLoop will not run. |
65 state_ = RUNNING_MANUAL_SHUTDOWN_PUMP; | 68 state_ = RUNNING_MANUAL_SHUTDOWN_PUMP; |
66 | 69 |
67 // Drain any final task manually until the SyncerThread tells us it has | 70 // Drain any final tasks manually until the SyncerThread tells us it has |
68 // totally finished. Note we use a 'while' loop and not 'if'. The main subtle | 71 // totally finished. There should only ever be 0 or 1 tasks Run() here. |
69 // reason for this is that syncapi_event could be signaled the first time we | |
70 // come through due to an old CallDoWork call, and we need to keep looping | |
71 // until the SyncerThread either calls it again or tells us it is done. There | |
72 // should only ever be 0 or 1 tasks Run() here, however. | |
73 while (!syncapi_has_shutdown_) { | 72 while (!syncapi_has_shutdown_) { |
74 { | 73 if (pending_work_) |
75 AutoLock lock(pending_work_lock_); | 74 pending_work_->Run(); // OnTaskCompleted will set pending_work_ to NULL. |
76 if (pending_work_) | 75 |
77 pending_work_->Run(); | 76 // Wait for either a new task or SyncerThread termination. |
78 } | 77 syncapi_event_.Wait(); |
79 syncapi_event_.Wait(); // Signaled either by new task, or SyncerThread | |
80 // termination. | |
81 } | 78 } |
82 | 79 |
83 state_ = STOPPED; | 80 state_ = STOPPED; |
84 } | 81 } |
85 | 82 |
86 void BookmarkModelWorker::CallDoWorkAndSignalTask::Run() { | 83 void BookmarkModelWorker::CallDoWorkAndSignalTask::Run() { |
87 if (!visitor_) { | 84 if (!visitor_) { |
88 // This can happen during tests or cases where there are more than just the | 85 // This can happen during tests or cases where there are more than just the |
89 // default BookmarkModelWorker in existence and it gets destroyed before | 86 // default BookmarkModelWorker in existence and it gets destroyed before |
90 // the main UI loop has terminated. There is no easy way to assert the | 87 // the main UI loop has terminated. There is no easy way to assert the |
(...skipping 10 matching lines...) Expand all Loading... |
101 // get run twice. | 98 // get run twice. |
102 visitor_ = NULL; | 99 visitor_ = NULL; |
103 | 100 |
104 // Notify the BookmarkModelWorker that scheduled us that we have run | 101 // Notify the BookmarkModelWorker that scheduled us that we have run |
105 // successfully. | 102 // successfully. |
106 scheduler_->OnTaskCompleted(); | 103 scheduler_->OnTaskCompleted(); |
107 work_done_->Signal(); // Unblock the syncer thread that scheduled us. | 104 work_done_->Signal(); // Unblock the syncer thread that scheduled us. |
108 } | 105 } |
109 | 106 |
110 } // namespace browser_sync | 107 } // namespace browser_sync |
OLD | NEW |