Josh Gao | 95068bb | 2019-07-08 15:23:17 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2006, Brian Swetland <swetland@frotz.net> |
| 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 | */ |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 16 | |
Yabin Cui | aed3c61 | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 17 | #define TRACE_TAG FDEVENT |
JP Abgrall | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 18 | |
Dan Albert | 3313426 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 19 | #include "sysdeps.h" |
Dan Albert | 3313426 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 20 | |
Josh Gao | ded557f | 2018-06-18 14:55:27 -0700 | [diff] [blame] | 21 | #include <inttypes.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 22 | |
Josh Gao | 2c95bf7 | 2019-07-08 18:05:16 -0700 | [diff] [blame^] | 23 | #include <android-base/logging.h> |
Elliott Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 24 | #include <android-base/stringprintf.h> |
Josh Gao | 2c95bf7 | 2019-07-08 18:05:16 -0700 | [diff] [blame^] | 25 | #include <android-base/threads.h> |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 26 | |
Josh Gao | 95068bb | 2019-07-08 15:23:17 -0700 | [diff] [blame] | 27 | #include "fdevent.h" |
| 28 | #include "fdevent_poll.h" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 29 | |
Josh Gao | 95068bb | 2019-07-08 15:23:17 -0700 | [diff] [blame] | 30 | std::string dump_fde(const fdevent* fde) { |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 31 | std::string state; |
| 32 | if (fde->state & FDE_ACTIVE) { |
| 33 | state += "A"; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 34 | } |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 35 | if (fde->state & FDE_PENDING) { |
| 36 | state += "P"; |
| 37 | } |
Yabin Cui | a108016 | 2015-09-04 16:19:56 -0700 | [diff] [blame] | 38 | if (fde->state & FDE_READ) { |
| 39 | state += "R"; |
| 40 | } |
| 41 | if (fde->state & FDE_WRITE) { |
| 42 | state += "W"; |
| 43 | } |
| 44 | if (fde->state & FDE_ERROR) { |
| 45 | state += "E"; |
| 46 | } |
Josh Gao | ded557f | 2018-06-18 14:55:27 -0700 | [diff] [blame] | 47 | return android::base::StringPrintf("(fdevent %" PRIu64 ": fd %d %s)", fde->id, fde->fd.get(), |
| 48 | state.c_str()); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 49 | } |
| 50 | |
Josh Gao | 2c95bf7 | 2019-07-08 18:05:16 -0700 | [diff] [blame^] | 51 | void fdevent_context::CheckMainThread() { |
| 52 | if (main_thread_id_) { |
| 53 | CHECK_EQ(*main_thread_id_, android::base::GetThreadId()); |
| 54 | } |
| 55 | } |
| 56 | |
Josh Gao | 95eef6b | 2019-07-08 17:37:23 -0700 | [diff] [blame] | 57 | void fdevent_context::Run(std::function<void()> fn) { |
| 58 | { |
| 59 | std::lock_guard<std::mutex> lock(run_queue_mutex_); |
| 60 | run_queue_.push_back(std::move(fn)); |
| 61 | } |
| 62 | |
| 63 | Interrupt(); |
| 64 | } |
| 65 | |
| 66 | void fdevent_context::FlushRunQueue() { |
| 67 | // We need to be careful around reentrancy here, since a function we call can queue up another |
| 68 | // function. |
| 69 | while (true) { |
| 70 | std::function<void()> fn; |
| 71 | { |
| 72 | std::lock_guard<std::mutex> lock(this->run_queue_mutex_); |
| 73 | if (this->run_queue_.empty()) { |
| 74 | break; |
| 75 | } |
| 76 | fn = this->run_queue_.front(); |
| 77 | this->run_queue_.pop_front(); |
| 78 | } |
| 79 | fn(); |
| 80 | } |
| 81 | } |
| 82 | |
Josh Gao | 7adca93 | 2019-07-08 17:31:47 -0700 | [diff] [blame] | 83 | static auto& g_ambient_fdevent_context = |
| 84 | *new std::unique_ptr<fdevent_context>(new fdevent_context_poll()); |
Josh Gao | c2cf121 | 2019-06-28 16:34:36 -0700 | [diff] [blame] | 85 | |
| 86 | static fdevent_context* fdevent_get_ambient() { |
Josh Gao | 7adca93 | 2019-07-08 17:31:47 -0700 | [diff] [blame] | 87 | return g_ambient_fdevent_context.get(); |
Josh Gao | c2cf121 | 2019-06-28 16:34:36 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Josh Gao | c2cf121 | 2019-06-28 16:34:36 -0700 | [diff] [blame] | 90 | fdevent* fdevent_create(int fd, fd_func func, void* arg) { |
| 91 | unique_fd ufd(fd); |
| 92 | return fdevent_get_ambient()->Create(std::move(ufd), func, arg); |
| 93 | } |
| 94 | |
| 95 | fdevent* fdevent_create(int fd, fd_func2 func, void* arg) { |
| 96 | unique_fd ufd(fd); |
| 97 | return fdevent_get_ambient()->Create(std::move(ufd), func, arg); |
| 98 | } |
| 99 | |
| 100 | unique_fd fdevent_release(fdevent* fde) { |
| 101 | return fdevent_get_ambient()->Destroy(fde); |
| 102 | } |
| 103 | |
| 104 | void fdevent_destroy(fdevent* fde) { |
| 105 | fdevent_get_ambient()->Destroy(fde); |
| 106 | } |
| 107 | |
| 108 | void fdevent_set(fdevent* fde, unsigned events) { |
| 109 | fdevent_get_ambient()->Set(fde, events); |
| 110 | } |
| 111 | |
| 112 | void fdevent_add(fdevent* fde, unsigned events) { |
| 113 | fdevent_get_ambient()->Add(fde, events); |
| 114 | } |
| 115 | |
| 116 | void fdevent_del(fdevent* fde, unsigned events) { |
| 117 | fdevent_get_ambient()->Del(fde, events); |
| 118 | } |
| 119 | |
| 120 | void fdevent_set_timeout(fdevent* fde, std::optional<std::chrono::milliseconds> timeout) { |
| 121 | fdevent_get_ambient()->SetTimeout(fde, timeout); |
| 122 | } |
| 123 | |
| 124 | void fdevent_run_on_main_thread(std::function<void()> fn) { |
| 125 | fdevent_get_ambient()->Run(std::move(fn)); |
| 126 | } |
| 127 | |
| 128 | void fdevent_loop() { |
| 129 | fdevent_get_ambient()->Loop(); |
| 130 | } |
| 131 | |
| 132 | void check_main_thread() { |
| 133 | fdevent_get_ambient()->CheckMainThread(); |
| 134 | } |
| 135 | |
Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 136 | void fdevent_terminate_loop() { |
Josh Gao | c2cf121 | 2019-06-28 16:34:36 -0700 | [diff] [blame] | 137 | fdevent_get_ambient()->TerminateLoop(); |
Josh Gao | 022d447 | 2016-02-10 14:49:00 -0800 | [diff] [blame] | 138 | } |
| 139 | |
Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 140 | size_t fdevent_installed_count() { |
Josh Gao | c2cf121 | 2019-06-28 16:34:36 -0700 | [diff] [blame] | 141 | return fdevent_get_ambient()->InstalledCount(); |
Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | void fdevent_reset() { |
Josh Gao | 7adca93 | 2019-07-08 17:31:47 -0700 | [diff] [blame] | 145 | g_ambient_fdevent_context.reset(new fdevent_context_poll()); |
Yabin Cui | c1b1f6f | 2015-09-15 16:27:09 -0700 | [diff] [blame] | 146 | } |