blob: a25dc117f37935bd6fc5b24b48b3f51b44bfa4b3 [file] [log] [blame]
Yifan Hong8c950422021-08-05 17:13:55 -07001/*
2 * Copyright (C) 2021 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Andrei Homescu9b404192022-07-21 00:55:10 +000016#pragma once
Yifan Hong8c950422021-08-05 17:13:55 -070017
18#include <memory>
19
Yifan Hong15fff8c2021-08-10 15:07:56 -070020#include <android-base/result.h>
Yifan Hong8c950422021-08-05 17:13:55 -070021#include <android-base/unique_fd.h>
22#include <utils/Errors.h>
23
24namespace android {
25
26/** This is not a pipe. */
27class FdTrigger {
28public:
29 /** Returns nullptr for error case */
30 static std::unique_ptr<FdTrigger> make();
31
32 /**
33 * Close the write end of the pipe so that the read end receives POLLHUP.
34 * Not threadsafe.
35 */
36 void trigger();
37
38 /**
Steven Morelandd6bca102021-09-14 16:25:22 -070039 * Check whether this has been triggered by checking the write end. Note:
40 * this has no internal locking, and it is inherently racey, but this is
41 * okay, because if we accidentally return false when a trigger has already
42 * happened, we can imagine that instead, the scheduler actually executed
43 * the code which is polling isTriggered earlier.
Yifan Hong8c950422021-08-05 17:13:55 -070044 */
Steven Morelanddd7f17a2021-09-14 13:48:05 -070045 [[nodiscard]] bool isTriggered();
Yifan Hong8c950422021-08-05 17:13:55 -070046
47 /**
48 * Poll for a read event.
49 *
50 * event - for pollfd
51 *
52 * Return:
53 * true - time to read!
54 * false - trigger happened
55 */
Steven Morelanddd7f17a2021-09-14 13:48:05 -070056 [[nodiscard]] status_t triggerablePoll(base::borrowed_fd fd, int16_t event);
Yifan Hong8c950422021-08-05 17:13:55 -070057
Yifan Hong8c950422021-08-05 17:13:55 -070058private:
Andrei Homescuffa3aaa2022-04-07 05:06:33 +000059#ifdef BINDER_RPC_SINGLE_THREADED
60 bool mTriggered = false;
61#else
Yifan Hong8c950422021-08-05 17:13:55 -070062 base::unique_fd mWrite;
63 base::unique_fd mRead;
Andrei Homescuffa3aaa2022-04-07 05:06:33 +000064#endif
Yifan Hong8c950422021-08-05 17:13:55 -070065};
66} // namespace android