blob: 5f67ba318818edb2b3d40abfcb975c66695fbe46 [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 */
16
17#include <memory>
18
Yifan Hong15fff8c2021-08-10 15:07:56 -070019#include <android-base/result.h>
Yifan Hong8c950422021-08-05 17:13:55 -070020#include <android-base/unique_fd.h>
21#include <utils/Errors.h>
22
23namespace android {
24
25/** This is not a pipe. */
26class FdTrigger {
27public:
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 Hong15fff8c2021-08-10 15:07:56 -070038 * Check whether this has been triggered by checking the write end.
Yifan Hong8c950422021-08-05 17:13:55 -070039 */
Steven Morelanddd7f17a2021-09-14 13:48:05 -070040 [[nodiscard]] bool isTriggered();
Yifan Hong8c950422021-08-05 17:13:55 -070041
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 Morelanddd7f17a2021-09-14 13:48:05 -070051 [[nodiscard]] status_t triggerablePoll(base::borrowed_fd fd, int16_t event);
Yifan Hong8c950422021-08-05 17:13:55 -070052
Yifan Hong15fff8c2021-08-10 15:07:56 -070053 /**
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 Morelanddd7f17a2021-09-14 13:48:05 -070061 [[nodiscard]] android::base::Result<bool> isTriggeredPolled();
Yifan Hong15fff8c2021-08-10 15:07:56 -070062
Yifan Hong8c950422021-08-05 17:13:55 -070063private:
64 base::unique_fd mWrite;
65 base::unique_fd mRead;
66};
67} // namespace android