|
||||
File indexing completed on 2025-01-17 09:56:14
0001 /* 0002 * Copyright 2011 Red Hat, Inc. 0003 * 0004 * Permission is hereby granted, free of charge, to any person 0005 * obtaining a copy of this software and associated documentation files 0006 * (the "Software"), to deal in the Software without restriction, 0007 * including without limitation the rights to use, copy, modify, merge, 0008 * publish, distribute, sublicense, and/or sell copies of the Software, 0009 * and to permit persons to whom the Software is furnished to do so, 0010 * subject to the following conditions: 0011 * 0012 * The above copyright notice and this permission notice shall be 0013 * included in all copies or substantial portions of the Software. 0014 * 0015 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 0016 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 0017 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 0018 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 0019 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 0020 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 0021 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 0022 * SOFTWARE. 0023 */ 0024 0025 #ifndef VERTO_H_ 0026 #define VERTO_H_ 0027 0028 #include <time.h> /* For time_t */ 0029 #include <unistd.h> /* For pid_t */ 0030 0031 #ifdef WIN32 0032 #include <windows.h> 0033 typedef HANDLE verto_proc; 0034 typedef DWORD verto_proc_status; 0035 #else 0036 #include <sys/types.h> 0037 typedef pid_t verto_proc; 0038 typedef int verto_proc_status; 0039 #endif 0040 0041 #define VERTO_SIG_IGN ((verto_callback *) 1) 0042 0043 #ifdef __cplusplus 0044 extern "C" 0045 { 0046 #endif /* __cplusplus */ 0047 0048 typedef struct verto_ctx verto_ctx; 0049 typedef struct verto_ev verto_ev; 0050 0051 typedef enum { 0052 VERTO_EV_TYPE_NONE = 0, 0053 VERTO_EV_TYPE_IO = 1, 0054 VERTO_EV_TYPE_TIMEOUT = 1 << 1, 0055 VERTO_EV_TYPE_IDLE = 1 << 2, 0056 VERTO_EV_TYPE_SIGNAL = 1 << 3, 0057 VERTO_EV_TYPE_CHILD = 1 << 4 0058 } verto_ev_type; 0059 0060 typedef enum { 0061 VERTO_EV_FLAG_NONE = 0, 0062 VERTO_EV_FLAG_PERSIST = 1, 0063 VERTO_EV_FLAG_PRIORITY_LOW = 1 << 1, 0064 VERTO_EV_FLAG_PRIORITY_MEDIUM = 1 << 2, 0065 VERTO_EV_FLAG_PRIORITY_HIGH = 1 << 3, 0066 VERTO_EV_FLAG_IO_READ = 1 << 4, 0067 VERTO_EV_FLAG_IO_WRITE = 1 << 5, 0068 VERTO_EV_FLAG_IO_ERROR = 1 << 7, 0069 VERTO_EV_FLAG_IO_CLOSE_FD = 1 << 8, 0070 VERTO_EV_FLAG_REINITIABLE = 1 << 6, 0071 _VERTO_EV_FLAG_MUTABLE_MASK = VERTO_EV_FLAG_PRIORITY_LOW 0072 | VERTO_EV_FLAG_PRIORITY_MEDIUM 0073 | VERTO_EV_FLAG_PRIORITY_HIGH 0074 | VERTO_EV_FLAG_IO_READ 0075 | VERTO_EV_FLAG_IO_WRITE, 0076 _VERTO_EV_FLAG_MAX = VERTO_EV_FLAG_IO_CLOSE_FD 0077 } verto_ev_flag; 0078 0079 typedef void (verto_callback)(verto_ctx *ctx, verto_ev *ev); 0080 0081 /** 0082 * Creates a new event context using an optionally specified implementation 0083 * and/or optionally specified required features. 0084 * 0085 * If you are an application that has already decided on using a particular 0086 * event loop implementation, you should not call this function, but instead 0087 * import the verto-NAME.h header and link against the verto-NAME.so, where 0088 * NAME is the implementation you wish to use. 0089 * 0090 * If you are a library, you should generally avoid creating event contexts 0091 * on your own but allow applications to pass in a verto_ctx you can use. 0092 * 0093 * There are two cases where you should use this function. The first is 0094 * where you have a need to choose an implementation at run time, usually 0095 * for testing purposes. The second and more common is when you simply 0096 * wish to remain implementation agnostic. In this later case, you should 0097 * always call like this: verto_new(NULL, ...). This lets verto choose the best 0098 * implementation to use. 0099 * 0100 * If impl is not NULL, a new context is returned which is backed by the 0101 * implementation specified. If the implementation specified is not 0102 * available or if the required types (reqtypes) are not provided by the 0103 * named implementation, NULL is returned. The parameter 'impl' can specify: 0104 * * The full path to an implementation library 0105 * * The name of the implementation library (i.e. - "glib" or "libev") 0106 * 0107 * If impl is NULL, verto will attempt to automatically determine the 0108 * best implementation to use. 0109 * 0110 * First, verto will attempt to use an existing, previously loaded 0111 * implementation. This is handled automatically by internal caching of either 0112 * the first implementation loaded or the one specified by verto_set_default(). 0113 * 0114 * Second, verto will attempt to discern if you are already linked to any 0115 * of the supported implementations (to avoid wasting memory by loading 0116 * extra unnecessary libraries). If you are linked to one supported 0117 * implementation, that implementation will be chosen. If you are linked 0118 * to more than one supported implementation one of the ones linked to 0119 * will be chosen, but the order of the particular choice is undefined. 0120 * 0121 * Third, verto will attempt to load the compile-time default, if defined at 0122 * build time and available at runtime. 0123 * 0124 * Last, verto will attempt to load any implementation installed. The specific 0125 * order of this step is undefined. 0126 * 0127 * In all cases above, if the implementation does not support all the specified 0128 * features (reqtypes), it will be skipped and processing will continue from 0129 * where it left off. This means that if verto_new() returns non-NULL it is 0130 * guaranteed to support the features you specified. 0131 * 0132 * @see verto_set_default() 0133 * @param impl The implementation to use, or NULL. 0134 * @param reqtypes A bitwise or'd list of required event type features. 0135 * @return A new verto_ctx, or NULL on error. Call verto_free() when done. 0136 */ 0137 verto_ctx * 0138 verto_new(const char *impl, verto_ev_type reqtypes); 0139 0140 /** 0141 * Gets the default event context using an optionally specified implementation. 0142 * 0143 * This function is essentially a singleton version of verto_new(). However, 0144 * since this function must return the same loop as the *_default() call of 0145 * the underlying implementation (if such a function exists), it is NOT a 0146 * global singleton, but a per-implementation singleton. For this reason, you 0147 * must call verto_free() when you are done with this loop. Even after calling 0148 * verto_free() on the default verto_ctx, you can safely call verto_default() 0149 * again and receive a new reference to the same (internally default) loop. 0150 * 0151 * In all other respects, verto_default() acts exactly like verto_new(). 0152 * 0153 * @see verto_new() 0154 * @see verto_free() 0155 * @param impl The implementation to use, or NULL. 0156 * @param reqtypes A bitwise or'd list of required event type features. 0157 * @return The default verto_ctx, or NULL on error. Call verto_free() when done. 0158 */ 0159 verto_ctx * 0160 verto_default(const char *impl, verto_ev_type reqtypes); 0161 0162 /** 0163 * Sets the default implementation to use by its name. 0164 * 0165 * This function returns 1 on success and 0 on failure. It can fail for the 0166 * following reasons: 0167 * 1. The default implementation was already set via verto_set_default(). 0168 * 2. The implementation specified could not be found. 0169 * 3. The implementation specified didn't support the features specified. 0170 * 4. The impl argument was NULL. 0171 * 5. verto_new() was already called. 0172 * 6. verto_default() was already called. 0173 * 7. verto_new_NAME() was already called. 0174 * 8. verto_default_NAME() was already called. 0175 * 9. verto_convert_NAME() was already called. 0176 * 0177 * @see verto_new() 0178 * @see verto_default() 0179 * @param impl The implementation to use. 0180 * @param reqtypes A bitwise or'd list of required event type features. 0181 * @return The default verto_ctx, or NULL on error. Call verto_free() when done. 0182 */ 0183 int 0184 verto_set_default(const char *impl, verto_ev_type reqtypes); 0185 0186 /** 0187 * Sets the allocator to use for verto_ctx and verto_ev objects. 0188 * 0189 * If you plan to set the allocator, you MUST call this function before any 0190 * other verto_*() calls. 0191 * 0192 * @see verto_new() 0193 * @see verto_default() 0194 * @see verto_add_io() 0195 * @see verto_add_timeout() 0196 * @see verto_add_idle() 0197 * @see verto_add_signal() 0198 * @see verto_add_child() 0199 * @param resize The allocator to use (behaves like realloc(); 0200 * resize(ptr, 0) must free memory at ptr.) 0201 * @param hierarchical Zero if the allocator is not hierarchical 0202 */ 0203 int 0204 verto_set_allocator(void *(*resize)(void *mem, size_t size), int hierarchical); 0205 0206 /** 0207 * Frees a verto_ctx. 0208 * 0209 * When called on a default verto_ctx, the reference will be freed but the 0210 * internal default loop will still be available via another call to 0211 * verto_default(). 0212 * 0213 * @see verto_new() 0214 * @see verto_default() 0215 * @param ctx The verto_ctx to free. 0216 */ 0217 void 0218 verto_free(verto_ctx *ctx); 0219 0220 /** 0221 * Frees global state. 0222 * 0223 * Remove and free all allocated global state. Call only when no further 0224 * contexts exist and all threads have exited. 0225 * 0226 * @see verto_new() 0227 * @see verto_free() 0228 * @see verto_default() 0229 */ 0230 void 0231 verto_cleanup(void); 0232 0233 /** 0234 * Run the verto_ctx forever, or at least until verto_break() is called. 0235 * 0236 * @see verto_break() 0237 * @param ctx The verto_ctx to run. 0238 */ 0239 void 0240 verto_run(verto_ctx *ctx); 0241 0242 /** 0243 * Run the verto_ctx once. May block. 0244 * 0245 * @param ctx The verto_ctx to run once. 0246 */ 0247 void 0248 verto_run_once(verto_ctx *ctx); 0249 0250 /** 0251 * Exits the currently running verto_ctx. 0252 * 0253 * @see verto_run() 0254 * @param ctx The verto_ctx to exit. 0255 */ 0256 void 0257 verto_break(verto_ctx *ctx); 0258 0259 /** 0260 * Re-initializes the verto_ctx. 0261 * 0262 * This function deletes all events, except those which have set the 0263 * VERTO_EV_FLAG_REINITIABLE flag. If you fork(), you MUST call this in the 0264 * child process after the fork! 0265 * 0266 * If this function fails it indicates that at least one 0267 * VERTO_EV_FLAG_REINITIABLE event was not rearmed or that ctx was NULL. 0268 * 0269 * @see verto_new() 0270 * @see verto_default() 0271 * @param ctx The verto_ctx to re-initialize. 0272 * @return Non-zero on success, 0 on error. 0273 */ 0274 int 0275 verto_reinitialize(verto_ctx *ctx); 0276 0277 /** 0278 * Adds a callback executed when a file descriptor is ready to be read/written. 0279 * 0280 * All verto_ev events are automatically freed when their parent verto_ctx is 0281 * freed. You do not need to free them manually. If VERTO_EV_FLAG_PERSIST is 0282 * provided, the event will repeat until verto_del() is called. If 0283 * VERTO_EV_FLAG_PERSIST is not provided, the event will be freed automatically 0284 * after its execution. In either case, you may call verto_del() at any time 0285 * to prevent the event from executing. 0286 * If VERTO_EV_FLAG_IO_CLOSE_FD is provided the passed in fd is automatically 0287 * closed when the event is freed with verto_del() 0288 * 0289 * NOTE: On Windows, the underlying select() only works with sockets. As such, 0290 * any attempt to add a non-socket io event on Windows will produce undefined 0291 * results and may even crash. 0292 * 0293 * @see verto_del() 0294 * @param ctx The verto_ctx which will fire the callback. 0295 * @param flags The flags to set (at least one VERTO_EV_FLAG_IO* required). 0296 * @param callback The callback to fire. 0297 * @param fd The file descriptor to watch for reads. 0298 * @return The verto_ev registered with the event context or NULL on error. 0299 */ 0300 verto_ev * 0301 verto_add_io(verto_ctx *ctx, verto_ev_flag flags, 0302 verto_callback *callback, int fd); 0303 0304 /** 0305 * Adds a callback executed after a period of time. 0306 * 0307 * All verto_ev events are automatically freed when their parent verto_ctx is 0308 * freed. You do not need to free them manually. If VERTO_EV_FLAG_PERSIST is 0309 * provided, the event will repeat until verto_del() is called. If 0310 * VERTO_EV_FLAG_PERSIST is not provided, the event will be freed automatically 0311 * after its execution. In either case, you may call verto_del() at any time 0312 * to prevent the event from executing. 0313 * 0314 * @see verto_del() 0315 * @param ctx The verto_ctx which will fire the callback. 0316 * @param flags The flags to set. 0317 * @param callback The callback to fire. 0318 * @param interval Time period to wait before firing (in milliseconds). 0319 * @return The verto_ev registered with the event context. 0320 */ 0321 verto_ev * 0322 verto_add_timeout(verto_ctx *ctx, verto_ev_flag flags, 0323 verto_callback *callback, time_t interval); 0324 0325 /** 0326 * Adds a callback executed when there is nothing else to do. 0327 * 0328 * All verto_ev events are automatically freed when their parent verto_ctx is 0329 * freed. You do not need to free them manually. If VERTO_EV_FLAG_PERSIST is 0330 * provided, the event will repeat until verto_del() is called. If 0331 * VERTO_EV_FLAG_PERSIST is not provided, the event will be freed automatically 0332 * after its execution. In either case, you may call verto_del() at any time 0333 * to prevent the event from executing. 0334 * 0335 * @see verto_del() 0336 * @param ctx The verto_ctx which will fire the callback. 0337 * @param flags The flags to set. 0338 * @param callback The callback to fire. 0339 * @return The verto_ev registered with the event context. 0340 */ 0341 verto_ev * 0342 verto_add_idle(verto_ctx *ctx, verto_ev_flag flags, 0343 verto_callback *callback); 0344 0345 /** 0346 * Adds a callback executed when a signal is received. 0347 * 0348 * All verto_ev events are automatically freed when their parent verto_ctx is 0349 * freed. You do not need to free them manually. If VERTO_EV_FLAG_PERSIST is 0350 * provided, the event will repeat until verto_del() is called. If 0351 * VERTO_EV_FLAG_PERSIST is not provided, the event will be freed automatically 0352 * after its execution. In either case, you may call verto_del() at any time 0353 * to prevent the event from executing. 0354 * 0355 * NOTE: If you attempt to ignore a signal without the VERTO_EV_FLAG_PERSIST 0356 * flag, this function fails. 0357 * 0358 * NOTE: SIGCHLD is expressly not supported. If you want this notification, 0359 * please use verto_add_child(). 0360 * 0361 * WARNNIG: Signal events can only be reliably received in the default verto_ctx 0362 * in some implementations. Attempting to receive signal events in non-default 0363 * loops may result in assert() failures. 0364 * 0365 * WARNING: While verto does its best to protect you from crashes, there is 0366 * essentially no way to do signal events if you mix multiple implementations in 0367 * a single process. Attempting to do so will result in undefined behavior, 0368 * and potentially even a crash. You have been warned. 0369 * 0370 * @see verto_add_child() 0371 * @see verto_repeat() 0372 * @see verto_del() 0373 * @param ctx The verto_ctx which will fire the callback. 0374 * @param flags The flags to set. 0375 * @param callback The callback to fire. 0376 * @param signal The signal to watch for. 0377 * @return The verto_ev registered with the event context. 0378 */ 0379 verto_ev * 0380 verto_add_signal(verto_ctx *ctx, verto_ev_flag flags, 0381 verto_callback *callback, int signal); 0382 0383 /** 0384 * Adds a callback executed when a child process exits. 0385 * 0386 * This event will be freed automatically after its execution. Due to the 0387 * nature of a process' life-cycle, child events cannot persist (processes only 0388 * exit once). This function returns NULL if you attempt to use 0389 * VERTO_EV_FLAG_PERSIST. You may, of course, call verto_del() at any time to 0390 * prevent the callback from firing. 0391 * 0392 * @see verto_del() 0393 * @param ctx The verto_ctx which will fire the callback. 0394 * @param flags The flags to set. 0395 * @param callback The callback to fire. 0396 * @param child The pid (POSIX) or handle (Win32) of the child to watch for. 0397 * @return The verto_ev registered with the event context. 0398 */ 0399 verto_ev * 0400 verto_add_child(verto_ctx *ctx, verto_ev_flag flags, 0401 verto_callback *callback, verto_proc proc); 0402 0403 /** 0404 * Sets the private pointer of the verto_ev. 0405 * 0406 * The free callback will be called in two cases: 0407 * 1. When the event is deleted (manually or automatically) 0408 * 2. When verto_set_private() is called again, unless 0409 * free is NULL. 0410 * 0411 * @see verto_get_private() 0412 * @param ev The verto_ev 0413 * @param priv The private value to store 0414 * @param free The callback used to free the data or NULL 0415 */ 0416 void 0417 verto_set_private(verto_ev *ev, void *priv, verto_callback *free); 0418 0419 /** 0420 * Gets the private pointer of the verto_ev. 0421 * 0422 * @see verto_set_private() 0423 * @param ev The verto_ev 0424 * @return The verto_ev private pointer 0425 */ 0426 void * 0427 verto_get_private(const verto_ev *ev); 0428 0429 /** 0430 * Gets the type of the verto_ev. 0431 * 0432 * @see verto_add_io() 0433 * @see verto_add_timeout() 0434 * @see verto_add_idle() 0435 * @see verto_add_signal() 0436 * @see verto_add_child() 0437 * @param ev The verto_ev 0438 * @return The verto_ev type 0439 */ 0440 verto_ev_type 0441 verto_get_type(const verto_ev *ev); 0442 0443 /** 0444 * Gets the flags associated with the given verto_ev. 0445 * 0446 * @see verto_add_io() 0447 * @see verto_add_timeout() 0448 * @see verto_add_idle() 0449 * @see verto_add_signal() 0450 * @see verto_add_child() 0451 * @see verto_set_flags() 0452 * @param ev The verto_ev 0453 * @return The verto_ev type 0454 */ 0455 verto_ev_flag 0456 verto_get_flags(const verto_ev *ev); 0457 0458 /** 0459 * Sets the flags associated with the given verto_ev. 0460 * 0461 * See _VERTO_EV_FLAG_MUTABLE_MASK for the flags that can be changed 0462 * with this function. All others will be ignored. If the flags specified 0463 * are the same as the flags the event already has, this function is a no-op. 0464 * 0465 * @see verto_add_io() 0466 * @see verto_add_timeout() 0467 * @see verto_add_idle() 0468 * @see verto_add_signal() 0469 * @see verto_add_child() 0470 * @see verto_get_flags() 0471 * @param ev The verto_ev 0472 * @param flags The flags for the event 0473 */ 0474 void 0475 verto_set_flags(verto_ev *ev, verto_ev_flag flags); 0476 0477 /** 0478 * Gets the file descriptor associated with a read/write verto_ev. 0479 * 0480 * @see verto_add_io() 0481 * @param ev The verto_ev to retrieve the file descriptor from. 0482 * @return The file descriptor, or -1 if not a read/write event. 0483 */ 0484 int 0485 verto_get_fd(const verto_ev *ev); 0486 0487 /** 0488 * Gets the file descriptor state from when the event fires. 0489 * 0490 * @see verto_add_io() 0491 * @param ev The verto_ev to retrieve the fd state from. 0492 * @return The fd state. 0493 */ 0494 verto_ev_flag 0495 verto_get_fd_state(const verto_ev *ev); 0496 0497 /** 0498 * Gets the interval associated with a timeout verto_ev. 0499 * 0500 * @see verto_add_timeout() 0501 * @param ev The verto_ev to retrieve the interval from. 0502 * @return The interval, or 0 if not a timeout event. 0503 */ 0504 time_t 0505 verto_get_interval(const verto_ev *ev); 0506 0507 /** 0508 * Gets the signal associated with a signal verto_ev. 0509 * 0510 * @see verto_add_signal() 0511 * @param ev The verto_ev to retrieve the signal from. 0512 * @return The signal, or -1 if not a signal event. 0513 */ 0514 int 0515 verto_get_signal(const verto_ev *ev); 0516 0517 /** 0518 * Gets the process associated with a child verto_ev. 0519 * 0520 * @see verto_add_child() 0521 * @param ev The verto_ev to retrieve the process from. 0522 * @return The pid/handle, or 0/NULL if not a child event (POSIX/Win32). 0523 */ 0524 verto_proc 0525 verto_get_proc(const verto_ev *ev); 0526 0527 /** 0528 * Gets the status of the process which caused this event to fire. 0529 * 0530 * @see verto_add_child() 0531 * @param ev The verto_ev to retrieve the status from. 0532 * @return The pid/handle status. 0533 */ 0534 verto_proc_status 0535 verto_get_proc_status(const verto_ev *ev); 0536 0537 /** 0538 * Gets the verto_ctx associated with a verto_ev. 0539 * 0540 * This is a borrowed reference, don't attempt to free it! 0541 * 0542 * @param ev The verto_ev to retrieve the verto_ctx from. 0543 * @return The verto_ctx. 0544 */ 0545 verto_ctx * 0546 verto_get_ctx(const verto_ev *ev); 0547 0548 /** 0549 * Removes an event from from the event context and frees it. 0550 * 0551 * The event and its contents cannot be used after this call. 0552 * 0553 * @see verto_add_io() 0554 * @see verto_add_timeout() 0555 * @see verto_add_idle() 0556 * @see verto_add_signal() 0557 * @see verto_add_child() 0558 * @param ev The event to delete. 0559 */ 0560 void 0561 verto_del(verto_ev *ev); 0562 0563 /** 0564 * Returns the event types supported by this implementation. 0565 * 0566 * @param ctx The verto_ctx to query. 0567 * @return The event types supported. 0568 */ 0569 verto_ev_type 0570 verto_get_supported_types(verto_ctx *ctx); 0571 0572 #ifdef __cplusplus 0573 } /* extern "C" */ 0574 #endif /* __cplusplus */ 0575 #endif /* VERTO_H_ */
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |