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

Side by Side Diff: mojo/public/c/environment/tests/async_waiter_perftest_helpers.cc

Issue 2235993002: Move mojo/public/c/environment/tests to mojo/public/c/tests/environment. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: oops Created 4 years, 4 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 #include "mojo/public/c/environment/tests/async_waiter_perftest_helpers.h"
6
7 #include <assert.h>
8 #include <mojo/system/handle.h>
9 #include <mojo/system/message_pipe.h>
10
11 #include <vector>
12
13 #include "mojo/public/cpp/system/macros.h"
14
15 #undef CHECK_OK
16 #ifdef NDEBUG
17 #define CHECK_OK(result) \
18 do { \
19 (void)result; \
20 } while (false)
21 #else
22 #define CHECK_OK(result) \
23 do { \
24 MojoResult result_internal = (result); \
25 assert(result_internal == MOJO_RESULT_OK); \
26 } while (false)
27 #endif
28
29 namespace mojo {
30 namespace test {
31 namespace {
32
33 class AsyncWaiterPerfTest {
34 public:
35 explicit AsyncWaiterPerfTest(const MojoAsyncWaiter* async_waiter,
36 uint32_t num_handles,
37 std::function<void()> run_loop_function)
38 : async_waiter_(async_waiter),
39 num_handles_(num_handles),
40 run_loop_function_(run_loop_function),
41 handle0s_(num_handles, MOJO_HANDLE_INVALID),
42 handle1s_(num_handles, MOJO_HANDLE_INVALID),
43 contexts_(num_handles) {}
44 ~AsyncWaiterPerfTest() {}
45
46 uint64_t DoIt() {
47 for (uint32_t i = 0; i < num_handles_; i++) {
48 CHECK_OK(MojoCreateMessagePipe(nullptr, &handle0s_[i], &handle1s_[i]));
49 AddAsyncWaiter(i);
50 }
51
52 // "Signal" the first async wait (i.e., write a message).
53 CHECK_OK(MojoWriteMessage(handle1s_[0], nullptr, 0, nullptr, 0,
54 MOJO_WRITE_MESSAGE_FLAG_NONE));
55
56 run_loop_function_();
57
58 for (uint32_t i = 0; i < num_handles_; i++) {
59 CancelAsyncWaiter(i);
60 CHECK_OK(MojoClose(handle0s_[i]));
61 CHECK_OK(MojoClose(handle1s_[i]));
62 }
63
64 return callback_count_;
65 }
66
67 private:
68 struct Context {
69 AsyncWaiterPerfTest* thiz = nullptr;
70 uint32_t index = 0;
71 MojoAsyncWaitID id = 0;
72 };
73
74 void AddAsyncWaiter(uint32_t index) {
75 assert(index < num_handles_);
76
77 Context& context = contexts_[index];
78 context.thiz = this;
79 context.index = index;
80 context.id = async_waiter_->AsyncWait(
81 handle0s_[index], MOJO_HANDLE_SIGNAL_READABLE, MOJO_DEADLINE_INDEFINITE,
82 &AsyncWaiterPerfTest::AsyncWaitCallbackThunk, &context);
83 }
84
85 void CancelAsyncWaiter(uint32_t index) {
86 async_waiter_->CancelWait(contexts_[index].id);
87 }
88
89 static void AsyncWaitCallbackThunk(void* closure, MojoResult result) {
90 CHECK_OK(result);
91 auto context = static_cast<Context*>(closure);
92 context->thiz->AsyncWaitCallback(context);
93 }
94
95 void AsyncWaitCallback(Context* context) {
96 callback_count_++;
97
98 uint32_t index = context->index;
99
100 // "Unsignal" (i.e., consume a message)).
101 CHECK_OK(MojoReadMessage(handle0s_[index], nullptr, nullptr, nullptr,
102 nullptr, MOJO_READ_MESSAGE_FLAG_MAY_DISCARD));
103
104 // Replace ourself.
105 AddAsyncWaiter(index);
106
107 // "Signal" the next one (i.e., write a message).
108 CHECK_OK(MojoWriteMessage(handle1s_[(index + 1) % num_handles_], nullptr, 0,
109 nullptr, 0, MOJO_WRITE_MESSAGE_FLAG_NONE));
110 }
111
112 const MojoAsyncWaiter* const async_waiter_;
113 const uint32_t num_handles_;
114 const std::function<void()> run_loop_function_;
115
116 // We'll always wait on the |handle0s_| and "signal" from the |handle1s_|.
117 std::vector<MojoHandle> handle0s_;
118 std::vector<MojoHandle> handle1s_;
119 std::vector<Context> contexts_;
120
121 uint64_t callback_count_ = 0;
122
123 MOJO_DISALLOW_COPY_AND_ASSIGN(AsyncWaiterPerfTest);
124 };
125
126 } // namespace
127
128 uint64_t DoAsyncWaiterPerfTest(const MojoAsyncWaiter* async_waiter,
129 uint32_t num_handles,
130 std::function<void()> run_loop_function) {
131 return AsyncWaiterPerfTest(async_waiter, num_handles, run_loop_function)
132 .DoIt();
133 }
134
135 } // namespace test
136 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698