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

Side by Side Diff: mojo/public/cpp/bindings/lib/sync_handle_registry.cc

Issue 1832193002: Mojo C++ bindings: refactor SyncHandleWatcher. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 8 months 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 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 "mojo/public/cpp/bindings/lib/sync_handle_watcher.h" 5 #include "mojo/public/cpp/bindings/lib/sync_handle_registry.h"
6 6
7 #include "base/lazy_instance.h" 7 #include "base/lazy_instance.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "base/stl_util.h" 9 #include "base/stl_util.h"
10 #include "base/threading/thread_local.h" 10 #include "base/threading/thread_local.h"
11 #include "mojo/public/c/system/core.h" 11 #include "mojo/public/c/system/core.h"
12 12
13 namespace mojo { 13 namespace mojo {
14 namespace internal { 14 namespace internal {
15 namespace { 15 namespace {
16 16
17 base::LazyInstance<base::ThreadLocalPointer<SyncHandleWatcher>> 17 base::LazyInstance<base::ThreadLocalPointer<SyncHandleRegistry>>
18 g_current_sync_handle_watcher = LAZY_INSTANCE_INITIALIZER; 18 g_current_sync_handle_watcher = LAZY_INSTANCE_INITIALIZER;
19 19
20 } // namespace 20 } // namespace
21 21
22 // static 22 // static
23 SyncHandleWatcher* SyncHandleWatcher::current() { 23 SyncHandleRegistry* SyncHandleRegistry::current() {
24 SyncHandleWatcher* result = g_current_sync_handle_watcher.Pointer()->Get(); 24 SyncHandleRegistry* result = g_current_sync_handle_watcher.Pointer()->Get();
25 if (!result) { 25 if (!result) {
26 // This object will be destroyed when the current message loop goes away. 26 result = new SyncHandleRegistry();
27 result = new SyncHandleWatcher();
28 DCHECK_EQ(result, g_current_sync_handle_watcher.Pointer()->Get()); 27 DCHECK_EQ(result, g_current_sync_handle_watcher.Pointer()->Get());
29 } 28 }
30 return result; 29 return result;
31 } 30 }
32 31
33 bool SyncHandleWatcher::RegisterHandle(const Handle& handle, 32 bool SyncHandleRegistry::RegisterHandle(const Handle& handle,
34 MojoHandleSignals handle_signals, 33 MojoHandleSignals handle_signals,
35 const HandleCallback& callback) { 34 const HandleCallback& callback) {
36 DCHECK(thread_checker_.CalledOnValidThread()); 35 DCHECK(thread_checker_.CalledOnValidThread());
37 36
38 if (ContainsKey(handles_, handle)) 37 if (ContainsKey(handles_, handle))
39 return false; 38 return false;
40 39
41 MojoResult result = MojoAddHandle(wait_set_handle_.get().value(), 40 MojoResult result = MojoAddHandle(wait_set_handle_.get().value(),
42 handle.value(), handle_signals); 41 handle.value(), handle_signals);
43 if (result != MOJO_RESULT_OK) 42 if (result != MOJO_RESULT_OK)
44 return false; 43 return false;
45 44
46 handles_[handle] = callback; 45 handles_[handle] = callback;
47 return true; 46 return true;
48 } 47 }
49 48
50 void SyncHandleWatcher::UnregisterHandle(const Handle& handle) { 49 void SyncHandleRegistry::UnregisterHandle(const Handle& handle) {
51 DCHECK(thread_checker_.CalledOnValidThread()); 50 DCHECK(thread_checker_.CalledOnValidThread());
52 DCHECK(ContainsKey(handles_, handle)); 51 if (!ContainsKey(handles_, handle))
52 return;
53 53
54 MojoResult result = 54 MojoResult result =
55 MojoRemoveHandle(wait_set_handle_.get().value(), handle.value()); 55 MojoRemoveHandle(wait_set_handle_.get().value(), handle.value());
56 DCHECK_EQ(MOJO_RESULT_OK, result); 56 DCHECK_EQ(MOJO_RESULT_OK, result);
57
58 handles_.erase(handle); 57 handles_.erase(handle);
59 } 58 }
60 59
61 bool SyncHandleWatcher::WatchAllHandles(const bool* should_stop[], 60 bool SyncHandleRegistry::WatchAllHandles(const bool* should_stop[],
62 size_t count) { 61 size_t count) {
63 DCHECK(thread_checker_.CalledOnValidThread()); 62 DCHECK(thread_checker_.CalledOnValidThread());
64 63
65 MojoResult result; 64 MojoResult result;
66 uint32_t num_ready_handles; 65 uint32_t num_ready_handles;
67 MojoHandle ready_handle; 66 MojoHandle ready_handle;
68 MojoResult ready_handle_result; 67 MojoResult ready_handle_result;
69 68
70 while (true) { 69 // This object may be destroyed during a callback. So we have to preserve
70 // the boolean.
71 scoped_refptr<base::RefCountedData<bool>> destroyed = destroyed_;
72 while (!destroyed->data) {
71 for (size_t i = 0; i < count; ++i) 73 for (size_t i = 0; i < count; ++i)
72 if (*should_stop[i]) 74 if (*should_stop[i])
73 return true; 75 return true;
74 do { 76 do {
75 result = Wait(wait_set_handle_.get(), MOJO_HANDLE_SIGNAL_READABLE, 77 result = Wait(wait_set_handle_.get(), MOJO_HANDLE_SIGNAL_READABLE,
76 MOJO_DEADLINE_INDEFINITE, nullptr); 78 MOJO_DEADLINE_INDEFINITE, nullptr);
77 if (result != MOJO_RESULT_OK) 79 if (result != MOJO_RESULT_OK)
78 return false; 80 return false;
79 81
80 // TODO(yzshen): Theoretically it can reduce sync call re-entrancy if we 82 // TODO(yzshen): Theoretically it can reduce sync call re-entrancy if we
81 // give priority to the handle that is waiting for sync response. 83 // give priority to the handle that is waiting for sync response.
82 num_ready_handles = 1; 84 num_ready_handles = 1;
83 result = MojoGetReadyHandles(wait_set_handle_.get().value(), 85 result = MojoGetReadyHandles(wait_set_handle_.get().value(),
84 &num_ready_handles, &ready_handle, 86 &num_ready_handles, &ready_handle,
85 &ready_handle_result, nullptr); 87 &ready_handle_result, nullptr);
86 if (result != MOJO_RESULT_OK && result != MOJO_RESULT_SHOULD_WAIT) 88 if (result != MOJO_RESULT_OK && result != MOJO_RESULT_SHOULD_WAIT)
87 return false; 89 return false;
88 } while (result == MOJO_RESULT_SHOULD_WAIT); 90 } while (result == MOJO_RESULT_SHOULD_WAIT);
89 91
90 const auto iter = handles_.find(Handle(ready_handle)); 92 const auto iter = handles_.find(Handle(ready_handle));
91 iter->second.Run(ready_handle_result); 93 iter->second.Run(ready_handle_result);
92 }; 94 };
93 95
94 return true; 96 return false;
95 } 97 }
96 98
97 SyncHandleWatcher::SyncHandleWatcher() { 99 SyncHandleRegistry::SyncHandleRegistry()
100 : destroyed_(new base::RefCountedData<bool>(false)) {
98 MojoHandle handle; 101 MojoHandle handle;
99 MojoResult result = MojoCreateWaitSet(&handle); 102 MojoResult result = MojoCreateWaitSet(&handle);
100 CHECK_EQ(MOJO_RESULT_OK, result); 103 CHECK_EQ(MOJO_RESULT_OK, result);
101 wait_set_handle_.reset(Handle(handle)); 104 wait_set_handle_.reset(Handle(handle));
102 CHECK(wait_set_handle_.is_valid()); 105 CHECK(wait_set_handle_.is_valid());
103 106
104 DCHECK(!g_current_sync_handle_watcher.Pointer()->Get()); 107 DCHECK(!g_current_sync_handle_watcher.Pointer()->Get());
105 g_current_sync_handle_watcher.Pointer()->Set(this); 108 g_current_sync_handle_watcher.Pointer()->Set(this);
106 109
107 base::MessageLoop::current()->AddDestructionObserver(this); 110 base::MessageLoop::current()->AddDestructionObserver(this);
108 } 111 }
109 112
110 SyncHandleWatcher::~SyncHandleWatcher() { 113 SyncHandleRegistry::~SyncHandleRegistry() {
111 DCHECK(thread_checker_.CalledOnValidThread()); 114 DCHECK(thread_checker_.CalledOnValidThread());
112 DCHECK(handles_.empty()); 115 destroyed_->data = true;
113 g_current_sync_handle_watcher.Pointer()->Set(nullptr); 116 g_current_sync_handle_watcher.Pointer()->Set(nullptr);
114 } 117 }
115 118
116 void SyncHandleWatcher::WillDestroyCurrentMessageLoop() { 119 void SyncHandleRegistry::WillDestroyCurrentMessageLoop() {
117 DCHECK(thread_checker_.CalledOnValidThread()); 120 DCHECK(thread_checker_.CalledOnValidThread());
118 DCHECK_EQ(this, g_current_sync_handle_watcher.Pointer()->Get()); 121 DCHECK_EQ(this, g_current_sync_handle_watcher.Pointer()->Get());
119 122
120 base::MessageLoop::current()->RemoveDestructionObserver(this); 123 base::MessageLoop::current()->RemoveDestructionObserver(this);
124
121 delete this; 125 delete this;
122 } 126 }
123 127
124 } // namespace internal 128 } // namespace internal
125 } // namespace mojo 129 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/public/cpp/bindings/lib/sync_handle_registry.h ('k') | mojo/public/cpp/bindings/lib/sync_handle_watcher.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698