blob: 984e685ae1ed9ab71be4da38f9ba3f7e34e9705d [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
19#include <android-base/unique_fd.h>
20#include <utils/Errors.h>
21
22namespace android {
23
24/** This is not a pipe. */
25class FdTrigger {
26public:
27 /** Returns nullptr for error case */
28 static std::unique_ptr<FdTrigger> make();
29
30 /**
31 * Close the write end of the pipe so that the read end receives POLLHUP.
32 * Not threadsafe.
33 */
34 void trigger();
35
36 /**
37 * Whether this has been triggered.
38 */
39 bool isTriggered();
40
41 /**
42 * Poll for a read event.
43 *
44 * event - for pollfd
45 *
46 * Return:
47 * true - time to read!
48 * false - trigger happened
49 */
50 status_t triggerablePoll(base::borrowed_fd fd, int16_t event);
51
52private:
53 base::unique_fd mWrite;
54 base::unique_fd mRead;
55};
56} // namespace android