1 /*
2  * Copyright (c) 2020 Andri Yngvason
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
9  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
10  * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
11  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
12  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
13  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
14  * PERFORMANCE OF THIS SOFTWARE.
15  */
16 
17 #pragma once
18 
19 #include <stdint.h>
20 
21 struct aml;
22 struct aml_handler;
23 struct aml_signal;
24 struct aml_work;
25 
26 typedef void (*aml_callback_fn)(void* obj);
27 
28 enum {
29 	AML_BACKEND_EDGE_TRIGGERED = 1 << 0,
30 };
31 
32 struct aml_backend {
33 	uint32_t flags;
34 	uint32_t clock;
35 	void* (*new_state)(struct aml*);
36 	void (*del_state)(void* state);
37 	int (*get_fd)(const void* state);
38 	int (*poll)(void* state, int timeout);
39 	void (*exit)(void* state);
40 	int (*add_fd)(void* state, struct aml_handler*);
41 	int (*mod_fd)(void* state, struct aml_handler*);
42 	int (*del_fd)(void* state, struct aml_handler*);
43 	int (*add_signal)(void* state, struct aml_signal*);
44 	int (*del_signal)(void* state, struct aml_signal*);
45 	int (*set_deadline)(void* state, uint64_t deadline);
46 	void (*post_dispatch)(void* state);
47 	void (*interrupt)(void* state);
48 	int (*thread_pool_acquire)(struct aml*, int n_threads);
49 	void (*thread_pool_release)(struct aml*);
50 	int (*thread_pool_enqueue)(struct aml*, struct aml_work*);
51 };
52 
53 /* These are for setting random data required by the backend implementation.
54  *
55  * The backend implementation shall NOT use aml_set_userdata() or
56  * aml_get_userdata().
57  */
58 void aml_set_backend_data(void* ptr, void* data);
59 void* aml_get_backend_data(const void* ptr);
60 
61 void* aml_get_backend_state(const struct aml*);
62 
63 /* Get the work function pointer assigned to a work object.
64  */
65 aml_callback_fn aml_get_work_fn(const struct aml_work*);
66 
67 /* revents is only used for fd events. Zero otherwise.
68  * This function may be called inside a signal handler
69  */
70 void aml_emit(struct aml* self, void* obj, uint32_t revents);
71 
72 /* Get time in milliseconds until the next timeout event.
73  *
74  * If timeout is -1, this returns:
75  *  -1 if no event is pending
76  *  0 if a timer has already expired
77  *  time until next event, otherwise
78  *
79  * Otherwise, if timeout is less than the time until the next event, timeout is
80  * returned, if it is greater, then the time until next event is returned.
81  */
82 int aml_get_next_timeout(struct aml* self, int timeout);
83