Chromium Code Reviews| 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 "components/sync/engine/ui_model_worker.h" | 5 #include "components/sync/engine/ui_model_worker.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| 11 #include "base/callback.h" | 11 #include "base/callback.h" |
| 12 #include "base/synchronization/waitable_event.h" | |
| 13 #include "components/sync/base/scoped_event_signal.h" | 12 #include "components/sync/base/scoped_event_signal.h" |
| 14 | 13 |
| 15 namespace syncer { | 14 namespace syncer { |
| 16 | 15 |
| 17 namespace { | 16 namespace { |
| 18 | 17 |
| 19 void CallDoWorkAndSignalEvent(const WorkCallback& work, | 18 class ScopedEventSignalWithWorker { |
| 20 syncer::ScopedEventSignal scoped_event_signal, | 19 public: |
| 21 SyncerError* error_info) { | 20 ScopedEventSignalWithWorker(scoped_refptr<UIModelWorker> ui_model_worker, |
| 22 *error_info = work.Run(); | 21 base::WaitableEvent* event) |
| 23 // The event in |scoped_event_signal| is signaled at the end of this scope. | 22 : ui_model_worker_(std::move(ui_model_worker)), |
| 23 scoped_event_signal_(event) {} | |
| 24 | |
| 25 ScopedEventSignalWithWorker(ScopedEventSignalWithWorker&&) = default; | |
| 26 ScopedEventSignalWithWorker& operator=(ScopedEventSignalWithWorker&&) = | |
| 27 default; | |
| 28 | |
| 29 bool IsStopped() const { return ui_model_worker_->IsStopped(); } | |
| 30 | |
| 31 private: | |
| 32 // This reference prevents the event in |scoped_event_signal_| from being | |
| 33 // signaled after being deleted. | |
| 34 scoped_refptr<UIModelWorker> ui_model_worker_; | |
| 35 | |
| 36 ScopedEventSignal scoped_event_signal_; | |
| 37 | |
| 38 DISALLOW_COPY_AND_ASSIGN(ScopedEventSignalWithWorker); | |
| 39 }; | |
| 40 | |
| 41 void CallDoWorkAndSignalEvent( | |
| 42 const WorkCallback& work, | |
| 43 ScopedEventSignalWithWorker scoped_event_signal_with_worker, | |
| 44 SyncerError* error_info) { | |
| 45 if (!scoped_event_signal_with_worker.IsStopped()) | |
| 46 *error_info = work.Run(); | |
| 47 // The event in |scoped_event_signal_with_worker| is signaled at the end of | |
| 48 // this scope. | |
| 24 } | 49 } |
| 25 | 50 |
| 26 } // namespace | 51 } // namespace |
| 27 | 52 |
| 28 UIModelWorker::UIModelWorker( | 53 UIModelWorker::UIModelWorker( |
| 29 scoped_refptr<base::SingleThreadTaskRunner> ui_thread) | 54 scoped_refptr<base::SingleThreadTaskRunner> ui_thread) |
| 30 : ui_thread_(std::move(ui_thread)) {} | 55 : ui_thread_(std::move(ui_thread)), |
| 56 work_done_or_abandoned_(base::WaitableEvent::ResetPolicy::MANUAL, | |
|
maxbogue
2016/11/21 17:17:46
I suspect this could be AUTOMATIC and you could om
fdoray
2016/11/21 19:06:11
If it was AUTOMATIC, we would still need a reset w
| |
| 57 base::WaitableEvent::InitialState::NOT_SIGNALED), | |
| 58 stop_requested_(base::WaitableEvent::ResetPolicy::MANUAL, | |
| 59 base::WaitableEvent::InitialState::NOT_SIGNALED) { | |
| 60 sequence_checker_.DetachFromSequence(); | |
| 61 } | |
| 31 | 62 |
| 32 SyncerError UIModelWorker::DoWorkAndWaitUntilDoneImpl( | 63 SyncerError UIModelWorker::DoWorkAndWaitUntilDoneImpl( |
| 33 const WorkCallback& work) { | 64 const WorkCallback& work) { |
| 65 DCHECK(sequence_checker_.CalledOnValidSequence()); | |
| 66 DCHECK(!ui_thread_->BelongsToCurrentThread()); | |
| 67 | |
| 34 SyncerError error_info; | 68 SyncerError error_info; |
| 35 if (ui_thread_->BelongsToCurrentThread()) { | 69 work_done_or_abandoned_.Reset(); |
| 36 DLOG(WARNING) << "DoWorkAndWaitUntilDone called from " | |
| 37 << "ui_loop_. Probably a nested invocation?"; | |
| 38 return work.Run(); | |
| 39 } | |
| 40 | 70 |
| 41 // Signaled when the task is deleted, i.e. after it runs or when it is | 71 if (!ui_thread_->PostTask( |
| 42 // abandoned. | 72 FROM_HERE, |
| 43 base::WaitableEvent work_done_or_abandoned( | 73 base::Bind(&CallDoWorkAndSignalEvent, work, |
| 44 base::WaitableEvent::ResetPolicy::AUTOMATIC, | 74 base::Passed(syncer::ScopedEventSignalWithWorker( |
| 45 base::WaitableEvent::InitialState::NOT_SIGNALED); | 75 this, &work_done_or_abandoned_)), |
| 46 | 76 &error_info))) { |
| 47 if (!ui_thread_->PostTask(FROM_HERE, | |
| 48 base::Bind(&CallDoWorkAndSignalEvent, work, | |
| 49 base::Passed(syncer::ScopedEventSignal( | |
| 50 &work_done_or_abandoned)), | |
| 51 &error_info))) { | |
| 52 DLOG(WARNING) << "Could not post work to UI loop."; | 77 DLOG(WARNING) << "Could not post work to UI loop."; |
| 53 error_info = CANNOT_DO_WORK; | 78 error_info = CANNOT_DO_WORK; |
| 54 return error_info; | 79 return error_info; |
| 55 } | 80 } |
| 56 work_done_or_abandoned.Wait(); | 81 |
| 82 base::WaitableEvent* events[] = {&work_done_or_abandoned_, &stop_requested_}; | |
| 83 base::WaitableEvent::WaitMany(events, arraysize(events)); | |
| 57 | 84 |
| 58 return error_info; | 85 return error_info; |
| 59 } | 86 } |
| 60 | 87 |
| 88 void UIModelWorker::RequestStop() { | |
| 89 DCHECK(ui_thread_->BelongsToCurrentThread()); | |
| 90 ModelSafeWorker::RequestStop(); | |
| 91 stop_requested_.Signal(); | |
| 92 } | |
| 93 | |
| 61 ModelSafeGroup UIModelWorker::GetModelSafeGroup() { | 94 ModelSafeGroup UIModelWorker::GetModelSafeGroup() { |
| 62 return GROUP_UI; | 95 return GROUP_UI; |
| 63 } | 96 } |
| 64 | 97 |
| 65 bool UIModelWorker::IsOnModelThread() { | 98 bool UIModelWorker::IsOnModelThread() { |
| 66 return ui_thread_->BelongsToCurrentThread(); | 99 return ui_thread_->BelongsToCurrentThread(); |
| 67 } | 100 } |
| 68 | 101 |
| 69 UIModelWorker::~UIModelWorker() {} | 102 UIModelWorker::~UIModelWorker() {} |
| 70 | 103 |
| 71 } // namespace syncer | 104 } // namespace syncer |
| OLD | NEW |