blob: 86814d7c5b653130ac79c10eb69b142741d3de54 [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/*
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 Cuic1b1f6f2015-09-15 16:27:09 -070020#include <stddef.h>
Josh Gao1a901182019-01-31 15:51:52 -080021#include <stdint.h>
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +020022
Anatol Pomazau7b2ff6d2019-08-08 14:45:12 -070023#include <atomic>
Josh Gao1a901182019-01-31 15:51:52 -080024#include <chrono>
Josh Gao95eef6b2019-07-08 17:37:23 -070025#include <deque>
Josh Gao4c936392017-05-03 14:10:39 -070026#include <functional>
Josh Gao95eef6b2019-07-08 17:37:23 -070027#include <mutex>
Josh Gao1a901182019-01-31 15:51:52 -080028#include <optional>
Josh Gaoe22cb9f2019-07-11 16:35:37 -070029#include <unordered_map>
Josh Gaoc162c712019-01-25 15:30:25 -080030#include <variant>
Josh Gao4c936392017-05-03 14:10:39 -070031
Josh Gao95eef6b2019-07-08 17:37:23 -070032#include <android-base/thread_annotations.h>
33
Josh Gao3b37fa22018-05-14 13:42:49 -070034#include "adb_unique_fd.h"
35
Josh Gao1a901182019-01-31 15:51:52 -080036// 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 Projectdd7bc332009-03-03 19:32:55 -080041
Josh Gaoe22cb9f2019-07-11 16:35:37 -070042struct fdevent;
Josh Gao95068bb2019-07-08 15:23:17 -070043
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080044typedef void (*fd_func)(int fd, unsigned events, void *userdata);
Josh Gaoc162c712019-01-25 15:30:25 -080045typedef void (*fd_func2)(struct fdevent* fde, unsigned events, void* userdata);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080046
Josh Gaoe22cb9f2019-07-11 16:35:37 -070047void invoke_fde(struct fdevent* fde, unsigned events);
Josh Gao95068bb2019-07-08 15:23:17 -070048std::string dump_fde(const fdevent* fde);
Josh Gaoc2cf1212019-06-28 16:34:36 -070049
Josh Gaoe22cb9f2019-07-11 16:35:37 -070050struct fdevent_event {
51 fdevent* fde;
52 unsigned events;
53};
54
Josh Gaoc2cf1212019-06-28 16:34:36 -070055struct fdevent_context {
Josh Gao95eef6b2019-07-08 17:37:23 -070056 public:
Josh Gaoc2cf1212019-06-28 16:34:36 -070057 virtual ~fdevent_context() = default;
58
59 // Allocate and initialize a new fdevent object.
Josh Gao33944a22019-07-08 18:16:19 -070060 fdevent* Create(unique_fd fd, std::variant<fd_func, fd_func2> func, void* arg);
Josh Gaoc2cf1212019-06-28 16:34:36 -070061
62 // Deallocate an fdevent object, returning the file descriptor that was owned by it.
Josh Gaob43ad442019-07-11 13:47:44 -070063 // Note that this calls Set, which is a virtual method, so destructors that call this must be
64 // final.
Josh Gao33944a22019-07-08 18:16:19 -070065 unique_fd Destroy(fdevent* fde);
Josh Gaoc2cf1212019-06-28 16:34:36 -070066
Josh Gao33944a22019-07-08 18:16:19 -070067 protected:
Josh Gaoe22cb9f2019-07-11 16:35:37 -070068 virtual void Register(fdevent*) {}
69 virtual void Unregister(fdevent*) {}
Josh Gao33944a22019-07-08 18:16:19 -070070
71 public:
Josh Gaoc2cf1212019-06-28 16:34:36 -070072 // Change which events should cause notifications.
73 virtual void Set(fdevent* fde, unsigned events) = 0;
Josh Gao35b29362019-07-08 18:17:41 -070074 void Add(fdevent* fde, unsigned events);
75 void Del(fdevent* fde, unsigned events);
Josh Gaoc2cf1212019-06-28 16:34:36 -070076
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 Gao35b29362019-07-08 18:17:41 -070081 void SetTimeout(fdevent* fde, std::optional<std::chrono::milliseconds> timeout);
Josh Gaoc2cf1212019-06-28 16:34:36 -070082
Josh Gaoe22cb9f2019-07-11 16:35:37 -070083 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 Gaoebaa3482019-07-08 18:08:06 -070092 // 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 Gaoc2cf1212019-06-28 16:34:36 -070095 virtual void Loop() = 0;
96
Josh Gao2c95bf72019-07-08 18:05:16 -070097 // 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 Gaoc2cf1212019-06-28 16:34:36 -0700100
101 // Queue an operation to be run on the main thread.
Josh Gao95eef6b2019-07-08 17:37:23 -0700102 void Run(std::function<void()> fn);
Josh Gaoc2cf1212019-06-28 16:34:36 -0700103
104 // Test-only functionality:
Josh Gaoebaa3482019-07-08 18:08:06 -0700105 void TerminateLoop();
Josh Gaoc2cf1212019-06-28 16:34:36 -0700106 virtual size_t InstalledCount() = 0;
Josh Gao95eef6b2019-07-08 17:37:23 -0700107
108 protected:
109 // Interrupt the run loop.
110 virtual void Interrupt() = 0;
111
Josh Gao2c95bf72019-07-08 18:05:16 -0700112 std::optional<uint64_t> main_thread_id_ = std::nullopt;
Josh Gaoebaa3482019-07-08 18:08:06 -0700113 std::atomic<bool> terminate_loop_ = false;
Josh Gao2c95bf72019-07-08 18:05:16 -0700114
Josh Gaoe22cb9f2019-07-11 16:35:37 -0700115 protected:
116 std::unordered_map<int, fdevent*> installed_fdevents_;
117
Josh Gao95eef6b2019-07-08 17:37:23 -0700118 private:
Josh Gao33944a22019-07-08 18:16:19 -0700119 uint64_t fdevent_id_ = 0;
Josh Gao95eef6b2019-07-08 17:37:23 -0700120 std::mutex run_queue_mutex_;
121 std::deque<std::function<void()>> run_queue_ GUARDED_BY(run_queue_mutex_);
Josh Gaoc2cf1212019-06-28 16:34:36 -0700122};
123
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700124struct fdevent {
Josh Gaoded557f2018-06-18 14:55:27 -0700125 uint64_t id;
126
Josh Gao3b37fa22018-05-14 13:42:49 -0700127 unique_fd fd;
Josh Gao71f775a2018-05-14 11:14:33 -0700128 int force_eof = 0;
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700129
Josh Gao71f775a2018-05-14 11:14:33 -0700130 uint16_t state = 0;
Josh Gao1a901182019-01-31 15:51:52 -0800131 std::optional<std::chrono::milliseconds> timeout;
132 std::chrono::steady_clock::time_point last_active;
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700133
Josh Gaoc162c712019-01-25 15:30:25 -0800134 std::variant<fd_func, fd_func2> func;
Josh Gao71f775a2018-05-14 11:14:33 -0700135 void* arg = nullptr;
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700136};
137
Josh Gaoc2cf1212019-06-28 16:34:36 -0700138// Backwards compatibility shims that forward to the global fdevent_context.
139fdevent* fdevent_create(int fd, fd_func func, void* arg);
Josh Gaoc162c712019-01-25 15:30:25 -0800140fdevent* fdevent_create(int fd, fd_func2 func, void* arg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800141
Josh Gao2f6c2fa2018-09-20 17:38:55 -0700142unique_fd fdevent_release(fdevent* fde);
Josh Gaoc2cf1212019-06-28 16:34:36 -0700143void fdevent_destroy(fdevent* fde);
Josh Gao2f6c2fa2018-09-20 17:38:55 -0700144
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800145void fdevent_set(fdevent *fde, unsigned events);
146void fdevent_add(fdevent *fde, unsigned events);
147void fdevent_del(fdevent *fde, unsigned events);
Josh Gao1a901182019-01-31 15:51:52 -0800148void fdevent_set_timeout(fdevent* fde, std::optional<std::chrono::milliseconds> timeout);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800149void fdevent_loop();
Yabin Cuib5e11412017-03-10 16:01:01 -0800150void check_main_thread();
151
Josh Gao4c936392017-05-03 14:10:39 -0700152// Queue an operation to run on the main thread.
153void fdevent_run_on_main_thread(std::function<void()> fn);
154
Josh Gao022d4472016-02-10 14:49:00 -0800155// The following functions are used only for tests.
156void fdevent_terminate_loop();
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700157size_t fdevent_installed_count();
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700158void fdevent_reset();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800159
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800160#endif