OLD | NEW |
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/sync_handle_registry.h" | 5 #include "mojo/public/cpp/bindings/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" |
(...skipping 19 matching lines...) Expand all Loading... |
30 } | 30 } |
31 | 31 |
32 bool SyncHandleRegistry::RegisterHandle(const Handle& handle, | 32 bool SyncHandleRegistry::RegisterHandle(const Handle& handle, |
33 MojoHandleSignals handle_signals, | 33 MojoHandleSignals handle_signals, |
34 const HandleCallback& callback) { | 34 const HandleCallback& callback) { |
35 DCHECK(thread_checker_.CalledOnValidThread()); | 35 DCHECK(thread_checker_.CalledOnValidThread()); |
36 | 36 |
37 if (base::ContainsKey(handles_, handle)) | 37 if (base::ContainsKey(handles_, handle)) |
38 return false; | 38 return false; |
39 | 39 |
40 MojoResult result = MojoAddHandle(wait_set_handle_.get().value(), | 40 MojoResult result = wait_set_.AddHandle(handle, handle_signals); |
41 handle.value(), handle_signals); | |
42 if (result != MOJO_RESULT_OK) | 41 if (result != MOJO_RESULT_OK) |
43 return false; | 42 return false; |
44 | 43 |
45 handles_[handle] = callback; | 44 handles_[handle] = callback; |
46 return true; | 45 return true; |
47 } | 46 } |
48 | 47 |
49 void SyncHandleRegistry::UnregisterHandle(const Handle& handle) { | 48 void SyncHandleRegistry::UnregisterHandle(const Handle& handle) { |
50 DCHECK(thread_checker_.CalledOnValidThread()); | 49 DCHECK(thread_checker_.CalledOnValidThread()); |
51 if (!base::ContainsKey(handles_, handle)) | 50 if (!base::ContainsKey(handles_, handle)) |
52 return; | 51 return; |
53 | 52 |
54 MojoResult result = | 53 MojoResult result = wait_set_.RemoveHandle(handle); |
55 MojoRemoveHandle(wait_set_handle_.get().value(), handle.value()); | |
56 DCHECK_EQ(MOJO_RESULT_OK, result); | 54 DCHECK_EQ(MOJO_RESULT_OK, result); |
57 handles_.erase(handle); | 55 handles_.erase(handle); |
58 } | 56 } |
59 | 57 |
60 bool SyncHandleRegistry::WatchAllHandles(const bool* should_stop[], | 58 bool SyncHandleRegistry::WatchAllHandles(const bool* should_stop[], |
61 size_t count) { | 59 size_t count) { |
62 DCHECK(thread_checker_.CalledOnValidThread()); | 60 DCHECK(thread_checker_.CalledOnValidThread()); |
63 | 61 |
64 MojoResult result; | 62 size_t num_ready_handles; |
65 uint32_t num_ready_handles; | 63 Handle ready_handle; |
66 MojoHandle ready_handle; | |
67 MojoResult ready_handle_result; | 64 MojoResult ready_handle_result; |
68 | 65 |
69 scoped_refptr<SyncHandleRegistry> preserver(this); | 66 scoped_refptr<SyncHandleRegistry> preserver(this); |
70 while (true) { | 67 while (true) { |
71 for (size_t i = 0; i < count; ++i) | 68 for (size_t i = 0; i < count; ++i) |
72 if (*should_stop[i]) | 69 if (*should_stop[i]) |
73 return true; | 70 return true; |
74 do { | |
75 result = Wait(wait_set_handle_.get(), MOJO_HANDLE_SIGNAL_READABLE, | |
76 MOJO_DEADLINE_INDEFINITE, nullptr); | |
77 if (result != MOJO_RESULT_OK) | |
78 return false; | |
79 | 71 |
80 // TODO(yzshen): Theoretically it can reduce sync call re-entrancy if we | 72 // TODO(yzshen): Theoretically it can reduce sync call re-entrancy if we |
81 // give priority to the handle that is waiting for sync response. | 73 // give priority to the handle that is waiting for sync response. |
82 num_ready_handles = 1; | 74 num_ready_handles = 1; |
83 result = MojoGetReadyHandles(wait_set_handle_.get().value(), | 75 wait_set_.Wait(&num_ready_handles, &ready_handle, &ready_handle_result); |
84 &num_ready_handles, &ready_handle, | 76 DCHECK_EQ(1u, num_ready_handles); |
85 &ready_handle_result, nullptr); | |
86 if (result != MOJO_RESULT_OK && result != MOJO_RESULT_SHOULD_WAIT) | |
87 return false; | |
88 } while (result == MOJO_RESULT_SHOULD_WAIT); | |
89 | 77 |
90 const auto iter = handles_.find(Handle(ready_handle)); | 78 const auto iter = handles_.find(ready_handle); |
91 iter->second.Run(ready_handle_result); | 79 iter->second.Run(ready_handle_result); |
92 }; | 80 }; |
93 | 81 |
94 return false; | 82 return false; |
95 } | 83 } |
96 | 84 |
97 SyncHandleRegistry::SyncHandleRegistry() { | 85 SyncHandleRegistry::SyncHandleRegistry() { |
98 MojoHandle handle; | |
99 MojoResult result = MojoCreateWaitSet(&handle); | |
100 CHECK_EQ(MOJO_RESULT_OK, result); | |
101 wait_set_handle_.reset(Handle(handle)); | |
102 CHECK(wait_set_handle_.is_valid()); | |
103 | |
104 DCHECK(!g_current_sync_handle_watcher.Pointer()->Get()); | 86 DCHECK(!g_current_sync_handle_watcher.Pointer()->Get()); |
105 g_current_sync_handle_watcher.Pointer()->Set(this); | 87 g_current_sync_handle_watcher.Pointer()->Set(this); |
106 } | 88 } |
107 | 89 |
108 SyncHandleRegistry::~SyncHandleRegistry() { | 90 SyncHandleRegistry::~SyncHandleRegistry() { |
109 DCHECK(thread_checker_.CalledOnValidThread()); | 91 DCHECK(thread_checker_.CalledOnValidThread()); |
110 | 92 |
111 // This object may be destructed after the thread local storage slot used by | 93 // This object may be destructed after the thread local storage slot used by |
112 // |g_current_sync_handle_watcher| is reset during thread shutdown. | 94 // |g_current_sync_handle_watcher| is reset during thread shutdown. |
113 // For example, another slot in the thread local storage holds a referrence to | 95 // For example, another slot in the thread local storage holds a referrence to |
114 // this object, and that slot is cleaned up after | 96 // this object, and that slot is cleaned up after |
115 // |g_current_sync_handle_watcher|. | 97 // |g_current_sync_handle_watcher|. |
116 if (!g_current_sync_handle_watcher.Pointer()->Get()) | 98 if (!g_current_sync_handle_watcher.Pointer()->Get()) |
117 return; | 99 return; |
118 | 100 |
119 // If this breaks, it is likely that the global variable is bulit into and | 101 // If this breaks, it is likely that the global variable is bulit into and |
120 // accessed from multiple modules. | 102 // accessed from multiple modules. |
121 DCHECK_EQ(this, g_current_sync_handle_watcher.Pointer()->Get()); | 103 DCHECK_EQ(this, g_current_sync_handle_watcher.Pointer()->Get()); |
122 | 104 |
123 g_current_sync_handle_watcher.Pointer()->Set(nullptr); | 105 g_current_sync_handle_watcher.Pointer()->Set(nullptr); |
124 } | 106 } |
125 | 107 |
126 } // namespace mojo | 108 } // namespace mojo |
OLD | NEW |