| 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/ui_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 { |
| 11 | 11 |
| 12 void BookmarkModelWorker::CallDoWorkFromModelSafeThreadAndWait( | 12 void UIModelWorker::DoWorkAndWaitUntilDone(Closure* work) { |
| 13 ModelSafeWorkerInterface::Visitor* visitor) { | |
| 14 // It is possible this gets called when we are in the STOPPING state, because | 13 // It is possible this gets called when we are in the STOPPING state, because |
| 15 // the UI loop has initiated shutdown but the syncer hasn't got the memo yet. | 14 // the UI loop has initiated shutdown but the syncer hasn't got the memo yet. |
| 16 // This is fine, the work will get scheduled and run normally or run by our | 15 // This is fine, the work will get scheduled and run normally or run by our |
| 17 // code handling this case in Stop(). | 16 // code handling this case in Stop(). |
| 18 DCHECK_NE(state_, STOPPED); | 17 DCHECK_NE(state_, STOPPED); |
| 19 if (state_ == STOPPED) | 18 if (state_ == STOPPED) |
| 20 return; | 19 return; |
| 21 if (MessageLoop::current() == bookmark_model_loop_) { | 20 if (MessageLoop::current() == ui_loop_) { |
| 22 DLOG(WARNING) << "CallDoWorkFromModelSafeThreadAndWait called from " | 21 DLOG(WARNING) << "DoWorkAndWaitUntilDone called from " |
| 23 << "bookmark_model_loop_. Probably a nested invocation?"; | 22 << "ui_loop_. Probably a nested invocation?"; |
| 24 visitor->DoWork(); | 23 work->Run(); |
| 25 return; | 24 return; |
| 26 } | 25 } |
| 27 | 26 |
| 28 // Create an unsignaled event to wait on. | 27 // Create an unsignaled event to wait on. |
| 29 base::WaitableEvent work_done(false, false); | 28 base::WaitableEvent work_done(false, false); |
| 30 { | 29 { |
| 31 // We lock only to avoid PostTask'ing a NULL pending_work_ (because it | 30 // 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). | 31 // could get Run() in Stop() and call OnTaskCompleted before we post). |
| 33 // The task is owned by the message loop as per usual. | 32 // The task is owned by the message loop as per usual. |
| 34 AutoLock lock(lock_); | 33 AutoLock lock(lock_); |
| 35 DCHECK(!pending_work_); | 34 DCHECK(!pending_work_); |
| 36 pending_work_ = new CallDoWorkAndSignalTask(visitor, &work_done, this); | 35 pending_work_ = new CallDoWorkAndSignalTask(work, &work_done, this); |
| 37 bookmark_model_loop_->PostTask(FROM_HERE, pending_work_); | 36 ui_loop_->PostTask(FROM_HERE, pending_work_); |
| 38 } | 37 } |
| 39 syncapi_event_.Signal(); // Notify that the syncapi produced work for us. | 38 syncapi_event_.Signal(); // Notify that the syncapi produced work for us. |
| 40 work_done.Wait(); | 39 work_done.Wait(); |
| 41 } | 40 } |
| 42 | 41 |
| 43 BookmarkModelWorker::~BookmarkModelWorker() { | 42 UIModelWorker::~UIModelWorker() { |
| 44 DCHECK_EQ(state_, STOPPED); | 43 DCHECK_EQ(state_, STOPPED); |
| 45 } | 44 } |
| 46 | 45 |
| 47 void BookmarkModelWorker::OnSyncerShutdownComplete() { | 46 void UIModelWorker::OnSyncerShutdownComplete() { |
| 48 AutoLock lock(lock_); | 47 AutoLock lock(lock_); |
| 49 // The SyncerThread has terminated and we are no longer needed by syncapi. | 48 // The SyncerThread has terminated and we are no longer needed by syncapi. |
| 50 // The UI loop initiated shutdown and is (or will be) waiting in Stop(). | 49 // The UI loop initiated shutdown and is (or will be) waiting in Stop(). |
| 51 // We could either be WORKING or RUNNING_MANUAL_SHUTDOWN_PUMP, depending | 50 // We could either be WORKING or RUNNING_MANUAL_SHUTDOWN_PUMP, depending |
| 52 // on where we timeslice the UI thread in Stop; but we can't be STOPPED, | 51 // on where we timeslice the UI thread in Stop; but we can't be STOPPED, |
| 53 // because that would imply OnSyncerShutdownComplete already signaled. | 52 // because that would imply OnSyncerShutdownComplete already signaled. |
| 54 DCHECK_NE(state_, STOPPED); | 53 DCHECK_NE(state_, STOPPED); |
| 55 | 54 |
| 56 syncapi_has_shutdown_ = true; | 55 syncapi_has_shutdown_ = true; |
| 57 syncapi_event_.Signal(); | 56 syncapi_event_.Signal(); |
| 58 } | 57 } |
| 59 | 58 |
| 60 void BookmarkModelWorker::Stop() { | 59 void UIModelWorker::Stop() { |
| 61 DCHECK_EQ(MessageLoop::current(), bookmark_model_loop_); | 60 DCHECK_EQ(MessageLoop::current(), ui_loop_); |
| 62 | 61 |
| 63 AutoLock lock(lock_); | 62 AutoLock lock(lock_); |
| 64 DCHECK_EQ(state_, WORKING); | 63 DCHECK_EQ(state_, WORKING); |
| 65 | 64 |
| 66 // We're on our own now, the beloved UI MessageLoop is no longer running. | 65 // We're on our own now, the beloved UI MessageLoop is no longer running. |
| 67 // Any tasks scheduled or to be scheduled on the UI MessageLoop will not run. | 66 // Any tasks scheduled or to be scheduled on the UI MessageLoop will not run. |
| 68 state_ = RUNNING_MANUAL_SHUTDOWN_PUMP; | 67 state_ = RUNNING_MANUAL_SHUTDOWN_PUMP; |
| 69 | 68 |
| 70 // Drain any final tasks manually until the SyncerThread tells us it has | 69 // Drain any final tasks manually until the SyncerThread tells us it has |
| 71 // totally finished. There should only ever be 0 or 1 tasks Run() here. | 70 // totally finished. There should only ever be 0 or 1 tasks Run() here. |
| 72 while (!syncapi_has_shutdown_) { | 71 while (!syncapi_has_shutdown_) { |
| 73 if (pending_work_) | 72 if (pending_work_) |
| 74 pending_work_->Run(); // OnTaskCompleted will set pending_work_ to NULL. | 73 pending_work_->Run(); // OnTaskCompleted will set pending_work_ to NULL. |
| 75 | 74 |
| 76 // Wait for either a new task or SyncerThread termination. | 75 // Wait for either a new task or SyncerThread termination. |
| 77 syncapi_event_.Wait(); | 76 syncapi_event_.Wait(); |
| 78 } | 77 } |
| 79 | 78 |
| 80 state_ = STOPPED; | 79 state_ = STOPPED; |
| 81 } | 80 } |
| 82 | 81 |
| 83 void BookmarkModelWorker::CallDoWorkAndSignalTask::Run() { | 82 void UIModelWorker::CallDoWorkAndSignalTask::Run() { |
| 84 if (!visitor_) { | 83 if (!work_) { |
| 85 // This can happen during tests or cases where there are more than just the | 84 // This can happen during tests or cases where there are more than just the |
| 86 // default BookmarkModelWorker in existence and it gets destroyed before | 85 // default UIModelWorker in existence and it gets destroyed before |
| 87 // the main UI loop has terminated. There is no easy way to assert the | 86 // the main UI loop has terminated. There is no easy way to assert the |
| 88 // loop is running / not running at the moment, so we just provide cancel | 87 // loop is running / not running at the moment, so we just provide cancel |
| 89 // semantics here and short-circuit. | 88 // semantics here and short-circuit. |
| 90 // TODO(timsteele): Maybe we should have the message loop destruction | 89 // TODO(timsteele): Maybe we should have the message loop destruction |
| 91 // observer fire when the loop has ended, just a bit before it | 90 // observer fire when the loop has ended, just a bit before it |
| 92 // actually gets destroyed. | 91 // actually gets destroyed. |
| 93 return; | 92 return; |
| 94 } | 93 } |
| 95 visitor_->DoWork(); | 94 work_->Run(); |
| 96 | 95 |
| 97 // Sever ties with visitor_ to allow the sanity-checking above that we don't | 96 // Sever ties with work_ to allow the sanity-checking above that we don't |
| 98 // get run twice. | 97 // get run twice. |
| 99 visitor_ = NULL; | 98 work_ = NULL; |
| 100 | 99 |
| 101 // Notify the BookmarkModelWorker that scheduled us that we have run | 100 // Notify the UIModelWorker that scheduled us that we have run |
| 102 // successfully. | 101 // successfully. |
| 103 scheduler_->OnTaskCompleted(); | 102 scheduler_->OnTaskCompleted(); |
| 104 work_done_->Signal(); // Unblock the syncer thread that scheduled us. | 103 work_done_->Signal(); // Unblock the syncer thread that scheduled us. |
| 105 } | 104 } |
| 106 | 105 |
| 107 } // namespace browser_sync | 106 } // namespace browser_sync |
| OLD | NEW |