1*7f2fe78bSCy Schubert /* 2*7f2fe78bSCy Schubert * Copyright 2011 Red Hat, Inc. 3*7f2fe78bSCy Schubert * 4*7f2fe78bSCy Schubert * Permission is hereby granted, free of charge, to any person 5*7f2fe78bSCy Schubert * obtaining a copy of this software and associated documentation files 6*7f2fe78bSCy Schubert * (the "Software"), to deal in the Software without restriction, 7*7f2fe78bSCy Schubert * including without limitation the rights to use, copy, modify, merge, 8*7f2fe78bSCy Schubert * publish, distribute, sublicense, and/or sell copies of the Software, 9*7f2fe78bSCy Schubert * and to permit persons to whom the Software is furnished to do so, 10*7f2fe78bSCy Schubert * subject to the following conditions: 11*7f2fe78bSCy Schubert * 12*7f2fe78bSCy Schubert * The above copyright notice and this permission notice shall be 13*7f2fe78bSCy Schubert * included in all copies or substantial portions of the Software. 14*7f2fe78bSCy Schubert * 15*7f2fe78bSCy Schubert * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16*7f2fe78bSCy Schubert * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17*7f2fe78bSCy Schubert * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18*7f2fe78bSCy Schubert * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19*7f2fe78bSCy Schubert * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20*7f2fe78bSCy Schubert * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21*7f2fe78bSCy Schubert * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22*7f2fe78bSCy Schubert * SOFTWARE. 23*7f2fe78bSCy Schubert */ 24*7f2fe78bSCy Schubert 25*7f2fe78bSCy Schubert /*** THE FOLLOWING ARE FOR IMPLEMENTATION MODULES ONLY ***/ 26*7f2fe78bSCy Schubert 27*7f2fe78bSCy Schubert #ifndef VERTO_MODULE_H_ 28*7f2fe78bSCy Schubert #define VERTO_MODULE_H_ 29*7f2fe78bSCy Schubert 30*7f2fe78bSCy Schubert #include <verto.h> 31*7f2fe78bSCy Schubert 32*7f2fe78bSCy Schubert #ifndef VERTO_MODULE_TYPES 33*7f2fe78bSCy Schubert #define VERTO_MODULE_TYPES 34*7f2fe78bSCy Schubert typedef void verto_mod_ctx; 35*7f2fe78bSCy Schubert typedef void verto_mod_ev; 36*7f2fe78bSCy Schubert #endif 37*7f2fe78bSCy Schubert 38*7f2fe78bSCy Schubert #define VERTO_MODULE_VERSION 3 39*7f2fe78bSCy Schubert #define VERTO_MODULE_TABLE(name) verto_module_table_ ## name 40*7f2fe78bSCy Schubert #define VERTO_MODULE(name, symb, types) \ 41*7f2fe78bSCy Schubert static verto_ctx_funcs name ## _funcs = { \ 42*7f2fe78bSCy Schubert name ## _ctx_new, \ 43*7f2fe78bSCy Schubert name ## _ctx_default, \ 44*7f2fe78bSCy Schubert name ## _ctx_free, \ 45*7f2fe78bSCy Schubert name ## _ctx_run, \ 46*7f2fe78bSCy Schubert name ## _ctx_run_once, \ 47*7f2fe78bSCy Schubert name ## _ctx_break, \ 48*7f2fe78bSCy Schubert name ## _ctx_reinitialize, \ 49*7f2fe78bSCy Schubert name ## _ctx_set_flags, \ 50*7f2fe78bSCy Schubert name ## _ctx_add, \ 51*7f2fe78bSCy Schubert name ## _ctx_del \ 52*7f2fe78bSCy Schubert }; \ 53*7f2fe78bSCy Schubert verto_module VERTO_MODULE_TABLE(name) = { \ 54*7f2fe78bSCy Schubert VERTO_MODULE_VERSION, \ 55*7f2fe78bSCy Schubert # name, \ 56*7f2fe78bSCy Schubert # symb, \ 57*7f2fe78bSCy Schubert types, \ 58*7f2fe78bSCy Schubert &name ## _funcs, \ 59*7f2fe78bSCy Schubert }; \ 60*7f2fe78bSCy Schubert verto_ctx * \ 61*7f2fe78bSCy Schubert verto_new_ ## name() \ 62*7f2fe78bSCy Schubert { \ 63*7f2fe78bSCy Schubert return verto_convert(name, 0, NULL); \ 64*7f2fe78bSCy Schubert } \ 65*7f2fe78bSCy Schubert verto_ctx * \ 66*7f2fe78bSCy Schubert verto_default_ ## name() \ 67*7f2fe78bSCy Schubert { \ 68*7f2fe78bSCy Schubert return verto_convert(name, 1, NULL); \ 69*7f2fe78bSCy Schubert } 70*7f2fe78bSCy Schubert 71*7f2fe78bSCy Schubert typedef struct { 72*7f2fe78bSCy Schubert /* Required */ verto_mod_ctx *(*ctx_new)(); 73*7f2fe78bSCy Schubert /* Optional */ verto_mod_ctx *(*ctx_default)(); 74*7f2fe78bSCy Schubert /* Required */ void (*ctx_free)(verto_mod_ctx *ctx); 75*7f2fe78bSCy Schubert /* Optional */ void (*ctx_run)(verto_mod_ctx *ctx); 76*7f2fe78bSCy Schubert /* Required */ void (*ctx_run_once)(verto_mod_ctx *ctx); 77*7f2fe78bSCy Schubert /* Optional */ void (*ctx_break)(verto_mod_ctx *ctx); 78*7f2fe78bSCy Schubert /* Optional */ void (*ctx_reinitialize)(verto_mod_ctx *ctx); 79*7f2fe78bSCy Schubert /* Optional */ void (*ctx_set_flags)(verto_mod_ctx *ctx, 80*7f2fe78bSCy Schubert const verto_ev *ev, 81*7f2fe78bSCy Schubert verto_mod_ev *modev); 82*7f2fe78bSCy Schubert /* Required */ verto_mod_ev *(*ctx_add)(verto_mod_ctx *ctx, 83*7f2fe78bSCy Schubert const verto_ev *ev, 84*7f2fe78bSCy Schubert verto_ev_flag *flags); 85*7f2fe78bSCy Schubert /* Required */ void (*ctx_del)(verto_mod_ctx *ctx, 86*7f2fe78bSCy Schubert const verto_ev *ev, 87*7f2fe78bSCy Schubert verto_mod_ev *modev); 88*7f2fe78bSCy Schubert } verto_ctx_funcs; 89*7f2fe78bSCy Schubert 90*7f2fe78bSCy Schubert typedef struct { 91*7f2fe78bSCy Schubert unsigned int vers; 92*7f2fe78bSCy Schubert const char *name; 93*7f2fe78bSCy Schubert const char *symb; 94*7f2fe78bSCy Schubert verto_ev_type types; 95*7f2fe78bSCy Schubert verto_ctx_funcs *funcs; 96*7f2fe78bSCy Schubert } verto_module; 97*7f2fe78bSCy Schubert 98*7f2fe78bSCy Schubert /** 99*7f2fe78bSCy Schubert * Converts an existing implementation specific loop to a verto_ctx. 100*7f2fe78bSCy Schubert * 101*7f2fe78bSCy Schubert * This function also sets the internal default implementation so that future 102*7f2fe78bSCy Schubert * calls to verto_new(NULL) or verto_default(NULL) will use this specific 103*7f2fe78bSCy Schubert * implementation if it was not already set. 104*7f2fe78bSCy Schubert * 105*7f2fe78bSCy Schubert * @param name The name of the module (unquoted) 106*7f2fe78bSCy Schubert * @param deflt Whether the ctx is the default context or not 107*7f2fe78bSCy Schubert * @param ctx The context to store 108*7f2fe78bSCy Schubert * @return A new verto_ctx, or NULL on error. Call verto_free() when done. 109*7f2fe78bSCy Schubert */ 110*7f2fe78bSCy Schubert #define verto_convert(name, deflt, ctx) \ 111*7f2fe78bSCy Schubert verto_convert_module(&VERTO_MODULE_TABLE(name), deflt, ctx) 112*7f2fe78bSCy Schubert 113*7f2fe78bSCy Schubert /** 114*7f2fe78bSCy Schubert * Converts an existing implementation specific loop to a verto_ctx. 115*7f2fe78bSCy Schubert * 116*7f2fe78bSCy Schubert * If you are a module implementation, you probably want the macro above. This 117*7f2fe78bSCy Schubert * function is generally used directly only when an application is attempting 118*7f2fe78bSCy Schubert * to expose a home-grown event loop to verto. 119*7f2fe78bSCy Schubert * 120*7f2fe78bSCy Schubert * If deflt is non-zero and a default ctx was already defined for this module 121*7f2fe78bSCy Schubert * and ctx is not NULL, than ctx will be free'd and the previously defined 122*7f2fe78bSCy Schubert * default will be returned. 123*7f2fe78bSCy Schubert * 124*7f2fe78bSCy Schubert * If ctx is non-NULL, than the pre-existing verto_mod_ctx will be converted to 125*7f2fe78bSCy Schubert * to a verto_ctx; if deflt is non-zero than this verto_mod_ctx will also be 126*7f2fe78bSCy Schubert * marked as the default loop for this process. If ctx is NULL, than the 127*7f2fe78bSCy Schubert * appropriate constructor will be called: either module->ctx_new() or 128*7f2fe78bSCy Schubert * module->ctx_default() depending on the boolean value of deflt. If 129*7f2fe78bSCy Schubert * module->ctx_default is NULL and deflt is non-zero, than module->ctx_new() 130*7f2fe78bSCy Schubert * will be called and the resulting verto_mod_ctx will be utilized as the 131*7f2fe78bSCy Schubert * default. 132*7f2fe78bSCy Schubert * 133*7f2fe78bSCy Schubert * This function also sets the internal default implementation so that future 134*7f2fe78bSCy Schubert * calls to verto_new(NULL) or verto_default(NULL) will use this specific 135*7f2fe78bSCy Schubert * implementation if it was not already set. 136*7f2fe78bSCy Schubert * 137*7f2fe78bSCy Schubert * @param name The name of the module (unquoted) 138*7f2fe78bSCy Schubert * @param ctx The context private to store 139*7f2fe78bSCy Schubert * @return A new verto_ctx, or NULL on error. Call verto_free() when done. 140*7f2fe78bSCy Schubert */ 141*7f2fe78bSCy Schubert verto_ctx * 142*7f2fe78bSCy Schubert verto_convert_module(const verto_module *module, int deflt, verto_mod_ctx *ctx); 143*7f2fe78bSCy Schubert 144*7f2fe78bSCy Schubert /** 145*7f2fe78bSCy Schubert * Calls the callback of the verto_ev and then frees it via verto_del(). 146*7f2fe78bSCy Schubert * 147*7f2fe78bSCy Schubert * The verto_ev is not freed (verto_del() is not called) if it is a signal event. 148*7f2fe78bSCy Schubert * 149*7f2fe78bSCy Schubert * @see verto_add_read() 150*7f2fe78bSCy Schubert * @see verto_add_write() 151*7f2fe78bSCy Schubert * @see verto_add_timeout() 152*7f2fe78bSCy Schubert * @see verto_add_idle() 153*7f2fe78bSCy Schubert * @see verto_add_signal() 154*7f2fe78bSCy Schubert * @see verto_add_child() 155*7f2fe78bSCy Schubert * @see verto_del() 156*7f2fe78bSCy Schubert * @param ev The verto_ev 157*7f2fe78bSCy Schubert */ 158*7f2fe78bSCy Schubert void 159*7f2fe78bSCy Schubert verto_fire(verto_ev *ev); 160*7f2fe78bSCy Schubert 161*7f2fe78bSCy Schubert /** 162*7f2fe78bSCy Schubert * Sets the status of the pid/handle which caused this event to fire. 163*7f2fe78bSCy Schubert * 164*7f2fe78bSCy Schubert * This function does nothing if the verto_ev is not a child type. 165*7f2fe78bSCy Schubert * 166*7f2fe78bSCy Schubert * @see verto_add_child() 167*7f2fe78bSCy Schubert * @param ev The verto_ev to set the status in. 168*7f2fe78bSCy Schubert * @param status The pid/handle status. 169*7f2fe78bSCy Schubert */ 170*7f2fe78bSCy Schubert void 171*7f2fe78bSCy Schubert verto_set_proc_status(verto_ev *ev, verto_proc_status status); 172*7f2fe78bSCy Schubert 173*7f2fe78bSCy Schubert /** 174*7f2fe78bSCy Schubert * Sets the state of the fd which caused this event to fire. 175*7f2fe78bSCy Schubert * 176*7f2fe78bSCy Schubert * This function does nothing if the verto_ev is not a io type. 177*7f2fe78bSCy Schubert * 178*7f2fe78bSCy Schubert * Only the flags VERTO_EV_FLAG_IO_(READ|WRITE|ERROR) are supported. All other 179*7f2fe78bSCy Schubert * flags are unset. 180*7f2fe78bSCy Schubert * 181*7f2fe78bSCy Schubert * @see verto_add_io() 182*7f2fe78bSCy Schubert * @param ev The verto_ev to set the state in. 183*7f2fe78bSCy Schubert * @param state The fd state. 184*7f2fe78bSCy Schubert */ 185*7f2fe78bSCy Schubert void 186*7f2fe78bSCy Schubert verto_set_fd_state(verto_ev *ev, verto_ev_flag state); 187*7f2fe78bSCy Schubert 188*7f2fe78bSCy Schubert #endif /* VERTO_MODULE_H_ */ 189