• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
20 #include <stddef.h>
21 #include <stdint.h>  /* for int64_t */
22 
23 #include <functional>
24 
25 /* events that may be observed */
26 #define FDE_READ              0x0001
27 #define FDE_WRITE             0x0002
28 #define FDE_ERROR             0x0004
29 
30 /* features that may be set (via the events set/add/del interface) */
31 #define FDE_DONT_CLOSE        0x0080
32 
33 typedef void (*fd_func)(int fd, unsigned events, void *userdata);
34 
35 struct fdevent {
36     fdevent *next;
37     fdevent *prev;
38 
39     int fd;
40     int force_eof;
41 
42     uint16_t state;
43     uint16_t events;
44 
45     fd_func func;
46     void *arg;
47 };
48 
49 /* Allocate and initialize a new fdevent object
50  * Note: use FD_TIMER as 'fd' to create a fd-less object
51  * (used to implement timers).
52 */
53 fdevent *fdevent_create(int fd, fd_func func, void *arg);
54 
55 /* Uninitialize and deallocate an fdevent object that was
56 ** created by fdevent_create()
57 */
58 void fdevent_destroy(fdevent *fde);
59 
60 /* Initialize an fdevent object that was externally allocated
61 */
62 void fdevent_install(fdevent *fde, int fd, fd_func func, void *arg);
63 
64 /* Uninitialize an fdevent object that was initialized by
65 ** fdevent_install()
66 */
67 void fdevent_remove(fdevent *item);
68 
69 /* Change which events should cause notifications
70 */
71 void fdevent_set(fdevent *fde, unsigned events);
72 void fdevent_add(fdevent *fde, unsigned events);
73 void fdevent_del(fdevent *fde, unsigned events);
74 
75 void fdevent_set_timeout(fdevent *fde, int64_t  timeout_ms);
76 
77 /* loop forever, handling events.
78 */
79 void fdevent_loop();
80 
81 void check_main_thread();
82 
83 // Queue an operation to run on the main thread.
84 void fdevent_run_on_main_thread(std::function<void()> fn);
85 
86 // The following functions are used only for tests.
87 void fdevent_terminate_loop();
88 size_t fdevent_installed_count();
89 void fdevent_reset();
90 void set_main_thread();
91 
92 #endif
93