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

Side by Side Diff: mojo/public/cpp/system/wait.h

Issue 1801793003: Move mojo::{Wait,WaitMany}() to (the newly-added) mojo/public/cpp/system/wait.h. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 4 years, 9 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
OLDNEW
(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 <stdint.h>
9
10 #include <cstddef>
11
12 #include "mojo/public/c/system/handle.h"
13 #include "mojo/public/c/system/result.h"
14 #include "mojo/public/c/system/time.h"
15 #include "mojo/public/c/system/wait.h"
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 }
49
50 MojoResult result;
51 uint32_t index;
52 };
53
54 // |HandleVectorType| and |FlagsVectorType| should be similar enough to
55 // |std::vector<Handle>| and |std::vector<MojoHandleSignals>|, respectively:
56 // - They should have a (const) |size()| method that returns an unsigned type.
57 // - They must provide contiguous storage, with access via (const) reference to
58 // that storage provided by a (const) |operator[]()| (by reference).
59 template <class HandleVectorType,
60 class FlagsVectorType,
61 class SignalsStateVectorType>
62 inline WaitManyResult WaitMany(const HandleVectorType& handles,
63 const FlagsVectorType& signals,
64 MojoDeadline deadline,
65 SignalsStateVectorType* signals_states) {
66 if (signals.size() != handles.size() ||
67 (signals_states && signals_states->size() != signals.size()))
68 return WaitManyResult(MOJO_RESULT_INVALID_ARGUMENT);
69 if (handles.size() >= kInvalidWaitManyIndexValue)
70 return WaitManyResult(MOJO_RESULT_RESOURCE_EXHAUSTED);
71
72 if (handles.size() == 0) {
73 return WaitManyResult(
74 MojoWaitMany(nullptr, nullptr, 0, deadline, nullptr, nullptr));
75 }
76
77 uint32_t result_index = kInvalidWaitManyIndexValue;
78 const Handle& first_handle = handles[0];
79 const MojoHandleSignals& first_signals = signals[0];
80 MojoHandleSignalsState* first_state =
81 signals_states ? &(*signals_states)[0] : nullptr;
82 MojoResult result =
83 MojoWaitMany(reinterpret_cast<const MojoHandle*>(&first_handle),
84 &first_signals, static_cast<uint32_t>(handles.size()),
85 deadline, &result_index, first_state);
86 return WaitManyResult(result, result_index);
87 }
88
89 // C++ 4.10, regarding pointer conversion, says that an integral null pointer
90 // constant can be converted to |std::nullptr_t|. The opposite direction is not
91 // allowed.
92 template <class HandleVectorType, class FlagsVectorType>
93 inline WaitManyResult WaitMany(const HandleVectorType& handles,
94 const FlagsVectorType& signals,
95 MojoDeadline deadline,
96 std::nullptr_t signals_states) {
97 if (signals.size() != handles.size())
98 return WaitManyResult(MOJO_RESULT_INVALID_ARGUMENT);
99 if (handles.size() >= kInvalidWaitManyIndexValue)
100 return WaitManyResult(MOJO_RESULT_RESOURCE_EXHAUSTED);
101
102 if (handles.size() == 0) {
103 return WaitManyResult(
104 MojoWaitMany(nullptr, nullptr, 0, deadline, nullptr, nullptr));
105 }
106
107 uint32_t result_index = kInvalidWaitManyIndexValue;
108 const Handle& first_handle = handles[0];
109 const MojoHandleSignals& first_signals = signals[0];
110 MojoResult result = MojoWaitMany(
111 reinterpret_cast<const MojoHandle*>(&first_handle), &first_signals,
112 static_cast<uint32_t>(handles.size()), deadline, &result_index, nullptr);
113 return WaitManyResult(result, result_index);
114 }
115
116 } // namespace mojo
117
118 #endif // MOJO_PUBLIC_CPP_SYSTEM_WAIT_H_
OLDNEW
« no previous file with comments | « mojo/public/cpp/system/tests/core_unittest.cc ('k') | mojo/public/cpp/test_support/lib/test_utils.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698