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