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

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

Issue 1900953002: Mojo C++ bindings: make SyncHandleRegistry a ref-counted object. (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_registry.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<SyncHandleRegistry>> 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 SyncHandleRegistry* SyncHandleRegistry::current() { 23 scoped_refptr<SyncHandleRegistry> SyncHandleRegistry::current() {
24 SyncHandleRegistry* result = g_current_sync_handle_watcher.Pointer()->Get(); 24 scoped_refptr<SyncHandleRegistry> result(
25 g_current_sync_handle_watcher.Pointer()->Get());
25 if (!result) { 26 if (!result) {
26 result = new SyncHandleRegistry(); 27 result = new SyncHandleRegistry();
27 DCHECK_EQ(result, g_current_sync_handle_watcher.Pointer()->Get()); 28 DCHECK_EQ(result.get(), g_current_sync_handle_watcher.Pointer()->Get());
28 } 29 }
29 return result; 30 return result;
30 } 31 }
31 32
32 bool SyncHandleRegistry::RegisterHandle(const Handle& handle, 33 bool SyncHandleRegistry::RegisterHandle(const Handle& handle,
33 MojoHandleSignals handle_signals, 34 MojoHandleSignals handle_signals,
34 const HandleCallback& callback) { 35 const HandleCallback& callback) {
35 DCHECK(thread_checker_.CalledOnValidThread()); 36 DCHECK(thread_checker_.CalledOnValidThread());
36 37
37 if (ContainsKey(handles_, handle)) 38 if (ContainsKey(handles_, handle))
(...skipping 21 matching lines...) Expand all
59 60
60 bool SyncHandleRegistry::WatchAllHandles(const bool* should_stop[], 61 bool SyncHandleRegistry::WatchAllHandles(const bool* should_stop[],
61 size_t count) { 62 size_t count) {
62 DCHECK(thread_checker_.CalledOnValidThread()); 63 DCHECK(thread_checker_.CalledOnValidThread());
63 64
64 MojoResult result; 65 MojoResult result;
65 uint32_t num_ready_handles; 66 uint32_t num_ready_handles;
66 MojoHandle ready_handle; 67 MojoHandle ready_handle;
67 MojoResult ready_handle_result; 68 MojoResult ready_handle_result;
68 69
69 // This object may be destroyed during a callback. So we have to preserve 70 scoped_refptr<SyncHandleRegistry> preserver(this);
70 // the boolean. 71 while (true) {
71 scoped_refptr<base::RefCountedData<bool>> destroyed = destroyed_;
72 while (!destroyed->data) {
73 for (size_t i = 0; i < count; ++i) 72 for (size_t i = 0; i < count; ++i)
74 if (*should_stop[i]) 73 if (*should_stop[i])
75 return true; 74 return true;
76 do { 75 do {
77 result = Wait(wait_set_handle_.get(), MOJO_HANDLE_SIGNAL_READABLE, 76 result = Wait(wait_set_handle_.get(), MOJO_HANDLE_SIGNAL_READABLE,
78 MOJO_DEADLINE_INDEFINITE, nullptr); 77 MOJO_DEADLINE_INDEFINITE, nullptr);
79 if (result != MOJO_RESULT_OK) 78 if (result != MOJO_RESULT_OK)
80 return false; 79 return false;
81 80
82 // TODO(yzshen): Theoretically it can reduce sync call re-entrancy if we 81 // TODO(yzshen): Theoretically it can reduce sync call re-entrancy if we
83 // give priority to the handle that is waiting for sync response. 82 // give priority to the handle that is waiting for sync response.
84 num_ready_handles = 1; 83 num_ready_handles = 1;
85 result = MojoGetReadyHandles(wait_set_handle_.get().value(), 84 result = MojoGetReadyHandles(wait_set_handle_.get().value(),
86 &num_ready_handles, &ready_handle, 85 &num_ready_handles, &ready_handle,
87 &ready_handle_result, nullptr); 86 &ready_handle_result, nullptr);
88 if (result != MOJO_RESULT_OK && result != MOJO_RESULT_SHOULD_WAIT) 87 if (result != MOJO_RESULT_OK && result != MOJO_RESULT_SHOULD_WAIT)
89 return false; 88 return false;
90 } while (result == MOJO_RESULT_SHOULD_WAIT); 89 } while (result == MOJO_RESULT_SHOULD_WAIT);
91 90
92 const auto iter = handles_.find(Handle(ready_handle)); 91 const auto iter = handles_.find(Handle(ready_handle));
93 iter->second.Run(ready_handle_result); 92 iter->second.Run(ready_handle_result);
94 }; 93 };
95 94
96 return false; 95 return false;
97 } 96 }
98 97
99 SyncHandleRegistry::SyncHandleRegistry() 98 SyncHandleRegistry::SyncHandleRegistry() {
100 : destroyed_(new base::RefCountedData<bool>(false)) {
101 MojoHandle handle; 99 MojoHandle handle;
102 MojoResult result = MojoCreateWaitSet(&handle); 100 MojoResult result = MojoCreateWaitSet(&handle);
103 CHECK_EQ(MOJO_RESULT_OK, result); 101 CHECK_EQ(MOJO_RESULT_OK, result);
104 wait_set_handle_.reset(Handle(handle)); 102 wait_set_handle_.reset(Handle(handle));
105 CHECK(wait_set_handle_.is_valid()); 103 CHECK(wait_set_handle_.is_valid());
106 104
107 DCHECK(!g_current_sync_handle_watcher.Pointer()->Get()); 105 DCHECK(!g_current_sync_handle_watcher.Pointer()->Get());
108 g_current_sync_handle_watcher.Pointer()->Set(this); 106 g_current_sync_handle_watcher.Pointer()->Set(this);
109
110 base::MessageLoop::current()->AddDestructionObserver(this);
111 } 107 }
112 108
113 SyncHandleRegistry::~SyncHandleRegistry() { 109 SyncHandleRegistry::~SyncHandleRegistry() {
114 DCHECK(thread_checker_.CalledOnValidThread()); 110 DCHECK(thread_checker_.CalledOnValidThread());
115 destroyed_->data = true;
116 g_current_sync_handle_watcher.Pointer()->Set(nullptr); 111 g_current_sync_handle_watcher.Pointer()->Set(nullptr);
117 } 112 }
118 113
119 void SyncHandleRegistry::WillDestroyCurrentMessageLoop() {
120 DCHECK(thread_checker_.CalledOnValidThread());
121 DCHECK_EQ(this, g_current_sync_handle_watcher.Pointer()->Get());
122
123 base::MessageLoop::current()->RemoveDestructionObserver(this);
124
125 delete this;
126 }
127
128 } // namespace internal 114 } // namespace internal
129 } // namespace mojo 115 } // 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