David 'Digit' Turner | b1c2c95 | 2009-05-18 17:07:46 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 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 | #ifndef __FDEVENT_H |
| 18 | #define __FDEVENT_H |
| 19 | |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 20 | #include <stddef.h> |
Josh Gao | 06d4e74 | 2019-01-31 15:51:52 -0800 | [diff] [blame] | 21 | #include <stdint.h> |
David 'Digit' Turner | 1f1efb5 | 2009-05-18 17:36:28 +0200 | [diff] [blame] | 22 | |
Josh Gao | 06d4e74 | 2019-01-31 15:51:52 -0800 | [diff] [blame] | 23 | #include <chrono> |
Josh Gao | 764f8c5 | 2017-05-03 14:10:39 -0700 | [diff] [blame] | 24 | #include <functional> |
Josh Gao | 06d4e74 | 2019-01-31 15:51:52 -0800 | [diff] [blame] | 25 | #include <optional> |
Josh Gao | 87a2db6 | 2019-01-25 15:30:25 -0800 | [diff] [blame] | 26 | #include <variant> |
Josh Gao | 764f8c5 | 2017-05-03 14:10:39 -0700 | [diff] [blame] | 27 | |
Josh Gao | d09ba31 | 2018-05-14 13:42:49 -0700 | [diff] [blame] | 28 | #include "adb_unique_fd.h" |
| 29 | |
Josh Gao | 06d4e74 | 2019-01-31 15:51:52 -0800 | [diff] [blame] | 30 | // Events that may be observed |
| 31 | #define FDE_READ 0x0001 |
| 32 | #define FDE_WRITE 0x0002 |
| 33 | #define FDE_ERROR 0x0004 |
| 34 | #define FDE_TIMEOUT 0x0008 |
David 'Digit' Turner | b1c2c95 | 2009-05-18 17:07:46 +0200 | [diff] [blame] | 35 | |
Josh Gao | 9e4aa3a | 2019-07-08 15:23:17 -0700 | [diff] [blame^] | 36 | // Internal states. |
| 37 | #define FDE_EVENTMASK 0x00ff |
| 38 | #define FDE_STATEMASK 0xff00 |
| 39 | |
| 40 | #define FDE_ACTIVE 0x0100 |
| 41 | #define FDE_PENDING 0x0200 |
| 42 | #define FDE_CREATED 0x0400 |
| 43 | |
David 'Digit' Turner | b1c2c95 | 2009-05-18 17:07:46 +0200 | [diff] [blame] | 44 | typedef void (*fd_func)(int fd, unsigned events, void *userdata); |
Josh Gao | 87a2db6 | 2019-01-25 15:30:25 -0800 | [diff] [blame] | 45 | typedef void (*fd_func2)(struct fdevent* fde, unsigned events, void* userdata); |
David 'Digit' Turner | b1c2c95 | 2009-05-18 17:07:46 +0200 | [diff] [blame] | 46 | |
Josh Gao | c081050 | 2019-06-28 16:34:36 -0700 | [diff] [blame] | 47 | struct fdevent; |
Josh Gao | 9e4aa3a | 2019-07-08 15:23:17 -0700 | [diff] [blame^] | 48 | std::string dump_fde(const fdevent* fde); |
Josh Gao | c081050 | 2019-06-28 16:34:36 -0700 | [diff] [blame] | 49 | |
| 50 | struct fdevent_context { |
| 51 | virtual ~fdevent_context() = default; |
| 52 | |
| 53 | // Allocate and initialize a new fdevent object. |
| 54 | virtual fdevent* Create(unique_fd fd, std::variant<fd_func, fd_func2> func, void* arg) = 0; |
| 55 | |
| 56 | // Deallocate an fdevent object, returning the file descriptor that was owned by it. |
| 57 | virtual unique_fd Destroy(fdevent* fde) = 0; |
| 58 | |
| 59 | // Change which events should cause notifications. |
| 60 | virtual void Set(fdevent* fde, unsigned events) = 0; |
| 61 | virtual void Add(fdevent* fde, unsigned events) = 0; |
| 62 | virtual void Del(fdevent* fde, unsigned events) = 0; |
| 63 | |
| 64 | // Set a timeout on an fdevent. |
| 65 | // If no events are triggered by the timeout, an FDE_TIMEOUT will be generated. |
| 66 | // Note timeouts are not defused automatically; if a timeout is set on an fdevent, it will |
| 67 | // trigger repeatedly every |timeout| ms. |
| 68 | virtual void SetTimeout(fdevent* fde, std::optional<std::chrono::milliseconds> timeout) = 0; |
| 69 | |
| 70 | // Loop forever, handling events. |
| 71 | virtual void Loop() = 0; |
| 72 | |
| 73 | // Assert that the caller is running on the context's main thread. |
| 74 | virtual void CheckMainThread() = 0; |
| 75 | |
| 76 | // Queue an operation to be run on the main thread. |
| 77 | virtual void Run(std::function<void()> fn) = 0; |
| 78 | |
| 79 | // Test-only functionality: |
| 80 | virtual void TerminateLoop() = 0; |
| 81 | virtual size_t InstalledCount() = 0; |
| 82 | virtual void Reset() = 0; |
| 83 | }; |
| 84 | |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 85 | struct fdevent { |
Josh Gao | 1293716 | 2018-06-18 14:55:27 -0700 | [diff] [blame] | 86 | uint64_t id; |
| 87 | |
Josh Gao | d09ba31 | 2018-05-14 13:42:49 -0700 | [diff] [blame] | 88 | unique_fd fd; |
Josh Gao | 9528df2 | 2018-05-14 11:14:33 -0700 | [diff] [blame] | 89 | int force_eof = 0; |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 90 | |
Josh Gao | 9528df2 | 2018-05-14 11:14:33 -0700 | [diff] [blame] | 91 | uint16_t state = 0; |
| 92 | uint16_t events = 0; |
Josh Gao | 06d4e74 | 2019-01-31 15:51:52 -0800 | [diff] [blame] | 93 | std::optional<std::chrono::milliseconds> timeout; |
| 94 | std::chrono::steady_clock::time_point last_active; |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 95 | |
Josh Gao | 87a2db6 | 2019-01-25 15:30:25 -0800 | [diff] [blame] | 96 | std::variant<fd_func, fd_func2> func; |
Josh Gao | 9528df2 | 2018-05-14 11:14:33 -0700 | [diff] [blame] | 97 | void* arg = nullptr; |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 98 | }; |
| 99 | |
Josh Gao | c081050 | 2019-06-28 16:34:36 -0700 | [diff] [blame] | 100 | // Backwards compatibility shims that forward to the global fdevent_context. |
| 101 | fdevent* fdevent_create(int fd, fd_func func, void* arg); |
Josh Gao | 87a2db6 | 2019-01-25 15:30:25 -0800 | [diff] [blame] | 102 | fdevent* fdevent_create(int fd, fd_func2 func, void* arg); |
David 'Digit' Turner | b1c2c95 | 2009-05-18 17:07:46 +0200 | [diff] [blame] | 103 | |
Josh Gao | ed15cac | 2018-09-20 17:38:55 -0700 | [diff] [blame] | 104 | unique_fd fdevent_release(fdevent* fde); |
Josh Gao | c081050 | 2019-06-28 16:34:36 -0700 | [diff] [blame] | 105 | void fdevent_destroy(fdevent* fde); |
Josh Gao | ed15cac | 2018-09-20 17:38:55 -0700 | [diff] [blame] | 106 | |
David 'Digit' Turner | b1c2c95 | 2009-05-18 17:07:46 +0200 | [diff] [blame] | 107 | void fdevent_set(fdevent *fde, unsigned events); |
| 108 | void fdevent_add(fdevent *fde, unsigned events); |
| 109 | void fdevent_del(fdevent *fde, unsigned events); |
Josh Gao | 06d4e74 | 2019-01-31 15:51:52 -0800 | [diff] [blame] | 110 | void fdevent_set_timeout(fdevent* fde, std::optional<std::chrono::milliseconds> timeout); |
David 'Digit' Turner | b1c2c95 | 2009-05-18 17:07:46 +0200 | [diff] [blame] | 111 | void fdevent_loop(); |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 112 | void check_main_thread(); |
| 113 | |
Josh Gao | 764f8c5 | 2017-05-03 14:10:39 -0700 | [diff] [blame] | 114 | // Queue an operation to run on the main thread. |
| 115 | void fdevent_run_on_main_thread(std::function<void()> fn); |
| 116 | |
Josh Gao | 511763b | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 117 | // The following functions are used only for tests. |
| 118 | void fdevent_terminate_loop(); |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 119 | size_t fdevent_installed_count(); |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 120 | void fdevent_reset(); |
David 'Digit' Turner | b1c2c95 | 2009-05-18 17:07:46 +0200 | [diff] [blame] | 121 | |
David 'Digit' Turner | b1c2c95 | 2009-05-18 17:07:46 +0200 | [diff] [blame] | 122 | #endif |