blob: c007e518b88b08920738bc19808a2378b77755d4 [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
Josh Gao1a901182019-01-31 15:51:52 -080023#include <chrono>
Josh Gao95eef6b2019-07-08 17:37:23 -070024#include <deque>
Josh Gao4c936392017-05-03 14:10:39 -070025#include <functional>
Josh Gao95eef6b2019-07-08 17:37:23 -070026#include <mutex>
Josh Gao1a901182019-01-31 15:51:52 -080027#include <optional>
Josh Gaoc162c712019-01-25 15:30:25 -080028#include <variant>
Josh Gao4c936392017-05-03 14:10:39 -070029
Josh Gao95eef6b2019-07-08 17:37:23 -070030#include <android-base/thread_annotations.h>
31
Josh Gao3b37fa22018-05-14 13:42:49 -070032#include "adb_unique_fd.h"
33
Josh Gao1a901182019-01-31 15:51:52 -080034// Events that may be observed
35#define FDE_READ 0x0001
36#define FDE_WRITE 0x0002
37#define FDE_ERROR 0x0004
38#define FDE_TIMEOUT 0x0008
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080039
Josh Gao95068bb2019-07-08 15:23:17 -070040// Internal states.
41#define FDE_EVENTMASK 0x00ff
42#define FDE_STATEMASK 0xff00
43
44#define FDE_ACTIVE 0x0100
45#define FDE_PENDING 0x0200
Josh Gao95068bb2019-07-08 15:23:17 -070046
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080047typedef void (*fd_func)(int fd, unsigned events, void *userdata);
Josh Gaoc162c712019-01-25 15:30:25 -080048typedef void (*fd_func2)(struct fdevent* fde, unsigned events, void* userdata);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080049
Josh Gaoc2cf1212019-06-28 16:34:36 -070050struct fdevent;
Josh Gao95068bb2019-07-08 15:23:17 -070051std::string dump_fde(const fdevent* fde);
Josh Gaoc2cf1212019-06-28 16:34:36 -070052
53struct fdevent_context {
Josh Gao95eef6b2019-07-08 17:37:23 -070054 public:
Josh Gaoc2cf1212019-06-28 16:34:36 -070055 virtual ~fdevent_context() = default;
56
57 // Allocate and initialize a new fdevent object.
58 virtual fdevent* Create(unique_fd fd, std::variant<fd_func, fd_func2> func, void* arg) = 0;
59
60 // Deallocate an fdevent object, returning the file descriptor that was owned by it.
61 virtual unique_fd Destroy(fdevent* fde) = 0;
62
63 // Change which events should cause notifications.
64 virtual void Set(fdevent* fde, unsigned events) = 0;
65 virtual void Add(fdevent* fde, unsigned events) = 0;
66 virtual void Del(fdevent* fde, unsigned events) = 0;
67
68 // Set a timeout on an fdevent.
69 // If no events are triggered by the timeout, an FDE_TIMEOUT will be generated.
70 // Note timeouts are not defused automatically; if a timeout is set on an fdevent, it will
71 // trigger repeatedly every |timeout| ms.
72 virtual void SetTimeout(fdevent* fde, std::optional<std::chrono::milliseconds> timeout) = 0;
73
74 // Loop forever, handling events.
Josh Gao95eef6b2019-07-08 17:37:23 -070075 // Implementations should call FlushRunQueue on every iteration.
Josh Gaoc2cf1212019-06-28 16:34:36 -070076 virtual void Loop() = 0;
77
Josh Gao2c95bf72019-07-08 18:05:16 -070078 // Assert that the caller is either running on the context's main thread, or that there is no
79 // active main thread.
80 void CheckMainThread();
Josh Gaoc2cf1212019-06-28 16:34:36 -070081
82 // Queue an operation to be run on the main thread.
Josh Gao95eef6b2019-07-08 17:37:23 -070083 void Run(std::function<void()> fn);
Josh Gaoc2cf1212019-06-28 16:34:36 -070084
85 // Test-only functionality:
86 virtual void TerminateLoop() = 0;
87 virtual size_t InstalledCount() = 0;
Josh Gao95eef6b2019-07-08 17:37:23 -070088
89 protected:
90 // Interrupt the run loop.
91 virtual void Interrupt() = 0;
92
93 // Run all pending functions enqueued via Run().
94 void FlushRunQueue() EXCLUDES(run_queue_mutex_);
95
Josh Gao2c95bf72019-07-08 18:05:16 -070096 std::optional<uint64_t> main_thread_id_ = std::nullopt;
97
Josh Gao95eef6b2019-07-08 17:37:23 -070098 private:
99 std::mutex run_queue_mutex_;
100 std::deque<std::function<void()>> run_queue_ GUARDED_BY(run_queue_mutex_);
Josh Gaoc2cf1212019-06-28 16:34:36 -0700101};
102
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700103struct fdevent {
Josh Gaoded557f2018-06-18 14:55:27 -0700104 uint64_t id;
105
Josh Gao3b37fa22018-05-14 13:42:49 -0700106 unique_fd fd;
Josh Gao71f775a2018-05-14 11:14:33 -0700107 int force_eof = 0;
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700108
Josh Gao71f775a2018-05-14 11:14:33 -0700109 uint16_t state = 0;
110 uint16_t events = 0;
Josh Gao1a901182019-01-31 15:51:52 -0800111 std::optional<std::chrono::milliseconds> timeout;
112 std::chrono::steady_clock::time_point last_active;
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700113
Josh Gaoc162c712019-01-25 15:30:25 -0800114 std::variant<fd_func, fd_func2> func;
Josh Gao71f775a2018-05-14 11:14:33 -0700115 void* arg = nullptr;
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700116};
117
Josh Gaoc2cf1212019-06-28 16:34:36 -0700118// Backwards compatibility shims that forward to the global fdevent_context.
119fdevent* fdevent_create(int fd, fd_func func, void* arg);
Josh Gaoc162c712019-01-25 15:30:25 -0800120fdevent* fdevent_create(int fd, fd_func2 func, void* arg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800121
Josh Gao2f6c2fa2018-09-20 17:38:55 -0700122unique_fd fdevent_release(fdevent* fde);
Josh Gaoc2cf1212019-06-28 16:34:36 -0700123void fdevent_destroy(fdevent* fde);
Josh Gao2f6c2fa2018-09-20 17:38:55 -0700124
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800125void fdevent_set(fdevent *fde, unsigned events);
126void fdevent_add(fdevent *fde, unsigned events);
127void fdevent_del(fdevent *fde, unsigned events);
Josh Gao1a901182019-01-31 15:51:52 -0800128void fdevent_set_timeout(fdevent* fde, std::optional<std::chrono::milliseconds> timeout);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800129void fdevent_loop();
Yabin Cuib5e11412017-03-10 16:01:01 -0800130void check_main_thread();
131
Josh Gao4c936392017-05-03 14:10:39 -0700132// Queue an operation to run on the main thread.
133void fdevent_run_on_main_thread(std::function<void()> fn);
134
Josh Gao022d4472016-02-10 14:49:00 -0800135// The following functions are used only for tests.
136void fdevent_terminate_loop();
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700137size_t fdevent_installed_count();
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700138void fdevent_reset();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800139
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800140#endif