blob: 39fa9c2625036552f67d082fbd9ddd5d608fbb53 [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>
24
Josh Gao3b37fa22018-05-14 13:42:49 -070025#include "adb_unique_fd.h"
26
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080027/* events that may be observed */
28#define FDE_READ 0x0001
29#define FDE_WRITE 0x0002
30#define FDE_ERROR 0x0004
31
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080032typedef void (*fd_func)(int fd, unsigned events, void *userdata);
33
Yabin Cuic1b1f6f2015-09-15 16:27:09 -070034struct fdevent {
Josh Gao3b37fa22018-05-14 13:42:49 -070035 unique_fd fd;
Josh Gao71f775a2018-05-14 11:14:33 -070036 int force_eof = 0;
Yabin Cuic1b1f6f2015-09-15 16:27:09 -070037
Josh Gao71f775a2018-05-14 11:14:33 -070038 uint16_t state = 0;
39 uint16_t events = 0;
Yabin Cuic1b1f6f2015-09-15 16:27:09 -070040
Josh Gao71f775a2018-05-14 11:14:33 -070041 fd_func func = nullptr;
42 void* arg = nullptr;
Yabin Cuic1b1f6f2015-09-15 16:27:09 -070043};
44
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080045/* Allocate and initialize a new fdevent object
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +020046 * Note: use FD_TIMER as 'fd' to create a fd-less object
47 * (used to implement timers).
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080048*/
49fdevent *fdevent_create(int fd, fd_func func, void *arg);
50
51/* Uninitialize and deallocate an fdevent object that was
52** created by fdevent_create()
53*/
54void fdevent_destroy(fdevent *fde);
55
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080056/* Change which events should cause notifications
57*/
58void fdevent_set(fdevent *fde, unsigned events);
59void fdevent_add(fdevent *fde, unsigned events);
60void fdevent_del(fdevent *fde, unsigned events);
61
David 'Digit' Turnerf6330a22009-05-18 17:36:28 +020062void fdevent_set_timeout(fdevent *fde, int64_t timeout_ms);
63
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080064/* loop forever, handling events.
65*/
66void fdevent_loop();
67
Yabin Cuib5e11412017-03-10 16:01:01 -080068void check_main_thread();
69
Josh Gao4c936392017-05-03 14:10:39 -070070// Queue an operation to run on the main thread.
71void fdevent_run_on_main_thread(std::function<void()> fn);
72
Josh Gao022d4472016-02-10 14:49:00 -080073// The following functions are used only for tests.
74void fdevent_terminate_loop();
Yabin Cuic1b1f6f2015-09-15 16:27:09 -070075size_t fdevent_installed_count();
Yabin Cuic1b1f6f2015-09-15 16:27:09 -070076void fdevent_reset();
Yabin Cuib5e11412017-03-10 16:01:01 -080077void set_main_thread();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080078
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080079#endif