signal.h (3940B)
1 /* Copyright (C) 2013-2023, 2025 Vincent Forest (vaplv@free.fr) 2 * 3 * The RSys library is free software: you can redistribute it and/or modify 4 * it under the terms of the GNU General Public License as published 5 * by the Free Software Foundation, either version 3 of the License, or 6 * (at your option) any later version. 7 * 8 * The RSys library is distributed in the hope that it will be useful, 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * GNU General Public License for more details. 12 * 13 * You should have received a copy of the GNU General Public License 14 * along with the RSys library. If not, see <http://www.gnu.org/licenses/>. */ 15 16 #ifndef SIGNAL_H 17 #define SIGNAL_H 18 19 #include "list.h" 20 #include "rsys.h" 21 22 /******************************************************************************* 23 * Simple callback data structure 24 ******************************************************************************/ 25 /* Declare a the callback data structure named `Type'. Its function arguments 26 * are `Args' and a void pointer toward its user defined data */ 27 #define CLBK(Type, Args) \ 28 typedef struct \ 29 { \ 30 struct list_node node; \ 31 void (*func)(LIST_##Args COMMA_##Args void* data ); \ 32 void* data; /* Data attached to the callback */ \ 33 } Type 34 35 /* Initialise the callback data structure. Must be called before any callback 36 * operation */ 37 #define CLBK_INIT(Clbk) \ 38 { \ 39 list_init(&(Clbk)->node); \ 40 (Clbk)->func = NULL; \ 41 (Clbk)->data = NULL; \ 42 } (void)0 43 44 /* Set the callback function and user defined data */ 45 #define CLBK_SETUP(Clbk, Func, Data) (Clbk)->func = Func, (Clbk)->data = Data 46 47 /* Disconnect the callback from the signal from which it may be attached */ 48 #define CLBK_DISCONNECT(Clbk) list_del(&(Clbk)->node) 49 50 /****************************************************************************** 51 * Minimalist signal data structure 52 ******************************************************************************/ 53 typedef struct list_node signal_T; 54 55 /* Initialise the signal. Must be called before any signal operation */ 56 #define SIG_INIT(Sig) list_init(Sig) 57 58 /* Attach the callback to the signal. If it was already attached, Clbk is 59 * firstly disconnected from the previous signal before its connection to Sig */ 60 #define SIG_CONNECT_CLBK(Sig, Clbk) \ 61 is_list_empty(&(Clbk)->node) \ 62 ? list_add((Sig), &(Clbk)->node) \ 63 : list_move(&(Clbk)->node, (Sig)) 64 65 /* Invoke the callback attached to `Sig' with the argument `Args' */ 66 #define SIG_BROADCAST(Sig, ClbkType, Args) \ 67 { \ 68 struct list_node *pos, *tmp; \ 69 LIST_FOR_EACH_SAFE(pos, tmp, (Sig)) { \ 70 ClbkType* clbk = CONTAINER_OF(pos, ClbkType, node); \ 71 ASSERT(clbk->func); \ 72 clbk->func(LIST_##Args COMMA_##Args clbk->data); \ 73 } \ 74 } (void)0 75 76 #endif /* SIGNAL_H */