| OLD | NEW |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 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 "components/sync/driver/glue/browser_thread_model_worker.h" | 5 #include "components/sync/driver/glue/browser_thread_model_worker.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/single_thread_task_runner.h" | 8 #include "base/single_thread_task_runner.h" |
| 9 #include "base/synchronization/waitable_event.h" | 9 #include "base/synchronization/waitable_event.h" |
| 10 | 10 |
| 11 using base::SingleThreadTaskRunner; | 11 using base::SingleThreadTaskRunner; |
| 12 using base::WaitableEvent; | |
| 13 | 12 |
| 14 namespace syncer { | 13 namespace syncer { |
| 15 | 14 |
| 16 BrowserThreadModelWorker::BrowserThreadModelWorker( | 15 BrowserThreadModelWorker::BrowserThreadModelWorker( |
| 17 const scoped_refptr<SingleThreadTaskRunner>& runner, | 16 const scoped_refptr<SingleThreadTaskRunner>& runner, |
| 18 ModelSafeGroup group, | 17 ModelSafeGroup group, |
| 19 WorkerLoopDestructionObserver* observer) | 18 WorkerLoopDestructionObserver* observer) |
| 20 : ModelSafeWorker(observer), runner_(runner), group_(group) {} | 19 : ModelSafeWorker(observer), runner_(runner), group_(group) {} |
| 21 | 20 |
| 22 SyncerError BrowserThreadModelWorker::DoWorkAndWaitUntilDoneImpl( | 21 SyncerError BrowserThreadModelWorker::DoWorkAndWaitUntilDoneImpl( |
| 23 const WorkCallback& work) { | 22 const WorkCallback& work) { |
| 24 SyncerError error = UNSET; | 23 SyncerError error = UNSET; |
| 25 if (runner_->BelongsToCurrentThread()) { | 24 if (runner_->BelongsToCurrentThread()) { |
| 26 DLOG(WARNING) << "Already on thread " << runner_; | 25 DLOG(WARNING) << "Already on thread " << runner_; |
| 27 return work.Run(); | 26 return work.Run(); |
| 28 } | 27 } |
| 29 | 28 |
| 29 // Signaled when the task is deleted, i.e. after it runs or when it is |
| 30 // abandoned. |
| 31 base::WaitableEvent work_done_or_abandoned( |
| 32 base::WaitableEvent::ResetPolicy::AUTOMATIC, |
| 33 base::WaitableEvent::InitialState::NOT_SIGNALED); |
| 34 |
| 30 if (!runner_->PostTask( | 35 if (!runner_->PostTask( |
| 31 FROM_HERE, | 36 FROM_HERE, |
| 32 base::Bind(&BrowserThreadModelWorker::CallDoWorkAndSignalTask, this, | 37 base::Bind( |
| 33 work, work_done_or_stopped(), &error))) { | 38 &BrowserThreadModelWorker::CallDoWorkAndSignalTask, this, work, |
| 39 base::Passed(syncer::ScopedEventSignal(&work_done_or_abandoned)), |
| 40 &error))) { |
| 34 DLOG(WARNING) << "Failed to post task to runner " << runner_; | 41 DLOG(WARNING) << "Failed to post task to runner " << runner_; |
| 35 error = CANNOT_DO_WORK; | 42 error = CANNOT_DO_WORK; |
| 36 return error; | 43 return error; |
| 37 } | 44 } |
| 38 work_done_or_stopped()->Wait(); | 45 work_done_or_abandoned.Wait(); |
| 39 return error; | 46 return error; |
| 40 } | 47 } |
| 41 | 48 |
| 42 ModelSafeGroup BrowserThreadModelWorker::GetModelSafeGroup() { | 49 ModelSafeGroup BrowserThreadModelWorker::GetModelSafeGroup() { |
| 43 return group_; | 50 return group_; |
| 44 } | 51 } |
| 45 | 52 |
| 46 BrowserThreadModelWorker::~BrowserThreadModelWorker() {} | 53 BrowserThreadModelWorker::~BrowserThreadModelWorker() {} |
| 47 | 54 |
| 48 void BrowserThreadModelWorker::RegisterForLoopDestruction() { | 55 void BrowserThreadModelWorker::RegisterForLoopDestruction() { |
| 49 if (runner_->BelongsToCurrentThread()) { | 56 if (runner_->BelongsToCurrentThread()) { |
| 50 SetWorkingLoopToCurrent(); | 57 SetWorkingLoopToCurrent(); |
| 51 } else { | 58 } else { |
| 52 runner_->PostTask( | 59 runner_->PostTask( |
| 53 FROM_HERE, | 60 FROM_HERE, |
| 54 Bind(&BrowserThreadModelWorker::RegisterForLoopDestruction, this)); | 61 Bind(&BrowserThreadModelWorker::RegisterForLoopDestruction, this)); |
| 55 } | 62 } |
| 56 } | 63 } |
| 57 | 64 |
| 58 void BrowserThreadModelWorker::CallDoWorkAndSignalTask(const WorkCallback& work, | 65 void BrowserThreadModelWorker::CallDoWorkAndSignalTask( |
| 59 WaitableEvent* done, | 66 const WorkCallback& work, |
| 60 SyncerError* error) { | 67 syncer::ScopedEventSignal scoped_event_signal, |
| 68 SyncerError* error) { |
| 61 DCHECK(runner_->BelongsToCurrentThread()); | 69 DCHECK(runner_->BelongsToCurrentThread()); |
| 62 if (!IsStopped()) | 70 if (!IsStopped()) |
| 63 *error = work.Run(); | 71 *error = work.Run(); |
| 64 done->Signal(); | 72 // The event in |scoped_event_signal| is signaled at the end of this scope. |
| 65 } | 73 } |
| 66 | 74 |
| 67 } // namespace syncer | 75 } // namespace syncer |
| OLD | NEW |