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 | */ |
| 16 | |
| 17 | #include <memory> |
| 18 | |
Yifan Hong | 15fff8c | 2021-08-10 15:07:56 -0700 | [diff] [blame] | 19 | #include <android-base/result.h> |
Yifan Hong | 8c95042 | 2021-08-05 17:13:55 -0700 | [diff] [blame] | 20 | #include <android-base/unique_fd.h> |
| 21 | #include <utils/Errors.h> |
| 22 | |
| 23 | namespace android { |
| 24 | |
| 25 | /** This is not a pipe. */ |
| 26 | class FdTrigger { |
| 27 | public: |
| 28 | /** Returns nullptr for error case */ |
| 29 | static std::unique_ptr<FdTrigger> make(); |
| 30 | |
| 31 | /** |
| 32 | * Close the write end of the pipe so that the read end receives POLLHUP. |
| 33 | * Not threadsafe. |
| 34 | */ |
| 35 | void trigger(); |
| 36 | |
| 37 | /** |
Yifan Hong | 15fff8c | 2021-08-10 15:07:56 -0700 | [diff] [blame] | 38 | * Check whether this has been triggered by checking the write end. |
Yifan Hong | 8c95042 | 2021-08-05 17:13:55 -0700 | [diff] [blame] | 39 | */ |
Steven Moreland | dd7f17a | 2021-09-14 13:48:05 -0700 | [diff] [blame^] | 40 | [[nodiscard]] bool isTriggered(); |
Yifan Hong | 8c95042 | 2021-08-05 17:13:55 -0700 | [diff] [blame] | 41 | |
| 42 | /** |
| 43 | * Poll for a read event. |
| 44 | * |
| 45 | * event - for pollfd |
| 46 | * |
| 47 | * Return: |
| 48 | * true - time to read! |
| 49 | * false - trigger happened |
| 50 | */ |
Steven Moreland | dd7f17a | 2021-09-14 13:48:05 -0700 | [diff] [blame^] | 51 | [[nodiscard]] status_t triggerablePoll(base::borrowed_fd fd, int16_t event); |
Yifan Hong | 8c95042 | 2021-08-05 17:13:55 -0700 | [diff] [blame] | 52 | |
Yifan Hong | 15fff8c | 2021-08-10 15:07:56 -0700 | [diff] [blame] | 53 | /** |
| 54 | * Check whether this has been triggered by poll()ing the read end. |
| 55 | * |
| 56 | * Return: |
| 57 | * true - triggered |
| 58 | * false - not triggered |
| 59 | * error - error when polling |
| 60 | */ |
Steven Moreland | dd7f17a | 2021-09-14 13:48:05 -0700 | [diff] [blame^] | 61 | [[nodiscard]] android::base::Result<bool> isTriggeredPolled(); |
Yifan Hong | 15fff8c | 2021-08-10 15:07:56 -0700 | [diff] [blame] | 62 | |
Yifan Hong | 8c95042 | 2021-08-05 17:13:55 -0700 | [diff] [blame] | 63 | private: |
| 64 | base::unique_fd mWrite; |
| 65 | base::unique_fd mRead; |
| 66 | }; |
| 67 | } // namespace android |