OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 #ifndef MOJO_PUBLIC_CPP_SYSTEM_HANDLE_H_ | 5 #ifndef MOJO_PUBLIC_CPP_SYSTEM_HANDLE_H_ |
6 #define MOJO_PUBLIC_CPP_SYSTEM_HANDLE_H_ | 6 #define MOJO_PUBLIC_CPP_SYSTEM_HANDLE_H_ |
7 | 7 |
8 #include <assert.h> | 8 #include <assert.h> |
9 #include <limits> | 9 #include <limits> |
10 | 10 |
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
179 }; | 179 }; |
180 | 180 |
181 // Should have zero overhead. | 181 // Should have zero overhead. |
182 static_assert(sizeof(Handle) == sizeof(MojoHandle), "Bad size for C++ Handle"); | 182 static_assert(sizeof(Handle) == sizeof(MojoHandle), "Bad size for C++ Handle"); |
183 | 183 |
184 // The scoper should also impose no more overhead. | 184 // The scoper should also impose no more overhead. |
185 typedef ScopedHandleBase<Handle> ScopedHandle; | 185 typedef ScopedHandleBase<Handle> ScopedHandle; |
186 static_assert(sizeof(ScopedHandle) == sizeof(Handle), | 186 static_assert(sizeof(ScopedHandle) == sizeof(Handle), |
187 "Bad size for C++ ScopedHandle"); | 187 "Bad size for C++ ScopedHandle"); |
188 | 188 |
189 // TODO(jimbe): Remove this function | |
190 inline MojoResult Wait(Handle handle, | |
191 MojoHandleSignals signals, | |
192 MojoDeadline deadline) { | |
193 return MojoWait(handle.value(), signals, deadline); | |
194 } | |
195 | |
196 inline MojoResult Wait(Handle handle, | 189 inline MojoResult Wait(Handle handle, |
197 MojoHandleSignals signals, | 190 MojoHandleSignals signals, |
198 MojoDeadline deadline, | 191 MojoDeadline deadline, |
199 MojoHandleSignalsState* signals_state) { | 192 MojoHandleSignalsState* signals_state) { |
200 return MojoNewWait(handle.value(), signals, deadline, signals_state); | 193 return MojoWait(handle.value(), signals, deadline, signals_state); |
201 } | |
202 | |
203 // TODO(jimbe): Remove this function | |
204 // |HandleVectorType| and |FlagsVectorType| should be similar enough to | |
205 // |std::vector<Handle>| and |std::vector<MojoHandleSignals>|, respectively: | |
206 // - They should have a (const) |size()| method that returns an unsigned type. | |
207 // - They must provide contiguous storage, with access via (const) reference to | |
208 // that storage provided by a (const) |operator[]()| (by reference). | |
209 template <class HandleVectorType, class FlagsVectorType> | |
210 inline MojoResult WaitMany(const HandleVectorType& handles, | |
211 const FlagsVectorType& signals, | |
212 MojoDeadline deadline) { | |
213 if (signals.size() != handles.size()) | |
214 return MOJO_RESULT_INVALID_ARGUMENT; | |
215 if (handles.size() > std::numeric_limits<uint32_t>::max()) | |
216 return MOJO_RESULT_OUT_OF_RANGE; | |
217 | |
218 if (handles.size() == 0) | |
219 return MojoWaitMany(nullptr, nullptr, 0, deadline); | |
220 | |
221 const Handle& first_handle = handles[0]; | |
222 const MojoHandleSignals& first_signals = signals[0]; | |
223 return MojoWaitMany( | |
224 reinterpret_cast<const MojoHandle*>(&first_handle), | |
225 reinterpret_cast<const MojoHandleSignals*>(&first_signals), | |
226 static_cast<uint32_t>(handles.size()), | |
227 deadline); | |
228 } | 194 } |
229 | 195 |
230 const uint32_t kInvalidWaitManyIndexValue = static_cast<uint32_t>(-1); | 196 const uint32_t kInvalidWaitManyIndexValue = static_cast<uint32_t>(-1); |
231 | 197 |
232 // Simplify the interpretation of the output from |MojoWaitMany()|. | 198 // Simplify the interpretation of the output from |MojoWaitMany()|. |
233 class WaitManyResult { | 199 class WaitManyResult { |
234 public: | 200 public: |
235 explicit WaitManyResult(MojoResult mojo_wait_many_result) | 201 explicit WaitManyResult(MojoResult mojo_wait_many_result) |
236 : result(mojo_wait_many_result), index(kInvalidWaitManyIndexValue) {} | 202 : result(mojo_wait_many_result), index(kInvalidWaitManyIndexValue) {} |
237 | 203 |
(...skipping 30 matching lines...) Expand all Loading... |
268 MojoDeadline deadline, | 234 MojoDeadline deadline, |
269 SignalsStateVectorType* signals_states) { | 235 SignalsStateVectorType* signals_states) { |
270 if (signals.size() != handles.size() || | 236 if (signals.size() != handles.size() || |
271 (signals_states && signals_states->size() != signals.size())) | 237 (signals_states && signals_states->size() != signals.size())) |
272 return WaitManyResult(MOJO_RESULT_INVALID_ARGUMENT); | 238 return WaitManyResult(MOJO_RESULT_INVALID_ARGUMENT); |
273 if (handles.size() >= kInvalidWaitManyIndexValue) | 239 if (handles.size() >= kInvalidWaitManyIndexValue) |
274 return WaitManyResult(MOJO_RESULT_RESOURCE_EXHAUSTED); | 240 return WaitManyResult(MOJO_RESULT_RESOURCE_EXHAUSTED); |
275 | 241 |
276 if (handles.size() == 0) { | 242 if (handles.size() == 0) { |
277 return WaitManyResult( | 243 return WaitManyResult( |
278 MojoNewWaitMany(nullptr, nullptr, 0, deadline, nullptr, nullptr)); | 244 MojoWaitMany(nullptr, nullptr, 0, deadline, nullptr, nullptr)); |
279 } | 245 } |
280 | 246 |
281 uint32_t result_index = kInvalidWaitManyIndexValue; | 247 uint32_t result_index = kInvalidWaitManyIndexValue; |
282 const Handle& first_handle = handles[0]; | 248 const Handle& first_handle = handles[0]; |
283 const MojoHandleSignals& first_signals = signals[0]; | 249 const MojoHandleSignals& first_signals = signals[0]; |
284 MojoHandleSignalsState* first_state = | 250 MojoHandleSignalsState* first_state = |
285 signals_states ? &(*signals_states)[0] : nullptr; | 251 signals_states ? &(*signals_states)[0] : nullptr; |
286 MojoResult result = | 252 MojoResult result = |
287 MojoNewWaitMany(reinterpret_cast<const MojoHandle*>(&first_handle), | 253 MojoWaitMany(reinterpret_cast<const MojoHandle*>(&first_handle), |
288 &first_signals, static_cast<uint32_t>(handles.size()), | 254 &first_signals, static_cast<uint32_t>(handles.size()), |
289 deadline, &result_index, first_state); | 255 deadline, &result_index, first_state); |
290 return WaitManyResult(result, result_index); | 256 return WaitManyResult(result, result_index); |
291 } | 257 } |
292 | 258 |
293 // C++ 4.10, regarding pointer conversion, says that an integral null pointer | 259 // C++ 4.10, regarding pointer conversion, says that an integral null pointer |
294 // constant can be converted to |std::nullptr_t| (which is a typedef for | 260 // constant can be converted to |std::nullptr_t| (which is a typedef for |
295 // |decltype(nullptr)|). The opposite direction is not allowed. | 261 // |decltype(nullptr)|). The opposite direction is not allowed. |
296 template <class HandleVectorType, class FlagsVectorType> | 262 template <class HandleVectorType, class FlagsVectorType> |
297 inline WaitManyResult WaitMany(const HandleVectorType& handles, | 263 inline WaitManyResult WaitMany(const HandleVectorType& handles, |
298 const FlagsVectorType& signals, | 264 const FlagsVectorType& signals, |
299 MojoDeadline deadline, | 265 MojoDeadline deadline, |
300 decltype(nullptr) signals_states) { | 266 decltype(nullptr) signals_states) { |
301 if (signals.size() != handles.size()) | 267 if (signals.size() != handles.size()) |
302 return WaitManyResult(MOJO_RESULT_INVALID_ARGUMENT); | 268 return WaitManyResult(MOJO_RESULT_INVALID_ARGUMENT); |
303 if (handles.size() >= kInvalidWaitManyIndexValue) | 269 if (handles.size() >= kInvalidWaitManyIndexValue) |
304 return WaitManyResult(MOJO_RESULT_RESOURCE_EXHAUSTED); | 270 return WaitManyResult(MOJO_RESULT_RESOURCE_EXHAUSTED); |
305 | 271 |
306 if (handles.size() == 0) { | 272 if (handles.size() == 0) { |
307 return WaitManyResult( | 273 return WaitManyResult( |
308 MojoNewWaitMany(nullptr, nullptr, 0, deadline, nullptr, nullptr)); | 274 MojoWaitMany(nullptr, nullptr, 0, deadline, nullptr, nullptr)); |
309 } | 275 } |
310 | 276 |
311 uint32_t result_index = kInvalidWaitManyIndexValue; | 277 uint32_t result_index = kInvalidWaitManyIndexValue; |
312 const Handle& first_handle = handles[0]; | 278 const Handle& first_handle = handles[0]; |
313 const MojoHandleSignals& first_signals = signals[0]; | 279 const MojoHandleSignals& first_signals = signals[0]; |
314 MojoResult result = MojoNewWaitMany( | 280 MojoResult result = MojoWaitMany( |
315 reinterpret_cast<const MojoHandle*>(&first_handle), &first_signals, | 281 reinterpret_cast<const MojoHandle*>(&first_handle), &first_signals, |
316 static_cast<uint32_t>(handles.size()), deadline, &result_index, nullptr); | 282 static_cast<uint32_t>(handles.size()), deadline, &result_index, nullptr); |
317 return WaitManyResult(result, result_index); | 283 return WaitManyResult(result, result_index); |
318 } | 284 } |
319 | 285 |
320 // |Close()| takes ownership of the handle, since it'll invalidate it. | 286 // |Close()| takes ownership of the handle, since it'll invalidate it. |
321 // Note: There's nothing to do, since the argument will be destroyed when it | 287 // Note: There's nothing to do, since the argument will be destroyed when it |
322 // goes out of scope. | 288 // goes out of scope. |
323 template <class HandleType> | 289 template <class HandleType> |
324 inline void Close(ScopedHandleBase<HandleType> /*handle*/) { | 290 inline void Close(ScopedHandleBase<HandleType> /*handle*/) { |
325 } | 291 } |
326 | 292 |
327 // Most users should typically use |Close()| (above) instead. | 293 // Most users should typically use |Close()| (above) instead. |
328 inline MojoResult CloseRaw(Handle handle) { | 294 inline MojoResult CloseRaw(Handle handle) { |
329 return MojoClose(handle.value()); | 295 return MojoClose(handle.value()); |
330 } | 296 } |
331 | 297 |
332 // Strict weak ordering, so that |Handle|s can be used as keys in |std::map|s, | 298 // Strict weak ordering, so that |Handle|s can be used as keys in |std::map|s, |
333 inline bool operator<(const Handle a, const Handle b) { | 299 inline bool operator<(const Handle a, const Handle b) { |
334 return a.value() < b.value(); | 300 return a.value() < b.value(); |
335 } | 301 } |
336 | 302 |
337 } // namespace mojo | 303 } // namespace mojo |
338 | 304 |
339 #endif // MOJO_PUBLIC_CPP_SYSTEM_HANDLE_H_ | 305 #endif // MOJO_PUBLIC_CPP_SYSTEM_HANDLE_H_ |
OLD | NEW |