OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/edk/system/core.h" | 5 #include "mojo/edk/system/core.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <utility> | 8 #include <utility> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 handle_table_(GetConfiguration().max_handle_table_size) {} | 91 handle_table_(GetConfiguration().max_handle_table_size) {} |
92 | 92 |
93 Core::~Core() { | 93 Core::~Core() { |
94 } | 94 } |
95 | 95 |
96 MojoHandle Core::AddHandle(Handle&& handle) { | 96 MojoHandle Core::AddHandle(Handle&& handle) { |
97 MutexLocker locker(&handle_table_mutex_); | 97 MutexLocker locker(&handle_table_mutex_); |
98 return handle_table_.AddHandle(std::move(handle)); | 98 return handle_table_.AddHandle(std::move(handle)); |
99 } | 99 } |
100 | 100 |
101 MojoResult Core::GetDispatcher(MojoHandle handle_value, | 101 MojoResult Core::GetDispatcher(MojoHandle handle, |
102 RefPtr<Dispatcher>* dispatcher) { | 102 RefPtr<Dispatcher>* dispatcher) { |
103 if (handle_value == MOJO_HANDLE_INVALID) | 103 if (handle == MOJO_HANDLE_INVALID) |
104 return MOJO_RESULT_INVALID_ARGUMENT; | 104 return MOJO_RESULT_INVALID_ARGUMENT; |
105 | 105 |
106 MutexLocker locker(&handle_table_mutex_); | 106 MutexLocker locker(&handle_table_mutex_); |
107 Handle handle; | 107 Handle h; |
108 MojoResult rv = handle_table_.GetHandle(handle_value, &handle); | 108 MojoResult rv = handle_table_.GetHandle(handle, &h); |
109 if (rv == MOJO_RESULT_OK) | 109 if (rv == MOJO_RESULT_OK) |
110 *dispatcher = std::move(handle.dispatcher); | 110 *dispatcher = std::move(h.dispatcher); |
111 return rv; | 111 return rv; |
112 } | 112 } |
113 | 113 |
114 MojoResult Core::GetAndRemoveDispatcher(MojoHandle handle_value, | 114 MojoResult Core::GetAndRemoveDispatcher(MojoHandle handle, |
115 RefPtr<Dispatcher>* dispatcher) { | 115 RefPtr<Dispatcher>* dispatcher) { |
116 if (handle_value == MOJO_HANDLE_INVALID) | 116 if (handle == MOJO_HANDLE_INVALID) |
117 return MOJO_RESULT_INVALID_ARGUMENT; | 117 return MOJO_RESULT_INVALID_ARGUMENT; |
118 | 118 |
119 MutexLocker locker(&handle_table_mutex_); | 119 MutexLocker locker(&handle_table_mutex_); |
120 return handle_table_.GetAndRemoveDispatcher(handle_value, dispatcher); | 120 Handle h; |
| 121 MojoResult rv = handle_table_.GetAndRemoveHandle(handle, &h); |
| 122 if (rv == MOJO_RESULT_OK) |
| 123 *dispatcher = std::move(h.dispatcher); |
| 124 return rv; |
121 } | 125 } |
122 | 126 |
123 MojoResult Core::AsyncWait(MojoHandle handle, | 127 MojoResult Core::AsyncWait(MojoHandle handle, |
124 MojoHandleSignals signals, | 128 MojoHandleSignals signals, |
125 const std::function<void(MojoResult)>& callback) { | 129 const std::function<void(MojoResult)>& callback) { |
126 RefPtr<Dispatcher> dispatcher; | 130 RefPtr<Dispatcher> dispatcher; |
127 MojoResult result = GetDispatcher(handle, &dispatcher); | 131 MojoResult result = GetDispatcher(handle, &dispatcher); |
128 if (result != MOJO_RESULT_OK) | 132 if (result != MOJO_RESULT_OK) |
129 return result; | 133 return result; |
130 | 134 |
131 std::unique_ptr<AsyncWaiter> waiter(new AsyncWaiter(callback)); | 135 std::unique_ptr<AsyncWaiter> waiter(new AsyncWaiter(callback)); |
132 result = dispatcher->AddAwakable(waiter.get(), signals, 0, nullptr); | 136 result = dispatcher->AddAwakable(waiter.get(), signals, 0, nullptr); |
133 if (result == MOJO_RESULT_OK) | 137 if (result == MOJO_RESULT_OK) |
134 ignore_result(waiter.release()); | 138 ignore_result(waiter.release()); |
135 return result; | 139 return result; |
136 } | 140 } |
137 | 141 |
138 MojoTimeTicks Core::GetTimeTicksNow() { | 142 MojoTimeTicks Core::GetTimeTicksNow() { |
139 return GetTimeTicks(); | 143 return GetTimeTicks(); |
140 } | 144 } |
141 | 145 |
142 MojoResult Core::Close(MojoHandle handle) { | 146 MojoResult Core::Close(MojoHandle handle) { |
143 if (handle == MOJO_HANDLE_INVALID) | 147 if (handle == MOJO_HANDLE_INVALID) |
144 return MOJO_RESULT_INVALID_ARGUMENT; | 148 return MOJO_RESULT_INVALID_ARGUMENT; |
145 | 149 |
146 RefPtr<Dispatcher> dispatcher; | 150 Handle h; |
147 { | 151 { |
148 MutexLocker locker(&handle_table_mutex_); | 152 MutexLocker locker(&handle_table_mutex_); |
149 MojoResult result = | 153 MojoResult result = handle_table_.GetAndRemoveHandle(handle, &h); |
150 handle_table_.GetAndRemoveDispatcher(handle, &dispatcher); | |
151 if (result != MOJO_RESULT_OK) | 154 if (result != MOJO_RESULT_OK) |
152 return result; | 155 return result; |
153 } | 156 } |
154 | 157 |
155 // The dispatcher doesn't have a say in being closed, but gets notified of it. | 158 // The dispatcher doesn't have a say in being closed, but gets notified of it. |
156 // Note: This is done outside of |handle_table_mutex_|. As a result, there's a | 159 // Note: This is done outside of |handle_table_mutex_|. As a result, there's a |
157 // race condition that the dispatcher must handle; see the comment in | 160 // race condition that the dispatcher must handle; see the comment in |
158 // |Dispatcher| in dispatcher.h. | 161 // |Dispatcher| in dispatcher.h. |
159 return dispatcher->Close(); | 162 return h.dispatcher->Close(); |
160 } | 163 } |
161 | 164 |
162 MojoResult Core::Wait(MojoHandle handle, | 165 MojoResult Core::Wait(MojoHandle handle, |
163 MojoHandleSignals signals, | 166 MojoHandleSignals signals, |
164 MojoDeadline deadline, | 167 MojoDeadline deadline, |
165 UserPointer<MojoHandleSignalsState> signals_state) { | 168 UserPointer<MojoHandleSignalsState> signals_state) { |
166 uint32_t unused = static_cast<uint32_t>(-1); | 169 uint32_t unused = static_cast<uint32_t>(-1); |
167 HandleSignalsState hss; | 170 HandleSignalsState hss; |
168 MojoResult result = WaitManyInternal(&handle, &signals, 1, deadline, &unused, | 171 MojoResult result = WaitManyInternal(&handle, &signals, 1, deadline, &unused, |
169 signals_state.IsNull() ? nullptr : &hss); | 172 signals_state.IsNull() ? nullptr : &hss); |
(...skipping 527 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
697 if (signals_states) { | 700 if (signals_states) { |
698 for (; i < num_handles; i++) | 701 for (; i < num_handles; i++) |
699 signals_states[i] = dispatchers[i]->GetHandleSignalsState(); | 702 signals_states[i] = dispatchers[i]->GetHandleSignalsState(); |
700 } | 703 } |
701 | 704 |
702 return result; | 705 return result; |
703 } | 706 } |
704 | 707 |
705 } // namespace system | 708 } // namespace system |
706 } // namespace mojo | 709 } // namespace mojo |
OLD | NEW |