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