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 void CallDoWorkAndSignalEvent(const WorkCallback& work, |
20 syncer::ScopedEventSignal scoped_event_signal, | 19 syncer::ScopedEventSignal scoped_event_signal, |
21 SyncerError* error_info) { | 20 SyncerError* error_info) { |
22 *error_info = work.Run(); | 21 *error_info = work.Run(); |
23 // The event in |scoped_event_signal| is signaled at the end of this scope. | 22 // The event in |scoped_event_signal| is signaled at the end of this scope. |
24 } | 23 } |
25 | 24 |
26 } // namespace | 25 } // namespace |
27 | 26 |
28 UIModelWorker::UIModelWorker( | 27 UIModelWorker::UIModelWorker( |
29 scoped_refptr<base::SingleThreadTaskRunner> ui_thread) | 28 const scoped_refptr<base::SingleThreadTaskRunner>& ui_thread) |
fdoray
2016/11/11 15:40:28
Pass scoped_refptr by value https://chromium.googl
maxbogue
2016/11/12 03:19:52
Huh. We do that wrong all over sync code apparentl
| |
30 : ui_thread_(std::move(ui_thread)) {} | 29 : ui_thread_(ui_thread), |
30 work_done_or_abandoned_(base::WaitableEvent::ResetPolicy::AUTOMATIC, | |
fdoray
2016/11/11 15:40:28
Should be base::WaitableEvent::ResetPolicy::MANUAL
maxbogue
2016/11/12 03:19:52
Sorta right but for the wrong reason. Needs to be
| |
31 base::WaitableEvent::InitialState::NOT_SIGNALED) { | |
32 } | |
31 | 33 |
32 SyncerError UIModelWorker::DoWorkAndWaitUntilDoneImpl( | 34 SyncerError UIModelWorker::DoWorkAndWaitUntilDoneImpl( |
33 const WorkCallback& work) { | 35 const WorkCallback& work) { |
34 SyncerError error_info; | 36 SyncerError error_info; |
35 if (ui_thread_->BelongsToCurrentThread()) { | 37 if (ui_thread_->BelongsToCurrentThread()) { |
36 DLOG(WARNING) << "DoWorkAndWaitUntilDone called from " | 38 DLOG(WARNING) << "DoWorkAndWaitUntilDone called from " |
37 << "ui_loop_. Probably a nested invocation?"; | 39 << "ui_loop_. Probably a nested invocation?"; |
38 return work.Run(); | 40 return work.Run(); |
39 } | 41 } |
40 | 42 |
41 // Signaled when the task is deleted, i.e. after it runs or when it is | |
42 // abandoned. | |
43 base::WaitableEvent work_done_or_abandoned( | |
44 base::WaitableEvent::ResetPolicy::AUTOMATIC, | |
45 base::WaitableEvent::InitialState::NOT_SIGNALED); | |
46 | |
47 if (!ui_thread_->PostTask(FROM_HERE, | 43 if (!ui_thread_->PostTask(FROM_HERE, |
48 base::Bind(&CallDoWorkAndSignalEvent, work, | 44 base::Bind(&CallDoWorkAndSignalEvent, work, |
49 base::Passed(syncer::ScopedEventSignal( | 45 base::Passed(syncer::ScopedEventSignal( |
50 &work_done_or_abandoned)), | 46 &work_done_or_abandoned_)), |
51 &error_info))) { | 47 &error_info))) { |
52 DLOG(WARNING) << "Could not post work to UI loop."; | 48 DLOG(WARNING) << "Could not post work to UI loop."; |
53 error_info = CANNOT_DO_WORK; | 49 error_info = CANNOT_DO_WORK; |
54 return error_info; | 50 return error_info; |
55 } | 51 } |
56 work_done_or_abandoned.Wait(); | 52 work_done_or_abandoned_.Wait(); |
57 | 53 |
58 return error_info; | 54 return error_info; |
59 } | 55 } |
60 | 56 |
57 void UIModelWorker::RequestStop() { | |
58 ModelSafeWorker::RequestStop(); | |
59 work_done_or_abandoned_.Signal(); | |
fdoray
2016/11/11 15:40:28
In sync_integration_tests, this is called before t
maxbogue
2016/11/12 03:19:52
Done.
| |
60 } | |
61 | |
61 ModelSafeGroup UIModelWorker::GetModelSafeGroup() { | 62 ModelSafeGroup UIModelWorker::GetModelSafeGroup() { |
62 return GROUP_UI; | 63 return GROUP_UI; |
63 } | 64 } |
64 | 65 |
65 bool UIModelWorker::IsOnModelThread() { | 66 bool UIModelWorker::IsOnModelThread() { |
66 return ui_thread_->BelongsToCurrentThread(); | 67 return ui_thread_->BelongsToCurrentThread(); |
67 } | 68 } |
68 | 69 |
69 UIModelWorker::~UIModelWorker() {} | 70 UIModelWorker::~UIModelWorker() {} |
70 | 71 |
71 } // namespace syncer | 72 } // namespace syncer |
OLD | NEW |