Debian Codegraph @

src/util/dispatch.h

raw
1 #pragma once
2
3 /*
4 * Event Dispatcher
5 */
6
7 #include <c-list.h>
8 #include <c-stdaux.h>
9 #include <stdlib.h>
10
11 enum {
12 _DISPATCH_E_SUCCESS,
13
14 DISPATCH_E_EXIT,
15 DISPATCH_E_FAILURE,
16 };
17
18 typedef struct DispatchContext DispatchContext;
19 typedef struct DispatchFile DispatchFile;
20 typedef int (*DispatchFn) (DispatchFile *file);
21
22 /* files */
23
24 struct DispatchFile {
25 DispatchContext *context;
26 CList ready_link;
27 DispatchFn fn;
28
29 int fd;
30 uint32_t user_mask;
31 uint32_t kernel_mask;
32 uint32_t events;
33 };
34
35 #define DISPATCH_FILE_NULL(_x) { \
36 .ready_link = C_LIST_INIT((_x).ready_link), \
37 .fd = -1, \
38 }
39
40 int dispatch_file_init(DispatchFile *file,
41 DispatchContext *ctx,
42 DispatchFn fn,
43 int fd,
44 uint32_t mask,
45 uint32_t events);
46 void dispatch_file_deinit(DispatchFile *file);
47
48 void dispatch_file_select(DispatchFile *file, uint32_t mask);
49 void dispatch_file_deselect(DispatchFile *file, uint32_t mask);
50 void dispatch_file_clear(DispatchFile *file, uint32_t mask);
51
52 /* contexts */
53
54 struct DispatchContext {
55 CList ready_list;
56 int epoll_fd;
57 size_t n_files;
58 };
59
60 #define DISPATCH_CONTEXT_NULL(_x) { \
61 .ready_list = C_LIST_INIT((_x).ready_list), \
62 .epoll_fd = -1, \
63 }
64
65 int dispatch_context_init(DispatchContext *ctx);
66 void dispatch_context_deinit(DispatchContext *ctx);
67
68 int dispatch_context_poll(DispatchContext *ctx, int timeout);
69 int dispatch_context_dispatch(DispatchContext *ctx);
70
71 /* inline helpers */
72
73 static inline uint32_t dispatch_file_events(DispatchFile *file) {
74 return file->events & file->user_mask;
75 }