blob: bb3af746fc7e6f24a42d6d9ef2c4714c7411c199 [file] [log] [blame]
David 'Digit' Turnerb1c2c952009-05-18 17:07:46 +02001/*
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 Cui2ce9d562015-09-15 16:27:09 -070020#include <stddef.h>
Josh Gao06d4e742019-01-31 15:51:52 -080021#include <stdint.h>
David 'Digit' Turner1f1efb52009-05-18 17:36:28 +020022
Anatol Pomazau324cb522019-08-08 14:45:12 -070023#include <atomic>
Josh Gao06d4e742019-01-31 15:51:52 -080024#include <chrono>
Josh Gaoc5e99e02019-07-08 17:37:23 -070025#include <deque>
Josh Gao764f8c52017-05-03 14:10:39 -070026#include <functional>
Josh Gaoc5e99e02019-07-08 17:37:23 -070027#include <mutex>
Josh Gao06d4e742019-01-31 15:51:52 -080028#include <optional>
Josh Gao4729f462019-07-11 16:35:37 -070029#include <unordered_map>
Josh Gao87a2db62019-01-25 15:30:25 -080030#include <variant>
Josh Gao764f8c52017-05-03 14:10:39 -070031
Josh Gaoc5e99e02019-07-08 17:37:23 -070032#include <android-base/thread_annotations.h>
33
Josh Gaod09ba312018-05-14 13:42:49 -070034#include "adb_unique_fd.h"
35
Josh Gao06d4e742019-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
David 'Digit' Turnerb1c2c952009-05-18 17:07:46 +020041
Josh Gao4729f462019-07-11 16:35:37 -070042struct fdevent;
Josh Gao9e4aa3a2019-07-08 15:23:17 -070043
David 'Digit' Turnerb1c2c952009-05-18 17:07:46 +020044typedef void (*fd_func)(int fd, unsigned events, void *userdata);
Josh Gao87a2db62019-01-25 15:30:25 -080045typedef void (*fd_func2)(struct fdevent* fde, unsigned events, void* userdata);
David 'Digit' Turnerb1c2c952009-05-18 17:07:46 +020046
Josh Gao4729f462019-07-11 16:35:37 -070047void invoke_fde(struct fdevent* fde, unsigned events);
Josh Gao9e4aa3a2019-07-08 15:23:17 -070048std::string dump_fde(const fdevent* fde);
Josh Gaoc0810502019-06-28 16:34:36 -070049
Josh Gao4729f462019-07-11 16:35:37 -070050struct fdevent_event {
51 fdevent* fde;
52 unsigned events;
53};
54
Yurii Zubrytskyi22863bd2020-03-18 22:29:10 -070055struct fdevent final {
56 uint64_t id;
57
58 unique_fd fd;
59 int force_eof = 0;
60
61 uint16_t state = 0;
62 std::optional<std::chrono::milliseconds> timeout;
63 std::chrono::steady_clock::time_point last_active;
64
65 std::variant<fd_func, fd_func2> func;
66 void* arg = nullptr;
67};
68
Josh Gaoc0810502019-06-28 16:34:36 -070069struct fdevent_context {
Josh Gaoc5e99e02019-07-08 17:37:23 -070070 public:
Josh Gaoc0810502019-06-28 16:34:36 -070071 virtual ~fdevent_context() = default;
72
73 // Allocate and initialize a new fdevent object.
Josh Gaoe10e56b2019-07-08 18:16:19 -070074 fdevent* Create(unique_fd fd, std::variant<fd_func, fd_func2> func, void* arg);
Josh Gaoc0810502019-06-28 16:34:36 -070075
76 // Deallocate an fdevent object, returning the file descriptor that was owned by it.
Josh Gaodd716422019-07-11 13:47:44 -070077 // Note that this calls Set, which is a virtual method, so destructors that call this must be
78 // final.
Josh Gaoe10e56b2019-07-08 18:16:19 -070079 unique_fd Destroy(fdevent* fde);
Josh Gaoc0810502019-06-28 16:34:36 -070080
Josh Gaoe10e56b2019-07-08 18:16:19 -070081 protected:
Elliott Hughesf24aebf2020-05-12 16:17:11 -070082 virtual void Register(fdevent*) = 0;
83 virtual void Unregister(fdevent*) = 0;
Josh Gaoe10e56b2019-07-08 18:16:19 -070084
85 public:
Josh Gaoc0810502019-06-28 16:34:36 -070086 // Change which events should cause notifications.
87 virtual void Set(fdevent* fde, unsigned events) = 0;
Josh Gaoad93dc52019-07-08 18:17:41 -070088 void Add(fdevent* fde, unsigned events);
89 void Del(fdevent* fde, unsigned events);
Josh Gaoc0810502019-06-28 16:34:36 -070090
91 // Set a timeout on an fdevent.
92 // If no events are triggered by the timeout, an FDE_TIMEOUT will be generated.
93 // Note timeouts are not defused automatically; if a timeout is set on an fdevent, it will
94 // trigger repeatedly every |timeout| ms.
Josh Gaoad93dc52019-07-08 18:17:41 -070095 void SetTimeout(fdevent* fde, std::optional<std::chrono::milliseconds> timeout);
Josh Gaoc0810502019-06-28 16:34:36 -070096
Josh Gao4729f462019-07-11 16:35:37 -070097 protected:
98 std::optional<std::chrono::milliseconds> CalculatePollDuration();
99 void HandleEvents(const std::vector<fdevent_event>& events);
100
101 private:
102 // Run all pending functions enqueued via Run().
103 void FlushRunQueue() EXCLUDES(run_queue_mutex_);
104
105 public:
Josh Gaodf92f8a2019-07-08 18:08:06 -0700106 // Loop until TerminateLoop is called, handling events.
107 // Implementations should call FlushRunQueue on every iteration, and check the value of
108 // terminate_loop_ to determine whether to stop.
Josh Gaoc0810502019-06-28 16:34:36 -0700109 virtual void Loop() = 0;
110
Josh Gao83e435a2019-07-08 18:05:16 -0700111 // Assert that the caller is either running on the context's main thread, or that there is no
112 // active main thread.
113 void CheckMainThread();
Josh Gaoc0810502019-06-28 16:34:36 -0700114
115 // Queue an operation to be run on the main thread.
Josh Gaoc5e99e02019-07-08 17:37:23 -0700116 void Run(std::function<void()> fn);
Josh Gaoc0810502019-06-28 16:34:36 -0700117
118 // Test-only functionality:
Josh Gaodf92f8a2019-07-08 18:08:06 -0700119 void TerminateLoop();
Josh Gaoc0810502019-06-28 16:34:36 -0700120 virtual size_t InstalledCount() = 0;
Josh Gaoc5e99e02019-07-08 17:37:23 -0700121
122 protected:
123 // Interrupt the run loop.
124 virtual void Interrupt() = 0;
125
Josh Gao83e435a2019-07-08 18:05:16 -0700126 std::optional<uint64_t> main_thread_id_ = std::nullopt;
Josh Gaodf92f8a2019-07-08 18:08:06 -0700127 std::atomic<bool> terminate_loop_ = false;
Josh Gao83e435a2019-07-08 18:05:16 -0700128
Josh Gao4729f462019-07-11 16:35:37 -0700129 protected:
Yurii Zubrytskyi22863bd2020-03-18 22:29:10 -0700130 std::unordered_map<int, fdevent> installed_fdevents_;
Josh Gao4729f462019-07-11 16:35:37 -0700131
Josh Gaoc5e99e02019-07-08 17:37:23 -0700132 private:
Josh Gaoe10e56b2019-07-08 18:16:19 -0700133 uint64_t fdevent_id_ = 0;
Josh Gaoc5e99e02019-07-08 17:37:23 -0700134 std::mutex run_queue_mutex_;
135 std::deque<std::function<void()>> run_queue_ GUARDED_BY(run_queue_mutex_);
Josh Gaoc0810502019-06-28 16:34:36 -0700136};
137
Josh Gaoc0810502019-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 Gao87a2db62019-01-25 15:30:25 -0800140fdevent* fdevent_create(int fd, fd_func2 func, void* arg);
David 'Digit' Turnerb1c2c952009-05-18 17:07:46 +0200141
Josh Gaoed15cac2018-09-20 17:38:55 -0700142unique_fd fdevent_release(fdevent* fde);
Josh Gaoc0810502019-06-28 16:34:36 -0700143void fdevent_destroy(fdevent* fde);
Josh Gaoed15cac2018-09-20 17:38:55 -0700144
David 'Digit' Turnerb1c2c952009-05-18 17:07:46 +0200145void fdevent_set(fdevent *fde, unsigned events);
146void fdevent_add(fdevent *fde, unsigned events);
147void fdevent_del(fdevent *fde, unsigned events);
Josh Gao06d4e742019-01-31 15:51:52 -0800148void fdevent_set_timeout(fdevent* fde, std::optional<std::chrono::milliseconds> timeout);
David 'Digit' Turnerb1c2c952009-05-18 17:07:46 +0200149void fdevent_loop();
Yabin Cui3cf1b362017-03-10 16:01:01 -0800150void check_main_thread();
151
Josh Gao764f8c52017-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 Gao511763b2016-02-10 14:49:00 -0800155// The following functions are used only for tests.
156void fdevent_terminate_loop();
Yabin Cui2ce9d562015-09-15 16:27:09 -0700157size_t fdevent_installed_count();
Yabin Cui2ce9d562015-09-15 16:27:09 -0700158void fdevent_reset();
David 'Digit' Turnerb1c2c952009-05-18 17:07:46 +0200159
David 'Digit' Turnerb1c2c952009-05-18 17:07:46 +0200160#endif