blob: 70e0a96e0a2f630ed3a8794bba65c08f935e5f8a [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>
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +020021#include <stdint.h> /* for int64_t */
22
Josh Gao4c936392017-05-03 14:10:39 -070023#include <functional>
Josh Gaoc162c712019-01-25 15:30:25 -080024#include <variant>
Josh Gao4c936392017-05-03 14:10:39 -070025
Josh Gao3b37fa22018-05-14 13:42:49 -070026#include "adb_unique_fd.h"
27
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080028/* events that may be observed */
29#define FDE_READ 0x0001
30#define FDE_WRITE 0x0002
31#define FDE_ERROR 0x0004
32
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080033typedef void (*fd_func)(int fd, unsigned events, void *userdata);
Josh Gaoc162c712019-01-25 15:30:25 -080034typedef void (*fd_func2)(struct fdevent* fde, unsigned events, void* userdata);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080035
Yabin Cuic1b1f6f2015-09-15 16:27:09 -070036struct fdevent {
Josh Gaoded557f2018-06-18 14:55:27 -070037 uint64_t id;
38
Josh Gao3b37fa22018-05-14 13:42:49 -070039 unique_fd fd;
Josh Gao71f775a2018-05-14 11:14:33 -070040 int force_eof = 0;
Yabin Cuic1b1f6f2015-09-15 16:27:09 -070041
Josh Gao71f775a2018-05-14 11:14:33 -070042 uint16_t state = 0;
43 uint16_t events = 0;
Yabin Cuic1b1f6f2015-09-15 16:27:09 -070044
Josh Gaoc162c712019-01-25 15:30:25 -080045 std::variant<fd_func, fd_func2> func;
Josh Gao71f775a2018-05-14 11:14:33 -070046 void* arg = nullptr;
Yabin Cuic1b1f6f2015-09-15 16:27:09 -070047};
48
Josh Gaoc162c712019-01-25 15:30:25 -080049// Allocate and initialize a new fdevent object
50// TODO: Switch these to unique_fd.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080051fdevent *fdevent_create(int fd, fd_func func, void *arg);
Josh Gaoc162c712019-01-25 15:30:25 -080052fdevent* fdevent_create(int fd, fd_func2 func, void* arg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080053
Josh Gao2f6c2fa2018-09-20 17:38:55 -070054// Deallocate an fdevent object that was created by fdevent_create.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080055void fdevent_destroy(fdevent *fde);
56
Josh Gao2f6c2fa2018-09-20 17:38:55 -070057// fdevent_destroy, except releasing the file descriptor previously owned by the fdevent.
58unique_fd fdevent_release(fdevent* fde);
59
Josh Gaoc162c712019-01-25 15:30:25 -080060// Change which events should cause notifications
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080061void fdevent_set(fdevent *fde, unsigned events);
62void fdevent_add(fdevent *fde, unsigned events);
63void fdevent_del(fdevent *fde, unsigned events);
64
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +020065void fdevent_set_timeout(fdevent *fde, int64_t timeout_ms);
66
Josh Gaoc162c712019-01-25 15:30:25 -080067// Loop forever, handling events.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080068void fdevent_loop();
69
Yabin Cuib5e11412017-03-10 16:01:01 -080070void check_main_thread();
71
Josh Gao4c936392017-05-03 14:10:39 -070072// Queue an operation to run on the main thread.
73void fdevent_run_on_main_thread(std::function<void()> fn);
74
Josh Gao022d4472016-02-10 14:49:00 -080075// The following functions are used only for tests.
76void fdevent_terminate_loop();
Yabin Cuic1b1f6f2015-09-15 16:27:09 -070077size_t fdevent_installed_count();
Yabin Cuic1b1f6f2015-09-15 16:27:09 -070078void fdevent_reset();
Yabin Cuib5e11412017-03-10 16:01:01 -080079void set_main_thread();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080080
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080081#endif