1 /* 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 */ 16 #pragma once 17 18 #include <memory> 19 20 #include <utils/Errors.h> 21 22 #include <binder/RpcTransport.h> 23 #include <binder/unique_fd.h> 24 25 namespace android { 26 27 /** This is not a pipe. */ 28 class LIBBINDER_INTERNAL_EXPORTED FdTrigger { 29 public: 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 /** 40 * 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. 45 */ 46 [[nodiscard]] bool isTriggered(); 47 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 */ 57 [[nodiscard]] status_t triggerablePoll(const android::RpcTransportFd& transportFd, 58 int16_t event); 59 60 private: 61 #ifdef BINDER_RPC_SINGLE_THREADED 62 bool mTriggered = false; 63 #else 64 binder::unique_fd mWrite; 65 binder::unique_fd mRead; 66 #endif 67 }; 68 } // namespace android 69