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 // |worker| owns the event backing |scoped_event_signal|, so it's important that |
20 syncer::ScopedEventSignal scoped_event_signal, | 19 // it comes after in the parameter list so it gets destroyed after. |
21 SyncerError* error_info) { | 20 void DoWorkAndSignalEvent(const WorkCallback& work, |
| 21 syncer::ScopedEventSignal scoped_event_signal, |
| 22 scoped_refptr<ModelSafeWorker> worker, |
| 23 SyncerError* error_info) { |
| 24 DCHECK(worker->IsOnModelThread()); |
| 25 if (worker->IsStopped()) { |
| 26 // Don't set the error here; the pointer may no longer be valid. |
| 27 return; |
| 28 } |
| 29 |
22 *error_info = work.Run(); | 30 *error_info = work.Run(); |
23 // The event in |scoped_event_signal| is signaled at the end of this scope. | 31 // The event in |scoped_event_signal| is signaled at the end of this scope. |
24 } | 32 } |
25 | 33 |
26 } // namespace | 34 } // namespace |
27 | 35 |
28 UIModelWorker::UIModelWorker( | 36 UIModelWorker::UIModelWorker( |
29 scoped_refptr<base::SingleThreadTaskRunner> ui_thread) | 37 scoped_refptr<base::SingleThreadTaskRunner> ui_thread) |
30 : ui_thread_(std::move(ui_thread)) {} | 38 : ui_thread_(std::move(ui_thread)), |
| 39 work_done_or_abandoned_(base::WaitableEvent::ResetPolicy::AUTOMATIC, |
| 40 base::WaitableEvent::InitialState::NOT_SIGNALED) { |
| 41 } |
31 | 42 |
32 SyncerError UIModelWorker::DoWorkAndWaitUntilDoneImpl( | 43 SyncerError UIModelWorker::DoWorkAndWaitUntilDoneImpl( |
33 const WorkCallback& work) { | 44 const WorkCallback& work) { |
34 SyncerError error_info; | 45 SyncerError error_info; |
35 if (ui_thread_->BelongsToCurrentThread()) { | 46 if (ui_thread_->BelongsToCurrentThread()) { |
36 DLOG(WARNING) << "DoWorkAndWaitUntilDone called from " | 47 DLOG(WARNING) << "DoWorkAndWaitUntilDone called from " |
37 << "ui_loop_. Probably a nested invocation?"; | 48 << "ui_loop_. Probably a nested invocation?"; |
38 return work.Run(); | 49 return work.Run(); |
39 } | 50 } |
40 | 51 |
41 // Signaled when the task is deleted, i.e. after it runs or when it is | 52 if (!ui_thread_->PostTask( |
42 // abandoned. | 53 FROM_HERE, base::Bind(&DoWorkAndSignalEvent, work, |
43 base::WaitableEvent work_done_or_abandoned( | 54 base::Passed(syncer::ScopedEventSignal( |
44 base::WaitableEvent::ResetPolicy::AUTOMATIC, | 55 &work_done_or_abandoned_)), |
45 base::WaitableEvent::InitialState::NOT_SIGNALED); | 56 make_scoped_refptr(this), &error_info))) { |
46 | |
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."; | 57 DLOG(WARNING) << "Could not post work to UI loop."; |
53 error_info = CANNOT_DO_WORK; | 58 error_info = CANNOT_DO_WORK; |
54 return error_info; | 59 return error_info; |
55 } | 60 } |
56 work_done_or_abandoned.Wait(); | 61 work_done_or_abandoned_.Wait(); |
57 | 62 |
58 return error_info; | 63 return error_info; |
59 } | 64 } |
60 | 65 |
| 66 void UIModelWorker::RequestStop() { |
| 67 DCHECK(ui_thread_->BelongsToCurrentThread()); |
| 68 ModelSafeWorker::RequestStop(); |
| 69 work_done_or_abandoned_.Signal(); |
| 70 } |
| 71 |
61 ModelSafeGroup UIModelWorker::GetModelSafeGroup() { | 72 ModelSafeGroup UIModelWorker::GetModelSafeGroup() { |
62 return GROUP_UI; | 73 return GROUP_UI; |
63 } | 74 } |
64 | 75 |
65 bool UIModelWorker::IsOnModelThread() { | 76 bool UIModelWorker::IsOnModelThread() { |
66 return ui_thread_->BelongsToCurrentThread(); | 77 return ui_thread_->BelongsToCurrentThread(); |
67 } | 78 } |
68 | 79 |
69 UIModelWorker::~UIModelWorker() {} | 80 UIModelWorker::~UIModelWorker() {} |
70 | 81 |
71 } // namespace syncer | 82 } // namespace syncer |
OLD | NEW |