OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef MOJO_PUBLIC_CPP_SYSTEM_WAIT_H_ | |
6 #define MOJO_PUBLIC_CPP_SYSTEM_WAIT_H_ | |
7 | |
8 #include <mojo/result.h> | |
9 #include <mojo/system/handle.h> | |
10 #include <mojo/system/time.h> | |
11 #include <mojo/system/wait.h> | |
12 #include <stdint.h> | |
13 | |
14 #include <vector> | |
15 | |
16 #include "mojo/public/cpp/system/handle.h" | |
17 | |
18 namespace mojo { | |
19 | |
20 inline MojoResult Wait(Handle handle, | |
21 MojoHandleSignals signals, | |
22 MojoDeadline deadline, | |
23 MojoHandleSignalsState* signals_state) { | |
24 return MojoWait(handle.value(), signals, deadline, signals_state); | |
25 } | |
26 | |
27 const uint32_t kInvalidWaitManyIndexValue = static_cast<uint32_t>(-1); | |
28 | |
29 // Simplify the interpretation of the output from |MojoWaitMany()|. | |
30 struct WaitManyResult { | |
31 explicit WaitManyResult(MojoResult mojo_wait_many_result) | |
32 : result(mojo_wait_many_result), index(kInvalidWaitManyIndexValue) {} | |
33 | |
34 WaitManyResult(MojoResult mojo_wait_many_result, uint32_t result_index) | |
35 : result(mojo_wait_many_result), index(result_index) {} | |
36 | |
37 // A valid handle index is always returned if |WaitMany()| succeeds, but may | |
38 // or may not be returned if |WaitMany()| returns an error. Use this helper | |
39 // function to check if |index| is a valid index into the handle array. | |
40 bool IsIndexValid() const { return index != kInvalidWaitManyIndexValue; } | |
41 | |
42 // The |signals_states| array is always returned by |WaitMany()| on success, | |
43 // but may or may not be returned if |WaitMany()| returns an error. Use this | |
44 // helper function to check if |signals_states| holds valid data. | |
45 bool AreSignalsStatesValid() const { | |
46 return result != MOJO_RESULT_INVALID_ARGUMENT && | |
47 result != MOJO_RESULT_RESOURCE_EXHAUSTED && | |
48 result != MOJO_RESULT_BUSY; | |
49 } | |
50 | |
51 MojoResult result; | |
52 uint32_t index; | |
53 }; | |
54 | |
55 // |HandleType| should be |Handle| or a "trivial" subclass thereof, like | |
56 // |MessagePipeHandle|, etc. | |
57 template <class HandleType> | |
58 inline WaitManyResult WaitMany( | |
59 const std::vector<HandleType>& handles, | |
60 const std::vector<MojoHandleSignals>& signals, | |
61 MojoDeadline deadline, | |
62 std::vector<MojoHandleSignalsState>* signals_states) { | |
63 // We rely on being able to treat a vector of |HandleType|s as if it's an | |
64 // array of |MojoHandle|s. | |
65 static_assert(sizeof(HandleType) == sizeof(Handle), | |
66 "HandleType is not the same size as Handle"); | |
67 | |
68 if (signals.size() != handles.size() || | |
69 (signals_states && signals_states->size() != signals.size())) | |
70 return WaitManyResult(MOJO_RESULT_INVALID_ARGUMENT); | |
71 if (handles.size() >= kInvalidWaitManyIndexValue) | |
72 return WaitManyResult(MOJO_RESULT_RESOURCE_EXHAUSTED); | |
73 | |
74 if (handles.size() == 0) { | |
75 return WaitManyResult( | |
76 MojoWaitMany(nullptr, nullptr, 0, deadline, nullptr, nullptr)); | |
77 } | |
78 | |
79 uint32_t result_index = kInvalidWaitManyIndexValue; | |
80 MojoResult result = MojoWaitMany( | |
81 &handles[0].value(), signals.data(), | |
82 static_cast<uint32_t>(handles.size()), deadline, &result_index, | |
83 signals_states ? signals_states->data() : nullptr); | |
84 return WaitManyResult(result, result_index); | |
85 } | |
86 | |
87 } // namespace mojo | |
88 | |
89 #endif // MOJO_PUBLIC_CPP_SYSTEM_WAIT_H_ | |
OLD | NEW |