• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
19 #include <android-base/result.h>
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     /**
38      * 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.
43      */
44     [[nodiscard]] bool isTriggered();
45 
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      */
55     [[nodiscard]] status_t triggerablePoll(base::borrowed_fd fd, int16_t event);
56 
57 private:
58     base::unique_fd mWrite;
59     base::unique_fd mRead;
60 };
61 } // namespace android
62