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 | |
Anatol Pomazau | 324cb52 | 2019-08-08 14:45:12 -0700 | [diff] [blame] | 23 | #include <atomic> |
Josh Gao | 06d4e74 | 2019-01-31 15:51:52 -0800 | [diff] [blame] | 24 | #include <chrono> |
Josh Gao | c5e99e0 | 2019-07-08 17:37:23 -0700 | [diff] [blame] | 25 | #include <deque> |
Josh Gao | 764f8c5 | 2017-05-03 14:10:39 -0700 | [diff] [blame] | 26 | #include <functional> |
Colin Cross | 78d34d8 | 2023-05-16 22:10:26 +0000 | [diff] [blame] | 27 | #include <map> |
Josh Gao | c5e99e0 | 2019-07-08 17:37:23 -0700 | [diff] [blame] | 28 | #include <mutex> |
Josh Gao | 06d4e74 | 2019-01-31 15:51:52 -0800 | [diff] [blame] | 29 | #include <optional> |
Colin Cross | 9b24532 | 2023-05-16 22:10:31 +0000 | [diff] [blame] | 30 | #include <set> |
Josh Gao | 87a2db6 | 2019-01-25 15:30:25 -0800 | [diff] [blame] | 31 | #include <variant> |
Josh Gao | 764f8c5 | 2017-05-03 14:10:39 -0700 | [diff] [blame] | 32 | |
Josh Gao | c5e99e0 | 2019-07-08 17:37:23 -0700 | [diff] [blame] | 33 | #include <android-base/thread_annotations.h> |
| 34 | |
Josh Gao | d09ba31 | 2018-05-14 13:42:49 -0700 | [diff] [blame] | 35 | #include "adb_unique_fd.h" |
| 36 | |
Josh Gao | 06d4e74 | 2019-01-31 15:51:52 -0800 | [diff] [blame] | 37 | // Events that may be observed |
| 38 | #define FDE_READ 0x0001 |
| 39 | #define FDE_WRITE 0x0002 |
| 40 | #define FDE_ERROR 0x0004 |
| 41 | #define FDE_TIMEOUT 0x0008 |
David 'Digit' Turner | b1c2c95 | 2009-05-18 17:07:46 +0200 | [diff] [blame] | 42 | |
Josh Gao | 4729f46 | 2019-07-11 16:35:37 -0700 | [diff] [blame] | 43 | struct fdevent; |
Josh Gao | 9e4aa3a | 2019-07-08 15:23:17 -0700 | [diff] [blame] | 44 | |
David 'Digit' Turner | b1c2c95 | 2009-05-18 17:07:46 +0200 | [diff] [blame] | 45 | typedef void (*fd_func)(int fd, unsigned events, void *userdata); |
Josh Gao | 87a2db6 | 2019-01-25 15:30:25 -0800 | [diff] [blame] | 46 | typedef void (*fd_func2)(struct fdevent* fde, unsigned events, void* userdata); |
David 'Digit' Turner | b1c2c95 | 2009-05-18 17:07:46 +0200 | [diff] [blame] | 47 | |
Josh Gao | 4729f46 | 2019-07-11 16:35:37 -0700 | [diff] [blame] | 48 | void invoke_fde(struct fdevent* fde, unsigned events); |
Josh Gao | 9e4aa3a | 2019-07-08 15:23:17 -0700 | [diff] [blame] | 49 | std::string dump_fde(const fdevent* fde); |
Josh Gao | c081050 | 2019-06-28 16:34:36 -0700 | [diff] [blame] | 50 | |
Josh Gao | 4729f46 | 2019-07-11 16:35:37 -0700 | [diff] [blame] | 51 | struct fdevent_event { |
| 52 | fdevent* fde; |
| 53 | unsigned events; |
Matt Lee | c7bb781 | 2023-05-16 21:02:04 +0000 | [diff] [blame] | 54 | fdevent_event(fdevent* pfde, unsigned ev) : fde(pfde), events(ev) {} |
Josh Gao | 4729f46 | 2019-07-11 16:35:37 -0700 | [diff] [blame] | 55 | }; |
| 56 | |
Yurii Zubrytskyi | 22863bd | 2020-03-18 22:29:10 -0700 | [diff] [blame] | 57 | struct fdevent final { |
| 58 | uint64_t id; |
| 59 | |
| 60 | unique_fd fd; |
Yurii Zubrytskyi | 22863bd | 2020-03-18 22:29:10 -0700 | [diff] [blame] | 61 | |
| 62 | uint16_t state = 0; |
| 63 | std::optional<std::chrono::milliseconds> timeout; |
| 64 | std::chrono::steady_clock::time_point last_active; |
| 65 | |
| 66 | std::variant<fd_func, fd_func2> func; |
| 67 | void* arg = nullptr; |
| 68 | }; |
| 69 | |
Josh Gao | c081050 | 2019-06-28 16:34:36 -0700 | [diff] [blame] | 70 | struct fdevent_context { |
Josh Gao | c5e99e0 | 2019-07-08 17:37:23 -0700 | [diff] [blame] | 71 | public: |
Josh Gao | c081050 | 2019-06-28 16:34:36 -0700 | [diff] [blame] | 72 | virtual ~fdevent_context() = default; |
| 73 | |
| 74 | // Allocate and initialize a new fdevent object. |
Josh Gao | e10e56b | 2019-07-08 18:16:19 -0700 | [diff] [blame] | 75 | fdevent* Create(unique_fd fd, std::variant<fd_func, fd_func2> func, void* arg); |
Josh Gao | c081050 | 2019-06-28 16:34:36 -0700 | [diff] [blame] | 76 | |
| 77 | // Deallocate an fdevent object, returning the file descriptor that was owned by it. |
Josh Gao | dd71642 | 2019-07-11 13:47:44 -0700 | [diff] [blame] | 78 | // Note that this calls Set, which is a virtual method, so destructors that call this must be |
| 79 | // final. |
Josh Gao | e10e56b | 2019-07-08 18:16:19 -0700 | [diff] [blame] | 80 | unique_fd Destroy(fdevent* fde); |
Josh Gao | c081050 | 2019-06-28 16:34:36 -0700 | [diff] [blame] | 81 | |
Josh Gao | e10e56b | 2019-07-08 18:16:19 -0700 | [diff] [blame] | 82 | protected: |
Elliott Hughes | f24aebf | 2020-05-12 16:17:11 -0700 | [diff] [blame] | 83 | virtual void Register(fdevent*) = 0; |
| 84 | virtual void Unregister(fdevent*) = 0; |
Josh Gao | e10e56b | 2019-07-08 18:16:19 -0700 | [diff] [blame] | 85 | |
| 86 | public: |
Josh Gao | c081050 | 2019-06-28 16:34:36 -0700 | [diff] [blame] | 87 | // Change which events should cause notifications. |
| 88 | virtual void Set(fdevent* fde, unsigned events) = 0; |
Josh Gao | ad93dc5 | 2019-07-08 18:17:41 -0700 | [diff] [blame] | 89 | void Add(fdevent* fde, unsigned events); |
| 90 | void Del(fdevent* fde, unsigned events); |
Josh Gao | c081050 | 2019-06-28 16:34:36 -0700 | [diff] [blame] | 91 | |
| 92 | // Set a timeout on an fdevent. |
| 93 | // If no events are triggered by the timeout, an FDE_TIMEOUT will be generated. |
| 94 | // Note timeouts are not defused automatically; if a timeout is set on an fdevent, it will |
| 95 | // trigger repeatedly every |timeout| ms. |
Josh Gao | ad93dc5 | 2019-07-08 18:17:41 -0700 | [diff] [blame] | 96 | void SetTimeout(fdevent* fde, std::optional<std::chrono::milliseconds> timeout); |
Josh Gao | c081050 | 2019-06-28 16:34:36 -0700 | [diff] [blame] | 97 | |
Josh Gao | 4729f46 | 2019-07-11 16:35:37 -0700 | [diff] [blame] | 98 | protected: |
| 99 | std::optional<std::chrono::milliseconds> CalculatePollDuration(); |
| 100 | void HandleEvents(const std::vector<fdevent_event>& events); |
| 101 | |
| 102 | private: |
| 103 | // Run all pending functions enqueued via Run(). |
| 104 | void FlushRunQueue() EXCLUDES(run_queue_mutex_); |
| 105 | |
| 106 | public: |
Josh Gao | df92f8a | 2019-07-08 18:08:06 -0700 | [diff] [blame] | 107 | // Loop until TerminateLoop is called, handling events. |
| 108 | // Implementations should call FlushRunQueue on every iteration, and check the value of |
| 109 | // terminate_loop_ to determine whether to stop. |
Josh Gao | c081050 | 2019-06-28 16:34:36 -0700 | [diff] [blame] | 110 | virtual void Loop() = 0; |
| 111 | |
Shaju Mathew | 705fa1c | 2022-12-31 19:36:54 -0800 | [diff] [blame] | 112 | // Assert that the caller is executing in the context of the execution |
| 113 | // thread that invoked Loop(). |
| 114 | void CheckLooperThread() const; |
Josh Gao | c081050 | 2019-06-28 16:34:36 -0700 | [diff] [blame] | 115 | |
Shaju Mathew | 705fa1c | 2022-12-31 19:36:54 -0800 | [diff] [blame] | 116 | // Queue an operation to be run on the looper thread. |
Josh Gao | c5e99e0 | 2019-07-08 17:37:23 -0700 | [diff] [blame] | 117 | void Run(std::function<void()> fn); |
Josh Gao | c081050 | 2019-06-28 16:34:36 -0700 | [diff] [blame] | 118 | |
| 119 | // Test-only functionality: |
Josh Gao | df92f8a | 2019-07-08 18:08:06 -0700 | [diff] [blame] | 120 | void TerminateLoop(); |
Josh Gao | c081050 | 2019-06-28 16:34:36 -0700 | [diff] [blame] | 121 | virtual size_t InstalledCount() = 0; |
Josh Gao | c5e99e0 | 2019-07-08 17:37:23 -0700 | [diff] [blame] | 122 | |
| 123 | protected: |
| 124 | // Interrupt the run loop. |
| 125 | virtual void Interrupt() = 0; |
| 126 | |
Shaju Mathew | 705fa1c | 2022-12-31 19:36:54 -0800 | [diff] [blame] | 127 | std::optional<uint64_t> looper_thread_id_ = std::nullopt; |
Josh Gao | df92f8a | 2019-07-08 18:08:06 -0700 | [diff] [blame] | 128 | std::atomic<bool> terminate_loop_ = false; |
Josh Gao | 83e435a | 2019-07-08 18:05:16 -0700 | [diff] [blame] | 129 | |
Colin Cross | 78d34d8 | 2023-05-16 22:10:26 +0000 | [diff] [blame] | 130 | std::map<int, fdevent> installed_fdevents_; |
Josh Gao | 4729f46 | 2019-07-11 16:35:37 -0700 | [diff] [blame] | 131 | |
Josh Gao | c5e99e0 | 2019-07-08 17:37:23 -0700 | [diff] [blame] | 132 | private: |
Josh Gao | e10e56b | 2019-07-08 18:16:19 -0700 | [diff] [blame] | 133 | uint64_t fdevent_id_ = 0; |
Josh Gao | c5e99e0 | 2019-07-08 17:37:23 -0700 | [diff] [blame] | 134 | std::mutex run_queue_mutex_; |
| 135 | std::deque<std::function<void()>> run_queue_ GUARDED_BY(run_queue_mutex_); |
Colin Cross | 9b24532 | 2023-05-16 22:10:31 +0000 | [diff] [blame] | 136 | |
| 137 | std::set<fdevent*> fdevent_set_; |
Josh Gao | c081050 | 2019-06-28 16:34:36 -0700 | [diff] [blame] | 138 | }; |
| 139 | |
Josh Gao | c081050 | 2019-06-28 16:34:36 -0700 | [diff] [blame] | 140 | // Backwards compatibility shims that forward to the global fdevent_context. |
| 141 | fdevent* fdevent_create(int fd, fd_func func, void* arg); |
Josh Gao | 87a2db6 | 2019-01-25 15:30:25 -0800 | [diff] [blame] | 142 | fdevent* fdevent_create(int fd, fd_func2 func, void* arg); |
David 'Digit' Turner | b1c2c95 | 2009-05-18 17:07:46 +0200 | [diff] [blame] | 143 | |
Josh Gao | ed15cac | 2018-09-20 17:38:55 -0700 | [diff] [blame] | 144 | unique_fd fdevent_release(fdevent* fde); |
Josh Gao | c081050 | 2019-06-28 16:34:36 -0700 | [diff] [blame] | 145 | void fdevent_destroy(fdevent* fde); |
Josh Gao | ed15cac | 2018-09-20 17:38:55 -0700 | [diff] [blame] | 146 | |
David 'Digit' Turner | b1c2c95 | 2009-05-18 17:07:46 +0200 | [diff] [blame] | 147 | void fdevent_set(fdevent *fde, unsigned events); |
| 148 | void fdevent_add(fdevent *fde, unsigned events); |
| 149 | void fdevent_del(fdevent *fde, unsigned events); |
Josh Gao | 06d4e74 | 2019-01-31 15:51:52 -0800 | [diff] [blame] | 150 | 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] | 151 | void fdevent_loop(); |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 152 | |
Shaju Mathew | 705fa1c | 2022-12-31 19:36:54 -0800 | [diff] [blame] | 153 | // Delegates to the member function that checks for the initialization |
| 154 | // of Loop() so that fdevent_context requests can be serially processed |
| 155 | // by the global instance robustly. |
| 156 | void fdevent_check_looper(); |
| 157 | |
| 158 | // Queue an operation to run on the looper event thread. |
| 159 | void fdevent_run_on_looper(std::function<void()> fn); |
Josh Gao | 764f8c5 | 2017-05-03 14:10:39 -0700 | [diff] [blame] | 160 | |
Josh Gao | 511763b | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 161 | // The following functions are used only for tests. |
| 162 | void fdevent_terminate_loop(); |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 163 | size_t fdevent_installed_count(); |
Yabin Cui | 2ce9d56 | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 164 | void fdevent_reset(); |
David 'Digit' Turner | b1c2c95 | 2009-05-18 17:07:46 +0200 | [diff] [blame] | 165 | |
David 'Digit' Turner | b1c2c95 | 2009-05-18 17:07:46 +0200 | [diff] [blame] | 166 | #endif |