libcortex
Event loop library following a "batteries included" approach
Loading...
Searching...
No Matches
core.h
Go to the documentation of this file.
1#ifndef _CRTX_CORE_H
2#define _CRTX_CORE_H
3
8#ifdef __cplusplus
9extern "C" {
10#endif
11
12/*
13 * Mario Kicherer (dev@kicherer.org) 2016
14 *
15 */
16
17#include <stdint.h>
18#include <regex.h>
19
20#include "config.h"
21#include "threads.h"
22#include "dict.h"
23#include "linkedlist.h"
24#include "evloop.h"
25
26#define CRTX_TEST_MAIN(mainfct) \
27 int main(int argc, char **argv) { \
28 int i; \
29 crtx_init(); \
30 crtx_handle_std_signals(); \
31 CRTX_DBG("entering test function\n"); \
32 i = mainfct(argc, argv); \
33 CRTX_DBG("left test function\n"); \
34 crtx_finish(); \
35 return i; \
36 }
37
45
46struct crtx_event;
48struct crtx_graph;
49
50typedef void (*crtx_raw_to_dict_t)(struct crtx_event *event, struct crtx_dict_item *item, void *user_data);
51
52#ifndef CRTX_EVENT_TYPE_VARTYPE
53#define CRTX_EVENT_TYPE_VARTYPE unsigned int
54#endif
55
56#define CRTX_EVENT_TYPE_FAMILY_MAX (2)
57
59struct crtx_event {
60 CRTX_EVENT_TYPE_VARTYPE type;
62
64
65 struct crtx_dict_item response;
66 char response_expected;
67
68 char error;
69 char release_in_progress;
70
71 unsigned char refs_before_response;
72 unsigned char refs_before_release;
73 pthread_cond_t response_cond;
74 pthread_cond_t release_cond;
75
77
78 void (*cb_before_release)(struct crtx_event *event, void *cb_before_release_data);
79 void *cb_before_release_data;
80
81 pthread_mutex_t mutex;
82
83 #ifndef CRTX_REDUCED_SIZE
84 /* reserved to avoid ABI breakage */
85 void *reserved1;
86 void *reserved2;
87 #endif
88};
89
91struct crtx_task {
92 const char *id;
93 unsigned char position;
94 struct crtx_graph *graph;
95
97
98 crtx_handle_task_t handle;
99 crtx_handle_task_t cleanup;
100 void *userdata;
101
102 struct crtx_task *prev;
103 struct crtx_task *next;
104
105 #ifndef CRTX_REDUCED_SIZE
106 /* reserved to avoid ABI breakage */
107 void *reserved1;
108 void *reserved2;
109 #endif
110};
111
112#define CRTX_GRAPH_KEEP_GOING 1<<0
113
116 const char *name;
117
118 char stop;
119 unsigned char flags;
120
121 CRTX_EVENT_TYPE_VARTYPE *types;
122 unsigned int n_types;
123
125 unsigned int n_descriptions;
126
127 pthread_mutex_t mutex;
128
129 struct crtx_task *tasks;
130
132
133 struct crtx_dll *equeue;
134 struct crtx_dll *claimed;
135 pthread_mutex_t queue_mutex;
136 pthread_cond_t queue_cond;
137
139
140 struct crtx_thread_job_description thread_job;
141
142 struct crtx_dll ll_hdr;
143
144 #ifndef CRTX_REDUCED_SIZE
145 /* reserved to avoid ABI breakage */
146 void *reserved1;
147 void *reserved2;
148 #endif
149};
150
153 char *id;
154
155 struct crtx_listener_base *(*create)(void *options);
156
157 #ifndef CRTX_REDUCED_SIZE
158 /* reserved to avoid ABI breakage */
159 void *reserved1;
160 void *reserved2;
161 #endif
162};
163
166 CRTX_LSTNR_UNKNOWN=0,
167 CRTX_LSTNR_STARTING,
168 CRTX_LSTNR_STARTED,
169 CRTX_LSTNR_PAUSED,
170 CRTX_LSTNR_STOPPING,
171 CRTX_LSTNR_STOPPED,
172 CRTX_LSTNR_SHUTTING_DOWN,
173 CRTX_LSTNR_SHUTDOWN
174};
175
176// immediately schedule a call of the fd event handler (e.g., to query flags/timeouts/...)
177#define CRTX_LSTNR_STARTUP_TRIGGER (1<<0)
178#define CRTX_LSTNR_NO_AUTO_CLOSE (1<<1)
179
187 struct crtx_ll ll;
188
189 const char *id;
190 char *name;
191
192 MUTEX_TYPE state_mutex;
194 struct crtx_graph *state_graph;
195
197
200
202 MUTEX_TYPE source_lock;
203
204 struct crtx_ll *dependencies;
205 struct crtx_ll *rev_dependencies;
206 MUTEX_TYPE dependencies_lock;
207
208 struct crtx_thread_job_description thread_job;
209 struct crtx_thread *eloop_thread;
210 void (*thread_stop)(struct crtx_thread *thread, void *data);
211
212 char (*start_listener)(struct crtx_listener_base *listener);
213 char (*stop_listener)(struct crtx_listener_base *listener);
214 char (*update_listener)(struct crtx_listener_base *listener);
215
216 void (*shutdown)(struct crtx_listener_base *base);
217 void (*free_cb)(struct crtx_listener_base *base, void *userdata);
218 void *free_cb_userdata;
219
221
222 unsigned int flags;
223 char free_after_event;
224
225 void *userdata;
226
227 #ifndef CRTX_REDUCED_SIZE
228 /* reserved to avoid ABI breakage */
229 void *reserved1;
230 void *reserved2;
231 #endif
232};
233
236 char *id;
237
238 void (*init)();
239 void (*finish)();
240};
241
245 char *path;
246 char *basename;
247
248 void *handle;
249
250 char initialized;
251 char (*init)();
252 void (*finish)();
254 void (*get_listener_repository)(struct crtx_listener_repository **listener_repository, unsigned int *listener_repository_length);
255
257
258 #ifndef CRTX_REDUCED_SIZE
259 /* reserved to avoid ABI breakage */
260 void *reserved1;
261 void *reserved2;
262 #endif
263};
264
266 char *handler_name;
267 crtx_handle_task_t function;
268 void *handler_data;
269};
270
273 char *event_type;
274 struct crtx_ll *entries;
275};
276
279struct crtx_root {
280 struct crtx_graph **graphs;
281 unsigned int n_graphs;
282 MUTEX_TYPE graphs_mutex;
283
284 struct crtx_ll *listeners;
285 MUTEX_TYPE listeners_mutex;
286
287 struct crtx_dll *graph_queue;
288 MUTEX_TYPE graph_queue_mutex;
289
290 char initialized;
291 char shutdown;
293 struct crtx_evloop_callback shutdown_el_cb;
294 void (*reinit_cb)(void *cb_data);
295 void *reinit_cb_data;
296
297 const char *chosen_event_loop;
298 struct crtx_event_loop *event_loop;
299 struct crtx_ll *event_loops;
300
301 struct crtx_thread_job_description evloop_job;
302 struct crtx_thread *evloop_thread;
303
304 enum crtx_processing_mode default_mode;
305 enum crtx_processing_mode force_mode;
306
307 struct crtx_graph *crtx_ctrl_graph;
308
310 unsigned int n_plugins;
311
312 struct crtx_listener_repository *listener_repository;
313 unsigned int listener_repository_length;
314
315 struct crtx_ll *handler_categories;
316
317 int global_fd_flags;
318
319 struct crtx_listener_base *selfpipe_lstnr;
320
321 MUTEX_TYPE mutex;
322
323 #ifndef CRTX_REDUCED_SIZE
324 /* reserved to avoid ABI breakage */
325 void *reserved1;
326 void *reserved2;
327 #endif
328};
329
330extern struct crtx_module static_modules[];
331extern struct crtx_root *crtx_root;
332
333
334void crtx_printf(char level, char const *format, ...) __attribute__ ((format (printf, 2, 3)));
335char *crtx_stracpy(const char *str, size_t *str_length);
336struct crtx_event *new_event();
337void crtx_free_event(struct crtx_event *event);
338int crtx_init();
339int crtx_finish();
340void crtx_loop();
341int crtx_loop_onetime();
342int crtx_is_shutting_down();
343
344int crtx_create_graph(struct crtx_graph **crtx_graph, const char *name);
345int crtx_init_graph(struct crtx_graph *crtx_graph, const char *name);
346int crtx_shutdown_graph(struct crtx_graph *egraph);
347int crtx_free_graph(struct crtx_graph *egraph);
348
349// void get_eventgraph(struct crtx_graph **crtx_graph, char **event_types, unsigned int n_event_types);
350void add_task(struct crtx_graph *graph, struct crtx_task *task);
351void crtx_add_task(struct crtx_graph *graph, struct crtx_task *task);
352int crtx_create_listener_task(struct crtx_listener_base *lstnr, crtx_handle_task_t handler, void *userdata);
353struct crtx_task * crtx_graph_get_task(struct crtx_graph *graph, const char *id, crtx_handle_task_t handler);
354void add_event_type(char *event_type);
355int crtx_find_graph_add_event(struct crtx_event *event);
356void crtx_add_event(struct crtx_graph *graph, struct crtx_event *event);
357void add_event_sync(struct crtx_graph *graph, struct crtx_event *event);
358struct crtx_graph *crtx_find_graph_for_event_description(char *event_description);
359struct crtx_graph *crtx_find_graph_for_event_type(CRTX_EVENT_TYPE_VARTYPE event_type);
360struct crtx_graph *crtx_get_graph_for_event_description(char *event_type, char **new_event_types);
361struct crtx_task *crtx_new_task();
362void crtx_free_task(struct crtx_task *task);
363int crtx_wait_on_event(struct crtx_event *event);
364int crtx_create_listener(const char *id, void *options) __attribute__((deprecated));
365int crtx_setup_listener(const char *id, void *options);
366int crtx_init_listener_base(struct crtx_listener_base *lstnr);
367struct crtx_listener_base *crtx_calloc_listener_base(void);
368int crtx_listener_add_fd(struct crtx_listener_base *listener,
369 int *fd_ptr,
370 int fd,
371 int event_flags,
372 uint64_t timeout_us,
373 crtx_handle_task_t event_handler,
374 void *event_handler_data,
375 crtx_evloop_error_cb_t error_cb,
376 void *error_cb_data
377 );
378int crtx_listener_get_fd(struct crtx_listener_base *listener);
379
380void crtx_shutdown_listener(struct crtx_listener_base *listener);
381int crtx_create_event(struct crtx_event **event);
382int crtx_push_new_event(struct crtx_listener_base *lstnr, struct crtx_event **event,
383 CRTX_EVENT_TYPE_VARTYPE event_type, char *description,
384 char data_type, char *data_key_or_sign,
385 ...);
386struct crtx_task *crtx_create_task(struct crtx_graph *graph, unsigned char position, const char *id, crtx_handle_task_t handler, void *userdata);
387struct crtx_task *crtx_create_task_unique(struct crtx_graph *graph, unsigned char position, const char *id, crtx_handle_task_t handler, void *userdata);
388void crtx_init_shutdown();
389
390int crtx_start_listener(struct crtx_listener_base *listener);
391int crtx_update_listener(struct crtx_listener_base *listener);
392int crtx_stop_listener(struct crtx_listener_base *listener);
393void crtx_print_tasks(struct crtx_graph *graph);
394void crtx_hexdump(unsigned char *buffer, size_t index);
395
396void crtx_traverse_graph(struct crtx_graph *graph, struct crtx_event *event);
397void crtx_reference_event_response(struct crtx_event *event);
398void crtx_dereference_event_response(struct crtx_event *event);
399void crtx_reference_event_release(struct crtx_event *event);
400void crtx_dereference_event_release(struct crtx_event *event);
401
402// void event_ll_add(struct crtx_event_ll **list, struct crtx_event *event);
403int crtx_graph_has_task(struct crtx_graph *graph);
404int crtx_is_graph_empty(struct crtx_graph *graph);
405// void *crtx_copy_raw_data(struct crtx_event_data *data);
406
407void crtx_init_notification_listeners(void **data);
408void crtx_finish_notification_listeners(void *data);
409
410int crtx_handle_std_signals();
411
412void *crtx_process_graph_tmain(void *arg);
413int crtx_process_one_event(struct crtx_graph *graph);
414
415// void crtx_event_set_data(struct crtx_event *event, void *raw_pointer, unsigned char flags, struct crtx_dict *data_dict, unsigned char n_additional_fields);
416void crtx_event_set_raw_data(struct crtx_event *event, unsigned char type, ...);
417void crtx_event_set_dict_data(struct crtx_event *event, struct crtx_dict *data_dict, unsigned char n_additional_fields);
418int crtx_event_raw2dict(struct crtx_event *event, void *user_data);
419void crtx_event_get_payload(struct crtx_event *event, char *id, void **raw_pointer, struct crtx_dict **dict);
420struct crtx_dict *crtx_event_get_dict(struct crtx_event *event);
421struct crtx_dict_item *crtx_event_get_item_by_key(struct crtx_event *event, char *id, char *key);
422int crtx_event_get_value_by_key(struct crtx_event *event, char *key, char type, void *buffer, size_t buffer_size);
423void *crtx_event_get_ptr(struct crtx_event *event);
424char *crtx_event_get_string(struct crtx_event *event, char *key);
425void crtx_event_set_dict(struct crtx_event *event, char *signature, ...);
426int crtx_event_get_int(struct crtx_event *event, int *value);
427int crtx_event_get_data(struct crtx_event *event, void **data, size_t *size);
428
429void crtx_register_handler_for_event_type(char *event_type, char *handler_name, crtx_handle_task_t handler_function, void *handler_data);
430void crtx_autofill_graph_with_tasks(struct crtx_graph *graph, const char *event_type);
431
432enum crtx_processing_mode crtx_get_mode(enum crtx_processing_mode local_mode);
433void crtx_wait_on_graph_empty(struct crtx_graph *graph);
434
435struct crtx_listener_repository* crtx_get_new_listener_repo_entry();
436
437void crtx_set_main_event_loop(const char *event_loop);
438int crtx_separate_evloop_thread();
439
440void crtx_lock_listener_source(struct crtx_listener_base *lbase);
441void crtx_unlock_listener_source(struct crtx_listener_base *lbase);
442void crtx_lock_listener(struct crtx_listener_base *lbase);
443void crtx_unlock_listener(struct crtx_listener_base *lbase);
444void crtx_trigger_event_processing(struct crtx_listener_base *lstnr);
445void crtx_lstnr_handle_fd_closed(struct crtx_listener_base *lstnr);
446
447void crtx_shutdown_after_fork();
448
449struct crtx_thread * crtx_start_detached_event_loop();
450
451int crtx_get_version(unsigned int *major, unsigned int *minor, unsigned int *revision);
452
453void crtx_selfpipe_cb_free_lstnr(void *data);
454int crtx_selfpipe_enqueue_cb(void (*cb)(void*), void *cb_data);
455
456void crtx_print_event(struct crtx_event *event, FILE *f);
457
458int crtx_is_fd_valid(int fd);
459
460#ifdef __cplusplus
461}
462#endif
463
464#endif
crtx_processing_mode
specifies how a listener should be processed
Definition core.h:39
@ CRTX_NO_PROCESSING_MODE
the listener requires no processing, e.g., if it depends on other listeners
Definition core.h:43
@ CRTX_PREFER_THREAD
a thread should be started, e.g., for blocking calls
Definition core.h:41
@ CRTX_PREFER_NONE
unspecified mode
Definition core.h:40
@ CRTX_PREFER_ELOOP
a listener provides a file descriptor that can be added to the event loop
Definition core.h:42
int crtx_update_listener(struct crtx_listener_base *listener)
trigger a listener update, e.g., to apply changes to a running listener
Definition core.c:252
int crtx_stop_listener(struct crtx_listener_base *listener)
deactivate a listener
Definition core.c:354
void crtx_shutdown_listener(struct crtx_listener_base *listener)
release this listener
Definition core.c:470
int crtx_start_listener(struct crtx_listener_base *listener)
start or activate a listener
Definition core.c:133
int crtx_push_new_event(struct crtx_listener_base *lstnr, struct crtx_event **event, CRTX_EVENT_TYPE_VARTYPE event_type, char *description, char data_type, char *data_key_or_sign,...)
helper function to create and add an event with one call
Definition core.c:852
crtx_listener_state
the possible states of a listener crtx_listener_base
Definition core.h:165
int crtx_setup_listener(const char *id, void *options)
look for a listener repo for the given id and iniitalize a listener of this type
Definition core.c:289
void crtx_add_event(struct crtx_graph *graph, struct crtx_event *event)
add a new event to a graph for processing
Definition core.c:786
void crtx_add_task(struct crtx_graph *graph, struct crtx_task *task)
add task to graph
Definition core.c:677
void crtx_loop()
start the main event loop
Definition core.c:1232
Definition dict.h:35
Definition dict.h:61
Definition evloop.h:116
an event that is emitted by a listener crtx_listener_base
Definition core.h:59
struct crtx_dict_item data
data associated with this event
Definition core.h:63
pthread_mutex_t mutex
mutex that protects this structure
Definition core.h:81
struct crtx_listener_base * origin
the listener that generated this event
Definition core.h:76
CRTX_EVENT_TYPE_VARTYPE type
this or crtx_event::description describe the type of event
Definition core.h:60
char error
if set, event has been invalidated
Definition core.h:68
char * description
this or crtx_event::type describe the type of event
Definition core.h:61
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
unsigned int n_types
number of crtx_graph::types
Definition core.h:122
char ** descriptions
event descriptions this graph accepts
Definition core.h:124
struct crtx_listener_base * listener
listener this graph belongs to
Definition core.h:138
CRTX_EVENT_TYPE_VARTYPE * types
type of events this graph accepts
Definition core.h:121
pthread_mutex_t mutex
mutex that protects this structure
Definition core.h:127
struct crtx_dll * claimed
list of events which are currently processed
Definition core.h:134
struct crtx_task * tasks
list of tasks in this graph
Definition core.h:129
unsigned int n_descriptions
number of crtx_graph::descriptions
Definition core.h:125
enum crtx_processing_mode mode
processing mode (none, thread, eloop) of this graph
Definition core.h:131
struct crtx_dll * equeue
event queue of this graph
Definition core.h:133
Definition core.h:265
a list of crtx_handler_category_entry for a certain event type
Definition core.h:272
base structure of a listener
Definition core.h:186
MUTEX_TYPE state_mutex
mutex that protects the listener's state
Definition core.h:192
void * userdata
storage for user-provided data
Definition core.h:225
MUTEX_TYPE dependencies_lock
mutex that protects the (reverse) depenency lists
Definition core.h:206
struct crtx_ll * rev_dependencies
list of listeners which depend on us
Definition core.h:205
char autolock_source
if enabled, crtx_listener_base.source_lock is acquired before calling an event handler
Definition core.h:201
struct crtx_ll ll
linked-list entry of this listener
Definition core.h:187
struct crtx_ll * dependencies
list of listeners this listener depends on
Definition core.h:204
struct crtx_graph * graph
graph that contains the tasks that will be executed on events
Definition core.h:220
enum crtx_listener_state state
the current state of this listener
Definition core.h:193
enum crtx_processing_mode mode
processing mode (none, thread, eloop) of this listener
Definition core.h:196
struct crtx_evloop_fd evloop_fd
if this listener has a file descriptor, it will be stored in this structure
Definition core.h:198
struct crtx_evloop_callback default_el_cb
contains data about what should happen if events occur
Definition core.h:199
listener repository to create new listeners of a certain type
Definition core.h:152
structure that represents a plugin
Definition core.h:244
char(* init)()
callback that initializes this plugin
Definition core.h:251
char * path
path to the library
Definition core.h:245
void(* finish)()
callback that finishes this plugin
Definition core.h:252
void * handle
library handle
Definition core.h:248
void(* get_listener_repository)(struct crtx_listener_repository **listener_repository, unsigned int *listener_repository_length)
callback that registers the listeners the plugin provides
Definition core.h:254
char * plugin_name
name of this plugin
Definition core.h:256
char * basename
filename of the library
Definition core.h:246
represents generic modules
Definition core.h:235
core structure of libcortex
Definition core.h:279
void(* reinit_cb)(void *cb_data)
callback function that is called during reinit after a fork
Definition core.h:294
struct crtx_ll * listeners
list of registered listeners
Definition core.h:284
unsigned int n_plugins
number of available plugins in crtx_root::plugins
Definition core.h:310
struct crtx_lstnr_plugin * plugins
list of available plugins
Definition core.h:309
char reinit_after_shutdown
if true, reinitialize libcortex after a fork
Definition core.h:292
MUTEX_TYPE listeners_mutex
mutex that protects the list of listeners crtx_root::listeners
Definition core.h:285
a task basically represents a function that will be executed for an crtx_event
Definition core.h:91
crtx_handle_task_t handle
function that is called with the current event
Definition core.h:98
unsigned char position
position of the task in the graph
Definition core.h:93
char * event_type_match
if 0, task receives all events passing the graph
Definition core.h:96
Definition threads.h:51
Definition threads.h:63