blob: 78cdaaa4083bec710658bd926844616668070d4d [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 Hong8c950422021-08-05 17:13:55 -070020#include <utils/Errors.h>
21
Pawan49d74cb2022-08-03 21:19:11 +000022#include <binder/RpcTransport.h>
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -070023#include <binder/unique_fd.h>
Pawan49d74cb2022-08-03 21:19:11 +000024
Yifan Hong8c950422021-08-05 17:13:55 -070025namespace android {
26
27/** This is not a pipe. */
Frederick Maylef7b65d12024-05-14 16:55:14 -070028class LIBBINDER_INTERNAL_EXPORTED FdTrigger {
Yifan Hong8c950422021-08-05 17:13:55 -070029public:
30 /** Returns nullptr for error case */
31 static std::unique_ptr<FdTrigger> make();
32
33 /**
34 * Close the write end of the pipe so that the read end receives POLLHUP.
35 * Not threadsafe.
36 */
37 void trigger();
38
39 /**
Steven Morelandd6bca102021-09-14 16:25:22 -070040 * Check whether this has been triggered by checking the write end. Note:
41 * this has no internal locking, and it is inherently racey, but this is
42 * okay, because if we accidentally return false when a trigger has already
43 * happened, we can imagine that instead, the scheduler actually executed
44 * the code which is polling isTriggered earlier.
Yifan Hong8c950422021-08-05 17:13:55 -070045 */
Steven Morelanddd7f17a2021-09-14 13:48:05 -070046 [[nodiscard]] bool isTriggered();
Yifan Hong8c950422021-08-05 17:13:55 -070047
48 /**
49 * Poll for a read event.
50 *
51 * event - for pollfd
52 *
53 * Return:
54 * true - time to read!
55 * false - trigger happened
56 */
Pawan3e0061c2022-08-26 21:08:34 +000057 [[nodiscard]] status_t triggerablePoll(const android::RpcTransportFd& transportFd,
58 int16_t event);
Yifan Hong8c950422021-08-05 17:13:55 -070059
Yifan Hong8c950422021-08-05 17:13:55 -070060private:
Andrei Homescuffa3aaa2022-04-07 05:06:33 +000061#ifdef BINDER_RPC_SINGLE_THREADED
62 bool mTriggered = false;
63#else
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -070064 binder::unique_fd mWrite;
65 binder::unique_fd mRead;
Andrei Homescuffa3aaa2022-04-07 05:06:33 +000066#endif
Yifan Hong8c950422021-08-05 17:13:55 -070067};
68} // namespace android