| Yifan Hong | 8c95042 | 2021-08-05 17:13:55 -0700 | [diff] [blame] | 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 | */ | 
| Andrei Homescu | 9b40419 | 2022-07-21 00:55:10 +0000 | [diff] [blame] | 16 | #pragma once | 
| Yifan Hong | 8c95042 | 2021-08-05 17:13:55 -0700 | [diff] [blame] | 17 |  | 
|  | 18 | #include <memory> | 
|  | 19 |  | 
| Yifan Hong | 15fff8c | 2021-08-10 15:07:56 -0700 | [diff] [blame] | 20 | #include <android-base/result.h> | 
| Yifan Hong | 8c95042 | 2021-08-05 17:13:55 -0700 | [diff] [blame] | 21 | #include <android-base/unique_fd.h> | 
|  | 22 | #include <utils/Errors.h> | 
|  | 23 |  | 
| Pawan | 49d74cb | 2022-08-03 21:19:11 +0000 | [diff] [blame] | 24 | #include <binder/RpcTransport.h> | 
|  | 25 |  | 
| Yifan Hong | 8c95042 | 2021-08-05 17:13:55 -0700 | [diff] [blame] | 26 | namespace android { | 
|  | 27 |  | 
|  | 28 | /** This is not a pipe. */ | 
|  | 29 | class FdTrigger { | 
|  | 30 | public: | 
|  | 31 | /** Returns nullptr for error case */ | 
|  | 32 | static std::unique_ptr<FdTrigger> make(); | 
|  | 33 |  | 
|  | 34 | /** | 
|  | 35 | * Close the write end of the pipe so that the read end receives POLLHUP. | 
|  | 36 | * Not threadsafe. | 
|  | 37 | */ | 
|  | 38 | void trigger(); | 
|  | 39 |  | 
|  | 40 | /** | 
| Steven Moreland | d6bca10 | 2021-09-14 16:25:22 -0700 | [diff] [blame] | 41 | * Check whether this has been triggered by checking the write end. Note: | 
|  | 42 | * this has no internal locking, and it is inherently racey, but this is | 
|  | 43 | * okay, because if we accidentally return false when a trigger has already | 
|  | 44 | * happened, we can imagine that instead, the scheduler actually executed | 
|  | 45 | * the code which is polling isTriggered earlier. | 
| Yifan Hong | 8c95042 | 2021-08-05 17:13:55 -0700 | [diff] [blame] | 46 | */ | 
| Steven Moreland | dd7f17a | 2021-09-14 13:48:05 -0700 | [diff] [blame] | 47 | [[nodiscard]] bool isTriggered(); | 
| Yifan Hong | 8c95042 | 2021-08-05 17:13:55 -0700 | [diff] [blame] | 48 |  | 
|  | 49 | /** | 
|  | 50 | * Poll for a read event. | 
|  | 51 | * | 
|  | 52 | * event - for pollfd | 
|  | 53 | * | 
|  | 54 | * Return: | 
|  | 55 | *   true - time to read! | 
|  | 56 | *   false - trigger happened | 
|  | 57 | */ | 
| Pawan | 3e0061c | 2022-08-26 21:08:34 +0000 | [diff] [blame] | 58 | [[nodiscard]] status_t triggerablePoll(const android::RpcTransportFd& transportFd, | 
|  | 59 | int16_t event); | 
| Yifan Hong | 8c95042 | 2021-08-05 17:13:55 -0700 | [diff] [blame] | 60 |  | 
| Yifan Hong | 8c95042 | 2021-08-05 17:13:55 -0700 | [diff] [blame] | 61 | private: | 
| Andrei Homescu | ffa3aaa | 2022-04-07 05:06:33 +0000 | [diff] [blame] | 62 | #ifdef BINDER_RPC_SINGLE_THREADED | 
|  | 63 | bool mTriggered = false; | 
|  | 64 | #else | 
| Yifan Hong | 8c95042 | 2021-08-05 17:13:55 -0700 | [diff] [blame] | 65 | base::unique_fd mWrite; | 
|  | 66 | base::unique_fd mRead; | 
| Andrei Homescu | ffa3aaa | 2022-04-07 05:06:33 +0000 | [diff] [blame] | 67 | #endif | 
| Yifan Hong | 8c95042 | 2021-08-05 17:13:55 -0700 | [diff] [blame] | 68 | }; | 
|  | 69 | } // namespace android |