The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [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 | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 20 | #include <stddef.h> |
Josh Gao | 1a90118 | 2019-01-31 15:51:52 -0800 | [diff] [blame] | 21 | #include <stdint.h> |
David 'Digit' Turner | f6330a2 | 2009-05-18 17:36:28 +0200 | [diff] [blame] | 22 | |
Anatol Pomazau | 7b2ff6d | 2019-08-08 14:45:12 -0700 | [diff] [blame] | 23 | #include <atomic> |
Josh Gao | 1a90118 | 2019-01-31 15:51:52 -0800 | [diff] [blame] | 24 | #include <chrono> |
Josh Gao | 95eef6b | 2019-07-08 17:37:23 -0700 | [diff] [blame] | 25 | #include <deque> |
Josh Gao | 4c93639 | 2017-05-03 14:10:39 -0700 | [diff] [blame] | 26 | #include <functional> |
Josh Gao | 95eef6b | 2019-07-08 17:37:23 -0700 | [diff] [blame] | 27 | #include <mutex> |
Josh Gao | 1a90118 | 2019-01-31 15:51:52 -0800 | [diff] [blame] | 28 | #include <optional> |
Josh Gao | e22cb9f | 2019-07-11 16:35:37 -0700 | [diff] [blame] | 29 | #include <unordered_map> |
Josh Gao | c162c71 | 2019-01-25 15:30:25 -0800 | [diff] [blame] | 30 | #include <variant> |
Josh Gao | 4c93639 | 2017-05-03 14:10:39 -0700 | [diff] [blame] | 31 | |
Josh Gao | 95eef6b | 2019-07-08 17:37:23 -0700 | [diff] [blame] | 32 | #include <android-base/thread_annotations.h> |
| 33 | |
Josh Gao | 3b37fa2 | 2018-05-14 13:42:49 -0700 | [diff] [blame] | 34 | #include "adb_unique_fd.h" |
| 35 | |
Josh Gao | 1a90118 | 2019-01-31 15:51:52 -0800 | [diff] [blame] | 36 | // Events that may be observed |
| 37 | #define FDE_READ 0x0001 |
| 38 | #define FDE_WRITE 0x0002 |
| 39 | #define FDE_ERROR 0x0004 |
| 40 | #define FDE_TIMEOUT 0x0008 |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 41 | |
Josh Gao | e22cb9f | 2019-07-11 16:35:37 -0700 | [diff] [blame] | 42 | struct fdevent; |
Josh Gao | 95068bb | 2019-07-08 15:23:17 -0700 | [diff] [blame] | 43 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 44 | typedef void (*fd_func)(int fd, unsigned events, void *userdata); |
Josh Gao | c162c71 | 2019-01-25 15:30:25 -0800 | [diff] [blame] | 45 | typedef void (*fd_func2)(struct fdevent* fde, unsigned events, void* userdata); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 46 | |
Josh Gao | e22cb9f | 2019-07-11 16:35:37 -0700 | [diff] [blame] | 47 | void invoke_fde(struct fdevent* fde, unsigned events); |
Josh Gao | 95068bb | 2019-07-08 15:23:17 -0700 | [diff] [blame] | 48 | std::string dump_fde(const fdevent* fde); |
Josh Gao | c2cf121 | 2019-06-28 16:34:36 -0700 | [diff] [blame] | 49 | |
Josh Gao | e22cb9f | 2019-07-11 16:35:37 -0700 | [diff] [blame] | 50 | struct fdevent_event { |
| 51 | fdevent* fde; |
| 52 | unsigned events; |
| 53 | }; |
| 54 | |
Josh Gao | c2cf121 | 2019-06-28 16:34:36 -0700 | [diff] [blame] | 55 | struct fdevent_context { |
Josh Gao | 95eef6b | 2019-07-08 17:37:23 -0700 | [diff] [blame] | 56 | public: |
Josh Gao | c2cf121 | 2019-06-28 16:34:36 -0700 | [diff] [blame] | 57 | virtual ~fdevent_context() = default; |
| 58 | |
| 59 | // Allocate and initialize a new fdevent object. |
Josh Gao | 33944a2 | 2019-07-08 18:16:19 -0700 | [diff] [blame] | 60 | fdevent* Create(unique_fd fd, std::variant<fd_func, fd_func2> func, void* arg); |
Josh Gao | c2cf121 | 2019-06-28 16:34:36 -0700 | [diff] [blame] | 61 | |
| 62 | // Deallocate an fdevent object, returning the file descriptor that was owned by it. |
Josh Gao | b43ad44 | 2019-07-11 13:47:44 -0700 | [diff] [blame] | 63 | // Note that this calls Set, which is a virtual method, so destructors that call this must be |
| 64 | // final. |
Josh Gao | 33944a2 | 2019-07-08 18:16:19 -0700 | [diff] [blame] | 65 | unique_fd Destroy(fdevent* fde); |
Josh Gao | c2cf121 | 2019-06-28 16:34:36 -0700 | [diff] [blame] | 66 | |
Josh Gao | 33944a2 | 2019-07-08 18:16:19 -0700 | [diff] [blame] | 67 | protected: |
Josh Gao | e22cb9f | 2019-07-11 16:35:37 -0700 | [diff] [blame] | 68 | virtual void Register(fdevent*) {} |
| 69 | virtual void Unregister(fdevent*) {} |
Josh Gao | 33944a2 | 2019-07-08 18:16:19 -0700 | [diff] [blame] | 70 | |
| 71 | public: |
Josh Gao | c2cf121 | 2019-06-28 16:34:36 -0700 | [diff] [blame] | 72 | // Change which events should cause notifications. |
| 73 | virtual void Set(fdevent* fde, unsigned events) = 0; |
Josh Gao | 35b2936 | 2019-07-08 18:17:41 -0700 | [diff] [blame] | 74 | void Add(fdevent* fde, unsigned events); |
| 75 | void Del(fdevent* fde, unsigned events); |
Josh Gao | c2cf121 | 2019-06-28 16:34:36 -0700 | [diff] [blame] | 76 | |
| 77 | // Set a timeout on an fdevent. |
| 78 | // If no events are triggered by the timeout, an FDE_TIMEOUT will be generated. |
| 79 | // Note timeouts are not defused automatically; if a timeout is set on an fdevent, it will |
| 80 | // trigger repeatedly every |timeout| ms. |
Josh Gao | 35b2936 | 2019-07-08 18:17:41 -0700 | [diff] [blame] | 81 | void SetTimeout(fdevent* fde, std::optional<std::chrono::milliseconds> timeout); |
Josh Gao | c2cf121 | 2019-06-28 16:34:36 -0700 | [diff] [blame] | 82 | |
Josh Gao | e22cb9f | 2019-07-11 16:35:37 -0700 | [diff] [blame] | 83 | protected: |
| 84 | std::optional<std::chrono::milliseconds> CalculatePollDuration(); |
| 85 | void HandleEvents(const std::vector<fdevent_event>& events); |
| 86 | |
| 87 | private: |
| 88 | // Run all pending functions enqueued via Run(). |
| 89 | void FlushRunQueue() EXCLUDES(run_queue_mutex_); |
| 90 | |
| 91 | public: |
Josh Gao | ebaa348 | 2019-07-08 18:08:06 -0700 | [diff] [blame] | 92 | // Loop until TerminateLoop is called, handling events. |
| 93 | // Implementations should call FlushRunQueue on every iteration, and check the value of |
| 94 | // terminate_loop_ to determine whether to stop. |
Josh Gao | c2cf121 | 2019-06-28 16:34:36 -0700 | [diff] [blame] | 95 | virtual void Loop() = 0; |
| 96 | |
Josh Gao | 2c95bf7 | 2019-07-08 18:05:16 -0700 | [diff] [blame] | 97 | // Assert that the caller is either running on the context's main thread, or that there is no |
| 98 | // active main thread. |
| 99 | void CheckMainThread(); |
Josh Gao | c2cf121 | 2019-06-28 16:34:36 -0700 | [diff] [blame] | 100 | |
| 101 | // Queue an operation to be run on the main thread. |
Josh Gao | 95eef6b | 2019-07-08 17:37:23 -0700 | [diff] [blame] | 102 | void Run(std::function<void()> fn); |
Josh Gao | c2cf121 | 2019-06-28 16:34:36 -0700 | [diff] [blame] | 103 | |
| 104 | // Test-only functionality: |
Josh Gao | ebaa348 | 2019-07-08 18:08:06 -0700 | [diff] [blame] | 105 | void TerminateLoop(); |
Josh Gao | c2cf121 | 2019-06-28 16:34:36 -0700 | [diff] [blame] | 106 | virtual size_t InstalledCount() = 0; |
Josh Gao | 95eef6b | 2019-07-08 17:37:23 -0700 | [diff] [blame] | 107 | |
| 108 | protected: |
| 109 | // Interrupt the run loop. |
| 110 | virtual void Interrupt() = 0; |
| 111 | |
Josh Gao | 2c95bf7 | 2019-07-08 18:05:16 -0700 | [diff] [blame] | 112 | std::optional<uint64_t> main_thread_id_ = std::nullopt; |
Josh Gao | ebaa348 | 2019-07-08 18:08:06 -0700 | [diff] [blame] | 113 | std::atomic<bool> terminate_loop_ = false; |
Josh Gao | 2c95bf7 | 2019-07-08 18:05:16 -0700 | [diff] [blame] | 114 | |
Josh Gao | e22cb9f | 2019-07-11 16:35:37 -0700 | [diff] [blame] | 115 | protected: |
| 116 | std::unordered_map<int, fdevent*> installed_fdevents_; |
| 117 | |
Josh Gao | 95eef6b | 2019-07-08 17:37:23 -0700 | [diff] [blame] | 118 | private: |
Josh Gao | 33944a2 | 2019-07-08 18:16:19 -0700 | [diff] [blame] | 119 | uint64_t fdevent_id_ = 0; |
Josh Gao | 95eef6b | 2019-07-08 17:37:23 -0700 | [diff] [blame] | 120 | std::mutex run_queue_mutex_; |
| 121 | std::deque<std::function<void()>> run_queue_ GUARDED_BY(run_queue_mutex_); |
Josh Gao | c2cf121 | 2019-06-28 16:34:36 -0700 | [diff] [blame] | 122 | }; |
| 123 | |
Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 124 | struct fdevent { |
Josh Gao | ded557f | 2018-06-18 14:55:27 -0700 | [diff] [blame] | 125 | uint64_t id; |
| 126 | |
Josh Gao | 3b37fa2 | 2018-05-14 13:42:49 -0700 | [diff] [blame] | 127 | unique_fd fd; |
Josh Gao | 71f775a | 2018-05-14 11:14:33 -0700 | [diff] [blame] | 128 | int force_eof = 0; |
Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 129 | |
Josh Gao | 71f775a | 2018-05-14 11:14:33 -0700 | [diff] [blame] | 130 | uint16_t state = 0; |
Josh Gao | 1a90118 | 2019-01-31 15:51:52 -0800 | [diff] [blame] | 131 | std::optional<std::chrono::milliseconds> timeout; |
| 132 | std::chrono::steady_clock::time_point last_active; |
Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 133 | |
Josh Gao | c162c71 | 2019-01-25 15:30:25 -0800 | [diff] [blame] | 134 | std::variant<fd_func, fd_func2> func; |
Josh Gao | 71f775a | 2018-05-14 11:14:33 -0700 | [diff] [blame] | 135 | void* arg = nullptr; |
Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 136 | }; |
| 137 | |
Josh Gao | c2cf121 | 2019-06-28 16:34:36 -0700 | [diff] [blame] | 138 | // Backwards compatibility shims that forward to the global fdevent_context. |
| 139 | fdevent* fdevent_create(int fd, fd_func func, void* arg); |
Josh Gao | c162c71 | 2019-01-25 15:30:25 -0800 | [diff] [blame] | 140 | fdevent* fdevent_create(int fd, fd_func2 func, void* arg); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 141 | |
Josh Gao | 2f6c2fa | 2018-09-20 17:38:55 -0700 | [diff] [blame] | 142 | unique_fd fdevent_release(fdevent* fde); |
Josh Gao | c2cf121 | 2019-06-28 16:34:36 -0700 | [diff] [blame] | 143 | void fdevent_destroy(fdevent* fde); |
Josh Gao | 2f6c2fa | 2018-09-20 17:38:55 -0700 | [diff] [blame] | 144 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 145 | void fdevent_set(fdevent *fde, unsigned events); |
| 146 | void fdevent_add(fdevent *fde, unsigned events); |
| 147 | void fdevent_del(fdevent *fde, unsigned events); |
Josh Gao | 1a90118 | 2019-01-31 15:51:52 -0800 | [diff] [blame] | 148 | void fdevent_set_timeout(fdevent* fde, std::optional<std::chrono::milliseconds> timeout); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 149 | void fdevent_loop(); |
Yabin Cui | b5e1141 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 150 | void check_main_thread(); |
| 151 | |
Josh Gao | 4c93639 | 2017-05-03 14:10:39 -0700 | [diff] [blame] | 152 | // Queue an operation to run on the main thread. |
| 153 | void fdevent_run_on_main_thread(std::function<void()> fn); |
| 154 | |
Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 155 | // The following functions are used only for tests. |
| 156 | void fdevent_terminate_loop(); |
Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 157 | size_t fdevent_installed_count(); |
Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 158 | void fdevent_reset(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 159 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 160 | #endif |