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 // This file provides a C++ wrapping around the Mojo C API for wait sets, | |
6 // replacing the prefix of "Mojo" with a "mojo" namespace, and using more | |
7 // strongly-typed representations of |MojoHandle|s. | |
8 // | |
9 // Please see "mojo/public/c/include/mojo/system/wait_set.h" for complete | |
10 // documentation of the API. | |
11 | |
12 #ifndef MOJO_PUBLIC_CPP_SYSTEM_WAIT_SET_H_ | |
13 #define MOJO_PUBLIC_CPP_SYSTEM_WAIT_SET_H_ | |
14 | |
15 #include <assert.h> | |
16 #include <mojo/system/wait_set.h> | |
17 | |
18 #include <vector> | |
19 | |
20 #include "mojo/public/cpp/system/handle.h" | |
21 | |
22 namespace mojo { | |
23 | |
24 // A strongly-typed representation of a |MojoHandle| to a wait set. | |
25 class WaitSetHandle : public Handle { | |
26 public: | |
27 WaitSetHandle() {} | |
28 explicit WaitSetHandle(MojoHandle value) : Handle(value) {} | |
29 | |
30 // Copying and assignment allowed. | |
31 }; | |
32 | |
33 static_assert(sizeof(WaitSetHandle) == sizeof(Handle), | |
34 "Bad size for C++ WaitSetHandle"); | |
35 | |
36 typedef ScopedHandleBase<WaitSetHandle> ScopedWaitSetHandle; | |
37 static_assert(sizeof(ScopedWaitSetHandle) == sizeof(WaitSetHandle), | |
38 "Bad size for C++ ScopedWaitSetHandle"); | |
39 | |
40 // Creates a new wait set. See |MojoCreateWaitSet()| for complete documentation. | |
41 inline MojoResult CreateWaitSet(const MojoCreateWaitSetOptions* options, | |
42 ScopedWaitSetHandle* wait_set) { | |
43 assert(wait_set); | |
44 WaitSetHandle wait_set_handle; | |
45 MojoResult rv = MojoCreateWaitSet(options, wait_set_handle.mutable_value()); | |
46 // Reset even on failure (reduces the chances that a "stale"/incorrect handle | |
47 // will be used). | |
48 wait_set->reset(wait_set_handle); | |
49 return rv; | |
50 } | |
51 | |
52 inline MojoResult WaitSetAdd(WaitSetHandle wait_set, | |
53 Handle handle, | |
54 MojoHandleSignals signals, | |
55 uint64_t cookie, | |
56 const struct MojoWaitSetAddOptions* options) { | |
57 return MojoWaitSetAdd(wait_set.value(), handle.value(), signals, cookie, | |
58 options); | |
59 } | |
60 | |
61 inline MojoResult WaitSetRemove(WaitSetHandle wait_set, uint64_t cookie) { | |
62 return MojoWaitSetRemove(wait_set.value(), cookie); | |
63 } | |
64 | |
65 inline MojoResult WaitSetWait(WaitSetHandle wait_set, | |
66 MojoDeadline deadline, | |
67 std::vector<MojoWaitSetResult>* results, | |
68 uint32_t* max_results) { | |
69 if (!results) { | |
70 return MojoWaitSetWait(wait_set.value(), deadline, 0u, nullptr, | |
71 max_results); | |
72 } | |
73 | |
74 assert(results->capacity() <= static_cast<uint32_t>(-1)); | |
75 uint32_t num_results = static_cast<uint32_t>(results->capacity()); | |
76 results->resize(num_results); | |
77 MojoResult rv = MojoWaitSetWait(wait_set.value(), deadline, &num_results, | |
78 results->data(), max_results); | |
79 results->resize((rv == MOJO_RESULT_OK) ? num_results : 0u); | |
80 return rv; | |
81 } | |
82 | |
83 } // namespace mojo | |
84 | |
85 #endif // MOJO_PUBLIC_CPP_SYSTEM_WAIT_SET_H_ | |
OLD | NEW |