blob: a545d6cbea21e40ca7030ca53e632ab219a98498 [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 /**
Steven Morelandd6bca102021-09-14 16:25:22 -070038 * Check whether this has been triggered by checking the write end. Note:
39 * this has no internal locking, and it is inherently racey, but this is
40 * okay, because if we accidentally return false when a trigger has already
41 * happened, we can imagine that instead, the scheduler actually executed
42 * the code which is polling isTriggered earlier.
Yifan Hong8c950422021-08-05 17:13:55 -070043 */
Steven Morelanddd7f17a2021-09-14 13:48:05 -070044 [[nodiscard]] bool isTriggered();
Yifan Hong8c950422021-08-05 17:13:55 -070045
46 /**
47 * Poll for a read event.
48 *
49 * event - for pollfd
50 *
51 * Return:
52 * true - time to read!
53 * false - trigger happened
54 */
Steven Morelanddd7f17a2021-09-14 13:48:05 -070055 [[nodiscard]] status_t triggerablePoll(base::borrowed_fd fd, int16_t event);
Yifan Hong8c950422021-08-05 17:13:55 -070056
Yifan Hong8c950422021-08-05 17:13:55 -070057private:
58 base::unique_fd mWrite;
59 base::unique_fd mRead;
60};
61} // namespace android