libcortex
Event loop library following a "batteries included" approach
Loading...
Searching...
No Matches
evloop.h
1#ifndef CRTX_EVLOOP_H
2#define CRTX_EVLOOP_H
3
4#ifdef __cplusplus
5extern "C" {
6#endif
7
8/*
9 * Mario Kicherer (dev@kicherer.org)
10 *
11 */
12
13#include <stdio.h>
14#include <sys/time.h>
15
16#include "common.h"
17
18#ifndef CRTX_DEFAULT_CLOCK
19#define CRTX_DEFAULT_CLOCK CLOCK_BOOTTIME
20#endif
21
22//
23#define CRTX_timespec2uint64(ts) (((uint64_t)(ts)->tv_sec) * UINT64_C(1000000000) + (ts)->tv_nsec)
24#define CRTX_timespec2us_u64(ts) (((uint64_t)(ts)->tv_sec) * UINT64_C(1000000) + (ts)->tv_nsec/1000)
25#define CRTX_CMP_TIMESPEC(ts1, ts2) \
26 ( \
27 ( (ts1)->tv_sec - (ts2)->tv_sec ) ? ( (ts1)->tv_sec - (ts2)->tv_sec ) : \
28 ( (ts1)->tv_nsec - (ts2)->tv_nsec ) ? ( (ts1)->tv_nsec - (ts2)->tv_nsec ) : 0 \
29 )
30
31// deprecated, use with CRTX_ prefix
32#define EVLOOP_READ (1<<0)
33#define EVLOOP_WRITE (1<<1)
34#define EVLOOP_SPECIAL (1<<2)
35#define EVLOOP_EDGE_TRIGGERED (1<<3)
36#define EVLOOP_TIMEOUT (1<<4)
37#define EVLOOP_ERROR (1<<5)
38#define EVLOOP_HUP (1<<6)
39#define EVLOOP_RDHUP (1<<7)
40
41#define CRTX_EVLOOP_READ (1<<0)
42#define CRTX_EVLOOP_WRITE (1<<1)
43#define CRTX_EVLOOP_SPECIAL (1<<2)
44#define CRTX_EVLOOP_EDGE_TRIGGERED (1<<3)
45#define CRTX_EVLOOP_TIMEOUT (1<<4)
46#define CRTX_EVLOOP_ERROR (1<<5)
47#define CRTX_EVLOOP_HUP (1<<6)
48#define CRTX_EVLOOP_RDHUP (1<<7)
49#define CRTX_EVLOOP_ALL (((1<<8)-1) & (~CRTX_EVLOOP_EDGE_TRIGGERED))
50
51struct crtx_evloop_fd;
53struct crtx_event_loop;
54
55typedef void (*crtx_evloop_error_cb_t)(struct crtx_evloop_callback *el_cb, void *data);
56
57#define CRTX_ECBF_FREE (1<<0)
58
60 struct crtx_ll ll;
61
62 int crtx_event_flags; // event flags set using libcortex #define's
63 char active;
64 char timeout_enabled;
65 struct timespec timeout;
66
67 void *el_data; // data that can be set by the event loop implementation
68
69 int triggered_flags;
70
71 struct crtx_graph *graph;
72 crtx_handle_task_t event_handler;
73 char *event_handler_name;
74 void *event_handler_data; // data that can be set by the specific listener
75
76 crtx_evloop_error_cb_t error_cb;
77 void *error_cb_data;
78
79 struct crtx_evloop_fd *fd_entry;
80
81 int flags;
82};
83
85
86// This structure contains the necessary information for a file descriptor
87// that will be added to the event loop. There can be one or more crtx_evloop_callback
88// structures for this structure where each callback can be responsible for a different
89// type of event for this file descripter (e.g., read or write).
91 struct crtx_ll ll;
92
93 int *fd;
94 int l_fd;
95 char fd_added;
96
97 void *el_data; // data that can be set by the event loop implementation
98
99 struct crtx_listener_base *listener;
100
101 struct crtx_evloop_callback *callbacks;
102
103 struct crtx_event_loop *evloop;
104
105 #ifndef CRTX_REDUCED_SIZE
106 /* reserved to avoid ABI breakage */
107 void *reserved1;
108 #endif
109};
110
112 struct crtx_graph *graph;
113 struct crtx_evloop_callback *el_cb;
114};
115
117 struct crtx_ll ll;
118
119 const char *id;
120
121 int ctrl_pipe[2];
122 struct crtx_evloop_fd ctrl_pipe_evloop_handler;
123 struct crtx_evloop_callback default_el_cb;
124
125 struct crtx_listener_base *listener;
126
127 struct crtx_evloop_fd *fds;
128
129 int (*create)(struct crtx_event_loop *evloop);
130 int (*release)(struct crtx_event_loop *evloop);
131 int (*start)(struct crtx_event_loop *evloop);
132 int (*stop)(struct crtx_event_loop *evloop);
133 int (*onetime)(struct crtx_event_loop *evloop);
134
135 int (*mod_fd)(struct crtx_event_loop *evloop, struct crtx_evloop_fd *evloop_fd);
136
137 char after_fork_close;
138
139 // if set, keep processing events but shutdown if no more events available
140 char phase_out;
141
142 struct crtx_evloop_callback *startup_el_cb;
143};
144
145extern struct crtx_event_loop *crtx_event_loops;
146
147// int crtx_evloop_add_el_fd(struct crtx_evloop_fd *el_fd);
148// int crtx_evloop_set_el_fd(struct crtx_evloop_fd *el_fd);
149struct crtx_event_loop* crtx_get_main_event_loop();
150int crtx_get_event_loop(struct crtx_event_loop **evloop, const char *id);
151
153int crtx_evloop_queue_graph(struct crtx_event_loop *evloop, struct crtx_graph *graph);
154int crtx_evloop_start(struct crtx_event_loop *evloop);
155int crtx_evloop_stop(struct crtx_event_loop *evloop);
156int crtx_evloop_release(struct crtx_event_loop *evloop);
157int crtx_evloop_create_fd_entry(struct crtx_evloop_fd *evloop_fd, struct crtx_evloop_callback *el_callback,
158 int *fd_ptr, int fd,
159 int event_flags,
160 struct crtx_graph *graph,
161 crtx_handle_task_t event_handler,
162 void *data,
163 crtx_evloop_error_cb_t error_cb,
164 void *error_cb_data
165 );
166int crtx_evloop_init_listener(struct crtx_listener_base *listener,
167 int *fd_ptr, int fd,
168 int event_flags,
169 struct crtx_graph *graph,
170 crtx_handle_task_t event_handler,
171 void *event_handler_data,
172 crtx_evloop_error_cb_t error_cb,
173 void *error_cb_data
174 );
175void crtx_evloop_update_default_fd(struct crtx_listener_base *listener, int fd);
176int crtx_evloop_enable_cb(struct crtx_evloop_callback *el_cb);
177int crtx_evloop_disable_cb(struct crtx_evloop_callback *el_cb);
178int crtx_evloop_remove_cb(struct crtx_evloop_callback *el_cb);
179
180void crtx_evloop_set_timeout(struct crtx_evloop_callback *el_cb, struct timespec *timeout);
181void crtx_evloop_set_timeout_abs(struct crtx_evloop_callback *el_cb, clockid_t clockid, uint64_t timeout_us);
182void crtx_evloop_set_timeout_rel(struct crtx_evloop_callback *el_cb, uint64_t timeout_us);
183void crtx_evloop_disable_timeout(struct crtx_evloop_callback *el_cb);
184int crtx_evloop_trigger_callback(struct crtx_event_loop *evloop, struct crtx_evloop_callback *el_cb);
185int crtx_evloop_register_startup_callback(crtx_handle_task_t event_handler, void *userdata, struct crtx_event_loop *evloop);
186int crtx_evloop_schedule_callback(crtx_handle_task_t event_handler, void *userdata, struct crtx_event_loop *evloop);
187
188void crtx_event_flags2str(FILE *fd, unsigned int flags);
189
190void crtx_evloop_handle_fd_closed(struct crtx_evloop_fd *evloop_fd);
191
192#ifdef __cplusplus
193}
194#endif
195
196#endif
Definition evloop.h:111
Definition evloop.h:116
Definition evloop.h:59
Definition evloop.h:90
structure that represents a graph of tasks (crtx_task) that will be traversed with every crtx_event
Definition core.h:115
base structure of a listener
Definition core.h:186