xref: /aosp_15_r20/frameworks/native/libs/binder/FdTrigger.h (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1*38e8c45fSAndroid Build Coastguard Worker /*
2*38e8c45fSAndroid Build Coastguard Worker  * Copyright (C) 2021 The Android Open Source Project
3*38e8c45fSAndroid Build Coastguard Worker  *
4*38e8c45fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*38e8c45fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*38e8c45fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*38e8c45fSAndroid Build Coastguard Worker  *
8*38e8c45fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*38e8c45fSAndroid Build Coastguard Worker  *
10*38e8c45fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*38e8c45fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*38e8c45fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*38e8c45fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*38e8c45fSAndroid Build Coastguard Worker  * limitations under the License.
15*38e8c45fSAndroid Build Coastguard Worker  */
16*38e8c45fSAndroid Build Coastguard Worker #pragma once
17*38e8c45fSAndroid Build Coastguard Worker 
18*38e8c45fSAndroid Build Coastguard Worker #include <memory>
19*38e8c45fSAndroid Build Coastguard Worker 
20*38e8c45fSAndroid Build Coastguard Worker #include <utils/Errors.h>
21*38e8c45fSAndroid Build Coastguard Worker 
22*38e8c45fSAndroid Build Coastguard Worker #include <binder/RpcTransport.h>
23*38e8c45fSAndroid Build Coastguard Worker #include <binder/unique_fd.h>
24*38e8c45fSAndroid Build Coastguard Worker 
25*38e8c45fSAndroid Build Coastguard Worker namespace android {
26*38e8c45fSAndroid Build Coastguard Worker 
27*38e8c45fSAndroid Build Coastguard Worker /** This is not a pipe. */
28*38e8c45fSAndroid Build Coastguard Worker class LIBBINDER_INTERNAL_EXPORTED FdTrigger {
29*38e8c45fSAndroid Build Coastguard Worker public:
30*38e8c45fSAndroid Build Coastguard Worker     /** Returns nullptr for error case */
31*38e8c45fSAndroid Build Coastguard Worker     static std::unique_ptr<FdTrigger> make();
32*38e8c45fSAndroid Build Coastguard Worker 
33*38e8c45fSAndroid Build Coastguard Worker     /**
34*38e8c45fSAndroid Build Coastguard Worker      * Close the write end of the pipe so that the read end receives POLLHUP.
35*38e8c45fSAndroid Build Coastguard Worker      * Not threadsafe.
36*38e8c45fSAndroid Build Coastguard Worker      */
37*38e8c45fSAndroid Build Coastguard Worker     void trigger();
38*38e8c45fSAndroid Build Coastguard Worker 
39*38e8c45fSAndroid Build Coastguard Worker     /**
40*38e8c45fSAndroid Build Coastguard Worker      * Check whether this has been triggered by checking the write end. Note:
41*38e8c45fSAndroid Build Coastguard Worker      * this has no internal locking, and it is inherently racey, but this is
42*38e8c45fSAndroid Build Coastguard Worker      * okay, because if we accidentally return false when a trigger has already
43*38e8c45fSAndroid Build Coastguard Worker      * happened, we can imagine that instead, the scheduler actually executed
44*38e8c45fSAndroid Build Coastguard Worker      * the code which is polling isTriggered earlier.
45*38e8c45fSAndroid Build Coastguard Worker      */
46*38e8c45fSAndroid Build Coastguard Worker     [[nodiscard]] bool isTriggered();
47*38e8c45fSAndroid Build Coastguard Worker 
48*38e8c45fSAndroid Build Coastguard Worker     /**
49*38e8c45fSAndroid Build Coastguard Worker      * Poll for a read event.
50*38e8c45fSAndroid Build Coastguard Worker      *
51*38e8c45fSAndroid Build Coastguard Worker      * event - for pollfd
52*38e8c45fSAndroid Build Coastguard Worker      *
53*38e8c45fSAndroid Build Coastguard Worker      * Return:
54*38e8c45fSAndroid Build Coastguard Worker      *   true - time to read!
55*38e8c45fSAndroid Build Coastguard Worker      *   false - trigger happened
56*38e8c45fSAndroid Build Coastguard Worker      */
57*38e8c45fSAndroid Build Coastguard Worker     [[nodiscard]] status_t triggerablePoll(const android::RpcTransportFd& transportFd,
58*38e8c45fSAndroid Build Coastguard Worker                                            int16_t event);
59*38e8c45fSAndroid Build Coastguard Worker 
60*38e8c45fSAndroid Build Coastguard Worker private:
61*38e8c45fSAndroid Build Coastguard Worker #ifdef BINDER_RPC_SINGLE_THREADED
62*38e8c45fSAndroid Build Coastguard Worker     bool mTriggered = false;
63*38e8c45fSAndroid Build Coastguard Worker #else
64*38e8c45fSAndroid Build Coastguard Worker     binder::unique_fd mWrite;
65*38e8c45fSAndroid Build Coastguard Worker     binder::unique_fd mRead;
66*38e8c45fSAndroid Build Coastguard Worker #endif
67*38e8c45fSAndroid Build Coastguard Worker };
68*38e8c45fSAndroid Build Coastguard Worker } // namespace android
69