Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(175)

Side by Side Diff: components/sync/driver/glue/ui_model_worker.cc

Issue 2496723003: [Sync] Signal UIModelWorker to abort on sync shutdown. (Closed)
Patch Set: Address comments + add test. Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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) {
21 if (scoped_event_signal.IsSignaled()) {
22 *error_info = CANNOT_DO_WORK;
23 return;
24 }
25
22 *error_info = work.Run(); 26 *error_info = work.Run();
23 // The event in |scoped_event_signal| is signaled at the end of this scope. 27 // The event in |scoped_event_signal| is signaled at the end of this scope.
24 } 28 }
25 29
26 } // namespace 30 } // namespace
27 31
28 UIModelWorker::UIModelWorker( 32 UIModelWorker::UIModelWorker(
29 scoped_refptr<base::SingleThreadTaskRunner> ui_thread) 33 scoped_refptr<base::SingleThreadTaskRunner> ui_thread)
30 : ui_thread_(std::move(ui_thread)) {} 34 : ui_thread_(std::move(ui_thread)),
35 work_done_or_abandoned_(base::WaitableEvent::ResetPolicy::MANUAL,
36 base::WaitableEvent::InitialState::NOT_SIGNALED) {
37 }
31 38
32 SyncerError UIModelWorker::DoWorkAndWaitUntilDoneImpl( 39 SyncerError UIModelWorker::DoWorkAndWaitUntilDoneImpl(
33 const WorkCallback& work) { 40 const WorkCallback& work) {
34 SyncerError error_info; 41 SyncerError error_info;
35 if (ui_thread_->BelongsToCurrentThread()) { 42 if (ui_thread_->BelongsToCurrentThread()) {
36 DLOG(WARNING) << "DoWorkAndWaitUntilDone called from " 43 DLOG(WARNING) << "DoWorkAndWaitUntilDone called from "
37 << "ui_loop_. Probably a nested invocation?"; 44 << "ui_loop_. Probably a nested invocation?";
38 return work.Run(); 45 return work.Run();
39 } 46 }
40 47
41 // Signaled when the task is deleted, i.e. after it runs or when it is 48 // RequestStop hasn't been called so reset the signal to do more work.
42 // abandoned. 49 work_done_or_abandoned_.Reset();
fdoray 2016/11/14 14:24:34 This can happen: THREAD ACTION Sync: DoWorkAndW
maxbogue 2016/11/14 20:53:21 This actually occurred to me on Friday right befor
43 base::WaitableEvent work_done_or_abandoned(
44 base::WaitableEvent::ResetPolicy::AUTOMATIC,
45 base::WaitableEvent::InitialState::NOT_SIGNALED);
46 50
47 if (!ui_thread_->PostTask(FROM_HERE, 51 if (!ui_thread_->PostTask(FROM_HERE,
48 base::Bind(&CallDoWorkAndSignalEvent, work, 52 base::Bind(&CallDoWorkAndSignalEvent, work,
49 base::Passed(syncer::ScopedEventSignal( 53 base::Passed(syncer::ScopedEventSignal(
50 &work_done_or_abandoned)), 54 &work_done_or_abandoned_)),
51 &error_info))) { 55 &error_info))) {
52 DLOG(WARNING) << "Could not post work to UI loop."; 56 DLOG(WARNING) << "Could not post work to UI loop.";
53 error_info = CANNOT_DO_WORK; 57 error_info = CANNOT_DO_WORK;
54 return error_info; 58 return error_info;
55 } 59 }
56 work_done_or_abandoned.Wait(); 60 work_done_or_abandoned_.Wait();
57 61
58 return error_info; 62 return error_info;
59 } 63 }
60 64
65 void UIModelWorker::RequestStop() {
66 DCHECK(ui_thread_->BelongsToCurrentThread());
67 ModelSafeWorker::RequestStop();
68 work_done_or_abandoned_.Signal();
69 }
70
61 ModelSafeGroup UIModelWorker::GetModelSafeGroup() { 71 ModelSafeGroup UIModelWorker::GetModelSafeGroup() {
62 return GROUP_UI; 72 return GROUP_UI;
63 } 73 }
64 74
65 bool UIModelWorker::IsOnModelThread() { 75 bool UIModelWorker::IsOnModelThread() {
66 return ui_thread_->BelongsToCurrentThread(); 76 return ui_thread_->BelongsToCurrentThread();
67 } 77 }
68 78
69 UIModelWorker::~UIModelWorker() {} 79 UIModelWorker::~UIModelWorker() {}
70 80
71 } // namespace syncer 81 } // namespace syncer
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698