1 /* 2 * Copyright (C) 2019 - 2020 Intel Corporation 3 * 4 * SPDX-License-Identifier: BSD-3-Clause 5 */ 6 /* 7 * This defines a simple mainloop for reading from multiple sockets and handling 8 * the one that becomes readable. Note that on Windows, it can currently only 9 * handle sockets, not arbitrary descriptors, since we only need it for RPC. 10 */ 11 #ifndef _USFSTL_LOOP_H_ 12 #define _USFSTL_LOOP_H_ 13 #include <unistd.h> 14 #include <string.h> 15 #include <stdint.h> 16 #include "list.h" 17 18 #ifdef _WIN32 19 typedef uintptr_t usfstl_fd_t; 20 #else 21 typedef int usfstl_fd_t; 22 #endif 23 24 /** 25 * struct usfstl_loop_entry - main loop entry 26 * @list: private 27 * @handler: handler to call when fd is readable 28 * @fd: file descriptor 29 * @priority: priority, higher is handled earlier; 30 * must not change while the entry is registered 31 * @data: user data 32 */ 33 struct usfstl_loop_entry { 34 struct usfstl_list_entry list; 35 void (*handler)(struct usfstl_loop_entry *); 36 usfstl_fd_t fd; 37 int priority; 38 void *data; 39 }; 40 41 extern struct usfstl_list g_usfstl_loop_entries; 42 43 /** 44 * g_usfstl_loop_pre_handler_fn - pre-handler function 45 * 46 * If assigned (defaults to %NULL) this handler will be called 47 * before every loop handler's handling function, e.g. in order 48 * to synchronize time with the wallclock scheduler. 49 */ 50 extern void (*g_usfstl_loop_pre_handler_fn)(void *data); 51 extern void *g_usfstl_loop_pre_handler_fn_data; 52 53 /** 54 * usfstl_loop_register - add an entry to the mainloop 55 * @entry: the entry to add, must be fully set up including 56 * the priority 57 */ 58 void usfstl_loop_register(struct usfstl_loop_entry *entry); 59 60 /** 61 * usfstl_loop_unregister - remove an entry from the mainloop 62 * @entry: the entry to remove 63 */ 64 void usfstl_loop_unregister(struct usfstl_loop_entry *entry); 65 66 /** 67 * usfstl_loop_wait_and_handle - wait and handle a single event 68 * 69 * Wait for, and handle, a single event, then return. 70 */ 71 void usfstl_loop_wait_and_handle(void); 72 73 /** 74 * usfstl_loop_for_each_entry - iterate main loop entries 75 */ 76 #define usfstl_loop_for_each_entry(entry) \ 77 usfstl_for_each_list_item(entry, &g_usfstl_loop_entries, list) 78 79 /** 80 * usfstl_loop_for_each_entry_safe - iterate main loop entries safely 81 * 82 * Where "safely" means safe to concurrent modification. 83 */ 84 #define usfstl_loop_for_each_entry_safe(entry, tmp) \ 85 usfstl_for_each_list_item_safe(entry, tmp, &g_usfstl_loop_entries, list) 86 87 #endif // _USFSTL_LOOP_H_ 88