| 
									
										
										
										
											1999-02-11 22:18:36 +00:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  *	BIRD Library -- Event Processing | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  *	(c) 1999 Martin Mares <mj@ucw.cz> | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  *	Can be freely distributed and used under the terms of the GNU GPL. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifndef _BIRD_EVENT_H_
 | 
					
						
							|  |  |  | #define _BIRD_EVENT_H_
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "lib/resource.h"
 | 
					
						
							| 
									
										
										
										
											2021-06-19 20:50:18 +02:00
										 |  |  | #include "lib/locking.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <stdatomic.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-24 19:53:34 +02:00
										 |  |  | struct birdloop; | 
					
						
							| 
									
										
										
										
											1999-02-11 22:18:36 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | typedef struct event { | 
					
						
							|  |  |  |   resource r; | 
					
						
							| 
									
										
										
										
											2000-04-27 22:28:49 +00:00
										 |  |  |   void (*hook)(void *); | 
					
						
							| 
									
										
										
										
											1999-02-11 22:18:36 +00:00
										 |  |  |   void *data; | 
					
						
							| 
									
										
										
										
											2022-06-24 19:53:34 +02:00
										 |  |  |   struct event * _Atomic next; | 
					
						
							| 
									
										
										
										
											2022-09-12 11:09:43 +02:00
										 |  |  |   struct event_list * _Atomic list; | 
					
						
							| 
									
										
										
										
											1999-02-11 22:18:36 +00:00
										 |  |  | } event; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-12 11:09:43 +02:00
										 |  |  | typedef struct event_list { | 
					
						
							|  |  |  |   event * _Atomic receiver;	/* Event receive list */ | 
					
						
							|  |  |  |   event * _Atomic _executor;	/* Event execute list */ | 
					
						
							|  |  |  |   const char *name; | 
					
						
							|  |  |  |   struct birdloop *loop;	/* The executor loop */ | 
					
						
							| 
									
										
										
										
											2021-06-19 20:50:18 +02:00
										 |  |  | } event_list; | 
					
						
							| 
									
										
										
										
											1999-02-11 22:18:36 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | extern event_list global_event_list; | 
					
						
							| 
									
										
										
										
											2021-03-12 15:35:56 +01:00
										 |  |  | extern event_list global_work_list; | 
					
						
							| 
									
										
										
										
											1999-02-11 22:18:36 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | event *ev_new(pool *); | 
					
						
							| 
									
										
										
										
											2000-04-27 22:28:49 +00:00
										 |  |  | void ev_run(event *); | 
					
						
							| 
									
										
										
										
											2022-06-24 19:53:34 +02:00
										 |  |  | void ev_init_list(event_list *, struct birdloop *loop, const char *name); | 
					
						
							|  |  |  | void ev_enqueue(event_list *, event *); | 
					
						
							|  |  |  | #define ev_send ev_enqueue
 | 
					
						
							| 
									
										
										
										
											2021-06-19 20:50:18 +02:00
										 |  |  | #define ev_send_loop(l, e) ev_send(birdloop_event_list((l)), (e))
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define ev_schedule(e) ({ ASSERT_THE_BIRD_LOCKED; if (!ev_active((e))) ev_send(&global_event_list, (e)); })
 | 
					
						
							|  |  |  | #define ev_schedule_work(e) ({ ASSERT_THE_BIRD_LOCKED; if (!ev_active((e))) ev_send(&global_work_list, (e)); })
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-02-11 22:18:36 +00:00
										 |  |  | void ev_postpone(event *); | 
					
						
							| 
									
										
										
										
											2021-03-12 15:35:56 +01:00
										 |  |  | int ev_run_list_limited(event_list *, uint); | 
					
						
							| 
									
										
										
										
											2022-06-24 19:53:34 +02:00
										 |  |  | #define ev_run_list(l)	ev_run_list_limited((l), ~0)
 | 
					
						
							| 
									
										
										
										
											1999-02-11 22:18:36 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-19 20:50:18 +02:00
										 |  |  | #define LEGACY_EVENT_LIST(l)  (((l) == &global_event_list) || ((l) == &global_work_list))
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-03 10:42:55 +01:00
										 |  |  | static inline int | 
					
						
							|  |  |  | ev_active(event *e) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-09-12 11:09:43 +02:00
										 |  |  |   return atomic_load_explicit(&e->list, memory_order_acquire) != NULL; | 
					
						
							| 
									
										
										
										
											2022-06-24 19:53:34 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2021-06-19 20:50:18 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-24 19:53:34 +02:00
										 |  |  | static inline event_list * | 
					
						
							|  |  |  | ev_get_list(event *e) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-09-12 11:09:43 +02:00
										 |  |  |   return atomic_load_explicit(&e->list, memory_order_acquire); | 
					
						
							| 
									
										
										
										
											2014-11-03 10:42:55 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-01 15:55:23 +02:00
										 |  |  | static inline event* | 
					
						
							|  |  |  | ev_new_init(pool *p, void (*hook)(void *), void *data) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   event *e = ev_new(p); | 
					
						
							|  |  |  |   e->hook = hook; | 
					
						
							|  |  |  |   e->data = data; | 
					
						
							|  |  |  |   return e; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-03 10:42:55 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-02-11 22:18:36 +00:00
										 |  |  | #endif
 |