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

Side by Side Diff: mojo/system/handle_table.cc

Issue 216893005: Mojo: Move the handle table details out of CoreImpl into its own class. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix compile assert Created 6 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 | Annotate | Revision Log
« no previous file with comments | « mojo/system/handle_table.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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.
4
5 #include "mojo/system/handle_table.h"
6
7 #include "base/basictypes.h"
8 #include "base/logging.h"
9 #include "mojo/system/constants.h"
10 #include "mojo/system/dispatcher.h"
11
12 namespace mojo {
13 namespace system {
14
15 HandleTable::Entry::Entry()
16 : busy(false) {
17 }
18
19 HandleTable::Entry::Entry(const scoped_refptr<Dispatcher>& dispatcher)
20 : dispatcher(dispatcher),
21 busy(false) {
22 }
23
24 HandleTable::Entry::~Entry() {
25 DCHECK(!busy);
26 }
27
28 HandleTable::HandleTable()
29 : next_handle_(MOJO_HANDLE_INVALID + 1) {
30 }
31
32 HandleTable::~HandleTable() {
33 // This should usually not be reached (the only instance should be owned by
34 // the singleton |CoreImpl|, which lives forever), except in tests.
35 }
36
37 Dispatcher* HandleTable::GetDispatcher(MojoHandle handle) {
38 DCHECK_NE(handle, MOJO_HANDLE_INVALID);
39
40 HandleToEntryMap::iterator it = handle_to_entry_map_.find(handle);
41 if (it == handle_to_entry_map_.end())
42 return NULL;
43 return it->second.dispatcher;
44 }
45
46 MojoResult HandleTable::GetAndRemoveDispatcher(
47 MojoHandle handle,
48 scoped_refptr<Dispatcher>* dispatcher) {
49 DCHECK_NE(handle, MOJO_HANDLE_INVALID);
50 DCHECK(dispatcher);
51
52 HandleToEntryMap::iterator it = handle_to_entry_map_.find(handle);
53 if (it == handle_to_entry_map_.end())
54 return MOJO_RESULT_INVALID_ARGUMENT;
55 if (it->second.busy)
56 return MOJO_RESULT_BUSY;
57 *dispatcher = it->second.dispatcher;
58 handle_to_entry_map_.erase(it);
59
60 return MOJO_RESULT_OK;
61 }
62
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);
68 }
69
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));
77 }
78
79 bool HandleTable::AddDispatcherVector(
80 const std::vector<scoped_refptr<Dispatcher> >& dispatchers,
81 MojoHandle* handles) {
82 DCHECK_LE(dispatchers.size(), kMaxMessageNumHandles);
83 DCHECK(handles);
84 // TODO(vtl): |std::numeric_limits<size_t>::max()| isn't a compile-time
85 // expression in C++03.
86 COMPILE_ASSERT(
87 static_cast<uint64_t>(kMaxHandleTableSize) + kMaxMessageNumHandles <
88 (sizeof(size_t) == 8 ? kuint64max :
89 static_cast<uint64_t>(kuint32max)),
90 addition_may_overflow);
91
92 if (handle_to_entry_map_.size() + dispatchers.size() > kMaxHandleTableSize)
93 return false;
94
95 for (size_t i = 0; i < dispatchers.size(); i++) {
96 if (dispatchers[i]) {
97 handles[i] = AddDispatcherNoSizeCheck(dispatchers[i]);
98 } else {
99 LOG(WARNING) << "Invalid dispatcher at index " << i;
100 handles[i] = MOJO_HANDLE_INVALID;
101 }
102 }
103 return true;
104 }
105
106 MojoResult HandleTable::MarkBusyAndStartTransport(
107 MojoHandle disallowed_handle,
108 const MojoHandle* handles,
109 uint32_t num_handles,
110 std::vector<DispatcherTransport>* transports) {
111 DCHECK_NE(disallowed_handle, MOJO_HANDLE_INVALID);
112 DCHECK(handles);
113 DCHECK_LE(num_handles, kMaxMessageNumHandles);
114 DCHECK(transports);
115
116 std::vector<Entry*> entries(num_handles);
117
118 // First verify all the handles and get their dispatchers.
119 uint32_t i;
120 MojoResult error_result = MOJO_RESULT_INTERNAL;
121 for (i = 0; i < num_handles; i++) {
122 // Sending your own handle is not allowed (and, for consistency, returns
123 // "busy").
124 if (handles[i] == disallowed_handle) {
125 error_result = MOJO_RESULT_BUSY;
126 break;
127 }
128
129 HandleToEntryMap::iterator it = handle_to_entry_map_.find(handles[i]);
130 if (it == handle_to_entry_map_.end()) {
131 error_result = MOJO_RESULT_INVALID_ARGUMENT;
132 break;
133 }
134
135 entries[i] = &it->second;
136 if (entries[i]->busy) {
137 error_result = MOJO_RESULT_BUSY;
138 break;
139 }
140 // Note: By marking the handle as busy here, we're also preventing the
141 // same handle from being sent multiple times in the same message.
142 entries[i]->busy = true;
143
144 // Try to start the transport.
145 DispatcherTransport transport =
146 Dispatcher::HandleTableAccess::TryStartTransport(
147 entries[i]->dispatcher.get());
148 if (!transport.is_valid()) {
149 // Unset the busy flag (since it won't be unset below).
150 entries[i]->busy = false;
151 error_result = MOJO_RESULT_BUSY;
152 break;
153 }
154
155 // Check if the dispatcher is busy (e.g., in a two-phase read/write).
156 // (Note that this must be done after the dispatcher's lock is acquired.)
157 if (transport.IsBusy()) {
158 // Unset the busy flag and end the transport (since it won't be done
159 // below).
160 entries[i]->busy = false;
161 transport.End();
162 error_result = MOJO_RESULT_BUSY;
163 break;
164 }
165
166 // Hang on to the transport (which we'll need to end the transport).
167 (*transports)[i] = transport;
168 }
169 if (i < num_handles) {
170 DCHECK_NE(error_result, MOJO_RESULT_INTERNAL);
171
172 // Unset the busy flags and release the locks.
173 for (uint32_t j = 0; j < i; j++) {
174 DCHECK(entries[j]->busy);
175 entries[j]->busy = false;
176 (*transports)[j].End();
177 }
178 return error_result;
179 }
180
181 return MOJO_RESULT_OK;
182 }
183
184
185 //////////////
186
187 MojoHandle HandleTable::AddDispatcherNoSizeCheck(
188 const scoped_refptr<Dispatcher>& dispatcher) {
189 DCHECK(dispatcher);
190 DCHECK_LT(handle_to_entry_map_.size(), kMaxHandleTableSize);
191 DCHECK_NE(next_handle_, MOJO_HANDLE_INVALID);
192
193 // TODO(vtl): Maybe we want to do something different/smarter. (Or maybe try
194 // assigning randomly?)
195 while (handle_to_entry_map_.find(next_handle_) !=
196 handle_to_entry_map_.end()) {
197 next_handle_++;
198 if (next_handle_ == MOJO_HANDLE_INVALID)
199 next_handle_++;
200 }
201
202 MojoHandle new_handle = next_handle_;
203 handle_to_entry_map_[new_handle] = Entry(dispatcher);
204
205 next_handle_++;
206 if (next_handle_ == MOJO_HANDLE_INVALID)
207 next_handle_++;
208
209 return new_handle;
210 }
211
212 void HandleTable::RemoveBusyHandles(const MojoHandle* handles,
213 uint32_t num_handles) {
214 DCHECK(handles);
215 DCHECK_LE(num_handles, kMaxMessageNumHandles);
216
217 for (uint32_t i = 0; i < num_handles; i++) {
218 HandleToEntryMap::iterator it = handle_to_entry_map_.find(handles[i]);
219 DCHECK(it != handle_to_entry_map_.end());
220 DCHECK(it->second.busy);
221 it->second.busy = false; // For the sake of a |DCHECK()|.
222 handle_to_entry_map_.erase(it);
223 }
224 }
225
226 void HandleTable::RestoreBusyHandles(const MojoHandle* handles,
227 uint32_t num_handles) {
228 DCHECK(handles);
229 DCHECK_LE(num_handles, kMaxMessageNumHandles);
230
231 for (uint32_t i = 0; i < num_handles; i++) {
232 HandleToEntryMap::iterator it = handle_to_entry_map_.find(handles[i]);
233 DCHECK(it != handle_to_entry_map_.end());
234 DCHECK(it->second.busy);
235 it->second.busy = false;
236 }
237 }
238
239 } // namespace system
240 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/system/handle_table.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698