1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "mojo/system/handle_table.h"
7 #include "base/basictypes.h"
8 #include "base/logging.h"
9 #include "mojo/system/constants.h"
10 #include "mojo/system/dispatcher.h"
15 HandleTable::Entry::Entry()
19 HandleTable::Entry::Entry(const scoped_refptr<Dispatcher>& dispatcher)
20 : dispatcher(dispatcher),
24 HandleTable::Entry::~Entry() {
28 HandleTable::HandleTable()
29 : next_handle_(MOJO_HANDLE_INVALID + 1) {
32 HandleTable::~HandleTable() {
33 // This should usually not be reached (the only instance should be owned by
34 // the singleton |Core|, which lives forever), except in tests.
37 Dispatcher* HandleTable::GetDispatcher(MojoHandle handle) {
38 DCHECK_NE(handle, MOJO_HANDLE_INVALID);
40 HandleToEntryMap::iterator it = handle_to_entry_map_.find(handle);
41 if (it == handle_to_entry_map_.end())
43 return it->second.dispatcher;
46 MojoResult HandleTable::GetAndRemoveDispatcher(
48 scoped_refptr<Dispatcher>* dispatcher) {
49 DCHECK_NE(handle, MOJO_HANDLE_INVALID);
52 HandleToEntryMap::iterator it = handle_to_entry_map_.find(handle);
53 if (it == handle_to_entry_map_.end())
54 return MOJO_RESULT_INVALID_ARGUMENT;
56 return MOJO_RESULT_BUSY;
57 *dispatcher = it->second.dispatcher;
58 handle_to_entry_map_.erase(it);
60 return MOJO_RESULT_OK;
63 MojoHandle HandleTable::AddDispatcher(
64 const scoped_refptr<Dispatcher>& dispatcher) {
65 if (handle_to_entry_map_.size() >= kMaxHandleTableSize)
66 return MOJO_HANDLE_INVALID;
67 return AddDispatcherNoSizeCheck(dispatcher);
70 std::pair<MojoHandle, MojoHandle> HandleTable::AddDispatcherPair(
71 const scoped_refptr<Dispatcher>& dispatcher0,
72 const scoped_refptr<Dispatcher>& dispatcher1) {
73 if (handle_to_entry_map_.size() + 1 >= kMaxHandleTableSize)
74 return std::make_pair(MOJO_HANDLE_INVALID, MOJO_HANDLE_INVALID);
75 return std::make_pair(AddDispatcherNoSizeCheck(dispatcher0),
76 AddDispatcherNoSizeCheck(dispatcher1));
79 bool HandleTable::AddDispatcherVector(const DispatcherVector& dispatchers,
80 MojoHandle* handles) {
81 DCHECK_LE(dispatchers.size(), kMaxMessageNumHandles);
83 // TODO(vtl): |std::numeric_limits<size_t>::max()| isn't a compile-time
84 // expression in C++03.
86 static_cast<uint64_t>(kMaxHandleTableSize) + kMaxMessageNumHandles <
87 (sizeof(size_t) == 8 ? kuint64max :
88 static_cast<uint64_t>(kuint32max)),
89 addition_may_overflow);
91 if (handle_to_entry_map_.size() + dispatchers.size() > kMaxHandleTableSize)
94 for (size_t i = 0; i < dispatchers.size(); i++) {
96 handles[i] = AddDispatcherNoSizeCheck(dispatchers[i]);
98 LOG(WARNING) << "Invalid dispatcher at index " << i;
99 handles[i] = MOJO_HANDLE_INVALID;
105 MojoResult HandleTable::MarkBusyAndStartTransport(
106 MojoHandle disallowed_handle,
107 const MojoHandle* handles,
108 uint32_t num_handles,
109 std::vector<DispatcherTransport>* transports) {
110 DCHECK_NE(disallowed_handle, MOJO_HANDLE_INVALID);
112 DCHECK_LE(num_handles, kMaxMessageNumHandles);
115 std::vector<Entry*> entries(num_handles);
117 // First verify all the handles and get their dispatchers.
119 MojoResult error_result = MOJO_RESULT_INTERNAL;
120 for (i = 0; i < num_handles; i++) {
121 // Sending your own handle is not allowed (and, for consistency, returns
123 if (handles[i] == disallowed_handle) {
124 error_result = MOJO_RESULT_BUSY;
128 HandleToEntryMap::iterator it = handle_to_entry_map_.find(handles[i]);
129 if (it == handle_to_entry_map_.end()) {
130 error_result = MOJO_RESULT_INVALID_ARGUMENT;
134 entries[i] = &it->second;
135 if (entries[i]->busy) {
136 error_result = MOJO_RESULT_BUSY;
139 // Note: By marking the handle as busy here, we're also preventing the
140 // same handle from being sent multiple times in the same message.
141 entries[i]->busy = true;
143 // Try to start the transport.
144 DispatcherTransport transport =
145 Dispatcher::HandleTableAccess::TryStartTransport(
146 entries[i]->dispatcher.get());
147 if (!transport.is_valid()) {
148 // Unset the busy flag (since it won't be unset below).
149 entries[i]->busy = false;
150 error_result = MOJO_RESULT_BUSY;
154 // Check if the dispatcher is busy (e.g., in a two-phase read/write).
155 // (Note that this must be done after the dispatcher's lock is acquired.)
156 if (transport.IsBusy()) {
157 // Unset the busy flag and end the transport (since it won't be done
159 entries[i]->busy = false;
161 error_result = MOJO_RESULT_BUSY;
165 // Hang on to the transport (which we'll need to end the transport).
166 (*transports)[i] = transport;
168 if (i < num_handles) {
169 DCHECK_NE(error_result, MOJO_RESULT_INTERNAL);
171 // Unset the busy flags and release the locks.
172 for (uint32_t j = 0; j < i; j++) {
173 DCHECK(entries[j]->busy);
174 entries[j]->busy = false;
175 (*transports)[j].End();
180 return MOJO_RESULT_OK;
183 MojoHandle HandleTable::AddDispatcherNoSizeCheck(
184 const scoped_refptr<Dispatcher>& dispatcher) {
186 DCHECK_LT(handle_to_entry_map_.size(), kMaxHandleTableSize);
187 DCHECK_NE(next_handle_, MOJO_HANDLE_INVALID);
189 // TODO(vtl): Maybe we want to do something different/smarter. (Or maybe try
190 // assigning randomly?)
191 while (handle_to_entry_map_.find(next_handle_) !=
192 handle_to_entry_map_.end()) {
194 if (next_handle_ == MOJO_HANDLE_INVALID)
198 MojoHandle new_handle = next_handle_;
199 handle_to_entry_map_[new_handle] = Entry(dispatcher);
202 if (next_handle_ == MOJO_HANDLE_INVALID)
208 void HandleTable::RemoveBusyHandles(const MojoHandle* handles,
209 uint32_t num_handles) {
211 DCHECK_LE(num_handles, kMaxMessageNumHandles);
213 for (uint32_t i = 0; i < num_handles; i++) {
214 HandleToEntryMap::iterator it = handle_to_entry_map_.find(handles[i]);
215 DCHECK(it != handle_to_entry_map_.end());
216 DCHECK(it->second.busy);
217 it->second.busy = false; // For the sake of a |DCHECK()|.
218 handle_to_entry_map_.erase(it);
222 void HandleTable::RestoreBusyHandles(const MojoHandle* handles,
223 uint32_t num_handles) {
225 DCHECK_LE(num_handles, kMaxMessageNumHandles);
227 for (uint32_t i = 0; i < num_handles; i++) {
228 HandleToEntryMap::iterator it = handle_to_entry_map_.find(handles[i]);
229 DCHECK(it != handle_to_entry_map_.end());
230 DCHECK(it->second.busy);
231 it->second.busy = false;
235 } // namespace system