blob: a953cc17d241062df83fd041ab341f7b5cd89f5a [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 Gao4c936392017-05-03 14:10:39 -070024#include <functional>
Josh Gao1a901182019-01-31 15:51:52 -080025#include <optional>
Josh Gaoc162c712019-01-25 15:30:25 -080026#include <variant>
Josh Gao4c936392017-05-03 14:10:39 -070027
Josh Gao3b37fa22018-05-14 13:42:49 -070028#include "adb_unique_fd.h"
29
Josh Gao1a901182019-01-31 15:51:52 -080030// Events that may be observed
31#define FDE_READ 0x0001
32#define FDE_WRITE 0x0002
33#define FDE_ERROR 0x0004
34#define FDE_TIMEOUT 0x0008
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080035
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080036typedef void (*fd_func)(int fd, unsigned events, void *userdata);
Josh Gaoc162c712019-01-25 15:30:25 -080037typedef void (*fd_func2)(struct fdevent* fde, unsigned events, void* userdata);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080038
Josh Gaoc2cf1212019-06-28 16:34:36 -070039struct fdevent;
40
41struct fdevent_context {
42 virtual ~fdevent_context() = default;
43
44 // Allocate and initialize a new fdevent object.
45 virtual fdevent* Create(unique_fd fd, std::variant<fd_func, fd_func2> func, void* arg) = 0;
46
47 // Deallocate an fdevent object, returning the file descriptor that was owned by it.
48 virtual unique_fd Destroy(fdevent* fde) = 0;
49
50 // Change which events should cause notifications.
51 virtual void Set(fdevent* fde, unsigned events) = 0;
52 virtual void Add(fdevent* fde, unsigned events) = 0;
53 virtual void Del(fdevent* fde, unsigned events) = 0;
54
55 // Set a timeout on an fdevent.
56 // If no events are triggered by the timeout, an FDE_TIMEOUT will be generated.
57 // Note timeouts are not defused automatically; if a timeout is set on an fdevent, it will
58 // trigger repeatedly every |timeout| ms.
59 virtual void SetTimeout(fdevent* fde, std::optional<std::chrono::milliseconds> timeout) = 0;
60
61 // Loop forever, handling events.
62 virtual void Loop() = 0;
63
64 // Assert that the caller is running on the context's main thread.
65 virtual void CheckMainThread() = 0;
66
67 // Queue an operation to be run on the main thread.
68 virtual void Run(std::function<void()> fn) = 0;
69
70 // Test-only functionality:
71 virtual void TerminateLoop() = 0;
72 virtual size_t InstalledCount() = 0;
73 virtual void Reset() = 0;
74};
75
Yabin Cuic1b1f6f2015-09-15 16:27:09 -070076struct fdevent {
Josh Gaoded557f2018-06-18 14:55:27 -070077 uint64_t id;
78
Josh Gao3b37fa22018-05-14 13:42:49 -070079 unique_fd fd;
Josh Gao71f775a2018-05-14 11:14:33 -070080 int force_eof = 0;
Yabin Cuic1b1f6f2015-09-15 16:27:09 -070081
Josh Gao71f775a2018-05-14 11:14:33 -070082 uint16_t state = 0;
83 uint16_t events = 0;
Josh Gao1a901182019-01-31 15:51:52 -080084 std::optional<std::chrono::milliseconds> timeout;
85 std::chrono::steady_clock::time_point last_active;
Yabin Cuic1b1f6f2015-09-15 16:27:09 -070086
Josh Gaoc162c712019-01-25 15:30:25 -080087 std::variant<fd_func, fd_func2> func;
Josh Gao71f775a2018-05-14 11:14:33 -070088 void* arg = nullptr;
Yabin Cuic1b1f6f2015-09-15 16:27:09 -070089};
90
Josh Gaoc2cf1212019-06-28 16:34:36 -070091// Backwards compatibility shims that forward to the global fdevent_context.
92fdevent* fdevent_create(int fd, fd_func func, void* arg);
Josh Gaoc162c712019-01-25 15:30:25 -080093fdevent* fdevent_create(int fd, fd_func2 func, void* arg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080094
Josh Gao2f6c2fa2018-09-20 17:38:55 -070095unique_fd fdevent_release(fdevent* fde);
Josh Gaoc2cf1212019-06-28 16:34:36 -070096void fdevent_destroy(fdevent* fde);
Josh Gao2f6c2fa2018-09-20 17:38:55 -070097
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080098void fdevent_set(fdevent *fde, unsigned events);
99void fdevent_add(fdevent *fde, unsigned events);
100void fdevent_del(fdevent *fde, unsigned events);
Josh Gao1a901182019-01-31 15:51:52 -0800101void fdevent_set_timeout(fdevent* fde, std::optional<std::chrono::milliseconds> timeout);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800102void fdevent_loop();
Yabin Cuib5e11412017-03-10 16:01:01 -0800103void check_main_thread();
104
Josh Gao4c936392017-05-03 14:10:39 -0700105// Queue an operation to run on the main thread.
106void fdevent_run_on_main_thread(std::function<void()> fn);
107
Josh Gao022d4472016-02-10 14:49:00 -0800108// The following functions are used only for tests.
109void fdevent_terminate_loop();
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700110size_t fdevent_installed_count();
Yabin Cuic1b1f6f2015-09-15 16:27:09 -0700111void fdevent_reset();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800112
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800113#endif