Chromium Code Reviews| 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 #ifndef MOJO_PUBLIC_CPP_SYSTEM_WAIT_H_ | 5 #ifndef MOJO_PUBLIC_CPP_SYSTEM_WAIT_H_ |
| 6 #define MOJO_PUBLIC_CPP_SYSTEM_WAIT_H_ | 6 #define MOJO_PUBLIC_CPP_SYSTEM_WAIT_H_ |
| 7 | 7 |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <cstddef> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "mojo/public/c/system/handle.h" | 12 #include "mojo/public/c/system/handle.h" |
| 13 #include "mojo/public/c/system/result.h" | 13 #include "mojo/public/c/system/result.h" |
| 14 #include "mojo/public/c/system/time.h" | 14 #include "mojo/public/c/system/time.h" |
| 15 #include "mojo/public/c/system/wait.h" | 15 #include "mojo/public/c/system/wait.h" |
| 16 #include "mojo/public/cpp/system/handle.h" | 16 #include "mojo/public/cpp/system/handle.h" |
| 17 | 17 |
| 18 namespace mojo { | 18 namespace mojo { |
| 19 | 19 |
| 20 inline MojoResult Wait(Handle handle, | 20 inline MojoResult Wait(Handle handle, |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 45 bool AreSignalsStatesValid() const { | 45 bool AreSignalsStatesValid() const { |
| 46 return result != MOJO_RESULT_INVALID_ARGUMENT && | 46 return result != MOJO_RESULT_INVALID_ARGUMENT && |
| 47 result != MOJO_RESULT_RESOURCE_EXHAUSTED && | 47 result != MOJO_RESULT_RESOURCE_EXHAUSTED && |
| 48 result != MOJO_RESULT_BUSY; | 48 result != MOJO_RESULT_BUSY; |
| 49 } | 49 } |
| 50 | 50 |
| 51 MojoResult result; | 51 MojoResult result; |
| 52 uint32_t index; | 52 uint32_t index; |
| 53 }; | 53 }; |
| 54 | 54 |
| 55 // |HandleVectorType| and |FlagsVectorType| should be similar enough to | 55 // |HandleType| should be |Handle| or a "trivial" subclass thereof, like |
| 56 // |std::vector<Handle>| and |std::vector<MojoHandleSignals>|, respectively: | 56 // |MessagePipeHandle|, etc. |
| 57 // - They should have a (const) |size()| method that returns an unsigned type. | 57 template <class HandleType> |
| 58 // - They must provide contiguous storage, with access via (const) reference to | 58 inline WaitManyResult WaitMany( |
| 59 // that storage provided by a (const) |operator[]()| (by reference). | 59 const std::vector<HandleType>& handles, |
|
vardhan
2016/06/29 18:02:18
I'm guessing it used to be templated so that it'd
| |
| 60 template <class HandleVectorType, | 60 const std::vector<MojoHandleSignals>& signals, |
| 61 class FlagsVectorType, | 61 MojoDeadline deadline, |
| 62 class SignalsStateVectorType> | 62 std::vector<MojoHandleSignalsState>* signals_states) { |
| 63 inline WaitManyResult WaitMany(const HandleVectorType& handles, | 63 // We rely on being able to treat a vector of |HandleType|s as if it's an |
| 64 const FlagsVectorType& signals, | 64 // array of |MojoHandle|s. |
| 65 MojoDeadline deadline, | 65 static_assert(sizeof(HandleType) == sizeof(Handle), |
| 66 SignalsStateVectorType* signals_states) { | 66 "HandleType is not the same size as Handle"); |
| 67 | |
| 67 if (signals.size() != handles.size() || | 68 if (signals.size() != handles.size() || |
| 68 (signals_states && signals_states->size() != signals.size())) | 69 (signals_states && signals_states->size() != signals.size())) |
| 69 return WaitManyResult(MOJO_RESULT_INVALID_ARGUMENT); | 70 return WaitManyResult(MOJO_RESULT_INVALID_ARGUMENT); |
| 70 if (handles.size() >= kInvalidWaitManyIndexValue) | 71 if (handles.size() >= kInvalidWaitManyIndexValue) |
| 71 return WaitManyResult(MOJO_RESULT_RESOURCE_EXHAUSTED); | 72 return WaitManyResult(MOJO_RESULT_RESOURCE_EXHAUSTED); |
| 72 | 73 |
| 73 if (handles.size() == 0) { | 74 if (handles.size() == 0) { |
| 74 return WaitManyResult( | 75 return WaitManyResult( |
| 75 MojoWaitMany(nullptr, nullptr, 0, deadline, nullptr, nullptr)); | 76 MojoWaitMany(nullptr, nullptr, 0, deadline, nullptr, nullptr)); |
| 76 } | 77 } |
| 77 | 78 |
| 78 uint32_t result_index = kInvalidWaitManyIndexValue; | 79 uint32_t result_index = kInvalidWaitManyIndexValue; |
| 79 const Handle& first_handle = handles[0]; | 80 MojoResult result = MojoWaitMany( |
| 80 const MojoHandleSignals& first_signals = signals[0]; | 81 &handles[0].value(), signals.data(), |
| 81 MojoHandleSignalsState* first_state = | 82 static_cast<uint32_t>(handles.size()), deadline, &result_index, |
| 82 signals_states ? &(*signals_states)[0] : nullptr; | 83 signals_states ? signals_states->data() : nullptr); |
| 83 MojoResult result = | |
| 84 MojoWaitMany(reinterpret_cast<const MojoHandle*>(&first_handle), | |
| 85 &first_signals, static_cast<uint32_t>(handles.size()), | |
| 86 deadline, &result_index, first_state); | |
| 87 return WaitManyResult(result, result_index); | 84 return WaitManyResult(result, result_index); |
| 88 } | 85 } |
| 89 | 86 |
| 90 // C++ 4.10, regarding pointer conversion, says that an integral null pointer | |
| 91 // constant can be converted to |std::nullptr_t|. The opposite direction is not | |
| 92 // allowed. | |
| 93 template <class HandleVectorType, class FlagsVectorType> | |
| 94 inline WaitManyResult WaitMany(const HandleVectorType& handles, | |
| 95 const FlagsVectorType& signals, | |
| 96 MojoDeadline deadline, | |
| 97 std::nullptr_t signals_states) { | |
| 98 if (signals.size() != handles.size()) | |
| 99 return WaitManyResult(MOJO_RESULT_INVALID_ARGUMENT); | |
| 100 if (handles.size() >= kInvalidWaitManyIndexValue) | |
| 101 return WaitManyResult(MOJO_RESULT_RESOURCE_EXHAUSTED); | |
| 102 | |
| 103 if (handles.size() == 0) { | |
| 104 return WaitManyResult( | |
| 105 MojoWaitMany(nullptr, nullptr, 0, deadline, nullptr, nullptr)); | |
| 106 } | |
| 107 | |
| 108 uint32_t result_index = kInvalidWaitManyIndexValue; | |
| 109 const Handle& first_handle = handles[0]; | |
| 110 const MojoHandleSignals& first_signals = signals[0]; | |
| 111 MojoResult result = MojoWaitMany( | |
| 112 reinterpret_cast<const MojoHandle*>(&first_handle), &first_signals, | |
| 113 static_cast<uint32_t>(handles.size()), deadline, &result_index, nullptr); | |
| 114 return WaitManyResult(result, result_index); | |
| 115 } | |
| 116 | |
| 117 } // namespace mojo | 87 } // namespace mojo |
| 118 | 88 |
| 119 #endif // MOJO_PUBLIC_CPP_SYSTEM_WAIT_H_ | 89 #endif // MOJO_PUBLIC_CPP_SYSTEM_WAIT_H_ |
| OLD | NEW |