blob: 9c6e06999e1a873b84016d0d1baa6519069d83e4 [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>
David 'Digit' Turner1f1efb52009-05-18 17:36:28 +020021#include <stdint.h> /* for int64_t */
22
Josh Gao764f8c52017-05-03 14:10:39 -070023#include <functional>
24
David 'Digit' Turnerb1c2c952009-05-18 17:07:46 +020025/* events that may be observed */
26#define FDE_READ 0x0001
27#define FDE_WRITE 0x0002
28#define FDE_ERROR 0x0004
29
David 'Digit' Turnerb1c2c952009-05-18 17:07:46 +020030typedef void (*fd_func)(int fd, unsigned events, void *userdata);
31
Yabin Cui2ce9d562015-09-15 16:27:09 -070032struct fdevent {
Josh Gao9528df22018-05-14 11:14:33 -070033 fdevent* next = nullptr;
34 fdevent* prev = nullptr;
Yabin Cui2ce9d562015-09-15 16:27:09 -070035
Josh Gao9528df22018-05-14 11:14:33 -070036 int fd = -1;
37 int force_eof = 0;
Yabin Cui2ce9d562015-09-15 16:27:09 -070038
Josh Gao9528df22018-05-14 11:14:33 -070039 uint16_t state = 0;
40 uint16_t events = 0;
Yabin Cui2ce9d562015-09-15 16:27:09 -070041
Josh Gao9528df22018-05-14 11:14:33 -070042 fd_func func = nullptr;
43 void* arg = nullptr;
Yabin Cui2ce9d562015-09-15 16:27:09 -070044};
45
David 'Digit' Turnerb1c2c952009-05-18 17:07:46 +020046/* Allocate and initialize a new fdevent object
David 'Digit' Turner1f1efb52009-05-18 17:36:28 +020047 * Note: use FD_TIMER as 'fd' to create a fd-less object
48 * (used to implement timers).
David 'Digit' Turnerb1c2c952009-05-18 17:07:46 +020049*/
50fdevent *fdevent_create(int fd, fd_func func, void *arg);
51
52/* Uninitialize and deallocate an fdevent object that was
53** created by fdevent_create()
54*/
55void fdevent_destroy(fdevent *fde);
56
David 'Digit' Turnerb1c2c952009-05-18 17:07:46 +020057/* Change which events should cause notifications
58*/
59void fdevent_set(fdevent *fde, unsigned events);
60void fdevent_add(fdevent *fde, unsigned events);
61void fdevent_del(fdevent *fde, unsigned events);
62
David 'Digit' Turner1f1efb52009-05-18 17:36:28 +020063void fdevent_set_timeout(fdevent *fde, int64_t timeout_ms);
64
David 'Digit' Turnerb1c2c952009-05-18 17:07:46 +020065/* loop forever, handling events.
66*/
67void fdevent_loop();
68
Yabin Cui3cf1b362017-03-10 16:01:01 -080069void check_main_thread();
70
Josh Gao764f8c52017-05-03 14:10:39 -070071// Queue an operation to run on the main thread.
72void fdevent_run_on_main_thread(std::function<void()> fn);
73
Josh Gao511763b2016-02-10 14:49:00 -080074// The following functions are used only for tests.
75void fdevent_terminate_loop();
Yabin Cui2ce9d562015-09-15 16:27:09 -070076size_t fdevent_installed_count();
Yabin Cui2ce9d562015-09-15 16:27:09 -070077void fdevent_reset();
Yabin Cui3cf1b362017-03-10 16:01:01 -080078void set_main_thread();
David 'Digit' Turnerb1c2c952009-05-18 17:07:46 +020079
David 'Digit' Turnerb1c2c952009-05-18 17:07:46 +020080#endif