2 * Copyright 2011 Red Hat, Inc.
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:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
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
28 #include <time.h> /* For time_t */
29 #include <unistd.h> /* For pid_t */
33 typedef HANDLE verto_proc;
34 typedef DWORD verto_proc_status;
36 typedef pid_t verto_proc;
37 typedef int verto_proc_status;
40 #define VERTO_SIG_IGN ((verto_callback *) 1)
42 typedef struct _verto_ctx verto_ctx;
43 typedef struct _verto_ev verto_ev;
46 VERTO_EV_TYPE_NONE = 0,
48 VERTO_EV_TYPE_TIMEOUT = 1 << 1,
49 VERTO_EV_TYPE_IDLE = 1 << 2,
50 VERTO_EV_TYPE_SIGNAL = 1 << 3,
51 VERTO_EV_TYPE_CHILD = 1 << 4
55 VERTO_EV_FLAG_NONE = 0,
56 VERTO_EV_FLAG_PERSIST = 1,
57 VERTO_EV_FLAG_PRIORITY_LOW = 1 << 1,
58 VERTO_EV_FLAG_PRIORITY_MEDIUM = 1 << 2,
59 VERTO_EV_FLAG_PRIORITY_HIGH = 1 << 3,
60 VERTO_EV_FLAG_IO_READ = 1 << 4,
61 VERTO_EV_FLAG_IO_WRITE = 1 << 5,
62 VERTO_EV_FLAG_REINITIABLE = 1 << 6,
63 _VERTO_EV_FLAG_MAX = VERTO_EV_FLAG_REINITIABLE
66 typedef void (verto_callback)(verto_ctx *ctx, verto_ev *ev);
69 * Creates a new event context using an optionally specified implementation
70 * and/or optionally specified required features.
72 * If you are an application that has already decided on using a particular
73 * event loop implementation, you should not call this function, but instead
74 * import the verto-NAME.h header and link against the verto-NAME.so, where
75 * NAME is the implementation you wish to use.
77 * If you are a library, you should generally avoid creating event contexts
78 * on your own but allow applications to pass in a verto_ctx you can use.
80 * There are two cases where you should use this function. The first is
81 * where you have a need to choose an implementation at run time, usually
82 * for testing purposes. The second and more common is when you simply
83 * wish to remain implementation agnostic. In this later case, you should
84 * always call like this: verto_new(NULL, ...). This lets verto choose the best
85 * implementation to use.
87 * If impl is not NULL, a new context is returned which is backed by the
88 * implementation specified. If the implementation specified is not
89 * available or if the required types (reqtypes) are not provided by the
90 * named implementation, NULL is returned. The parameter 'impl' can specify:
91 * * The full path to an implementation library
92 * * The name of the implementation library (i.e. - "glib" or "libev")
94 * If impl is NULL, verto will attempt to automatically determine the
95 * best implementation to use.
97 * First, verto will attempt to use an existing, previously loaded
98 * implementation. This is handled automatically by internal caching of either
99 * the first implementation loaded or the one specified by verto_set_default().
101 * Second, verto will attempt to discern if you are already linked to any
102 * of the supported implementations (to avoid wasting memory by loading
103 * extra unnecessary libraries). If you are linked to one supported
104 * implementation, that implementation will be chosen. If you are linked
105 * to more than one supported implementation one of the ones linked to
106 * will be chosen, but the order of the particular choice is undefined.
108 * Third, verto will attempt to load the compile-time default, if defined at
109 * build time and available at runtime.
111 * Last, verto will attempt to load any implementation installed. The specific
112 * order of this step is undefined.
114 * In all cases above, if the implementation does not support all the specified
115 * features (reqtypes), it will be skipped and processing will continue from
116 * where it left off. This means that if verto_new() returns non-NULL it is
117 * guaranteed to support the features you specified.
119 * @see verto_set_default()
120 * @param impl The implementation to use, or NULL.
121 * @param reqtypes A bitwise or'd list of required event type features.
122 * @return A new _ev_ctx, or NULL on error. Call verto_free() when done.
125 verto_new(const char *impl, verto_ev_type reqtypes);
128 * Gets the default event context using an optionally specified implementation.
130 * This function is essentially a singleton version of verto_new(). However,
131 * since this function must return the same loop as the *_default() call of
132 * the underlying implementation (if such a function exists), it is NOT a
133 * global singleton, but a per-implementation singleton. For this reason, you
134 * must call verto_free() when you are done with this loop. Even after calling
135 * verto_free() on the default verto_ctx, you can safely call verto_default()
136 * again and receive a new reference to the same (internally default) loop.
138 * In all other respects, verto_default() acts exactly like verto_new().
142 * @param impl The implementation to use, or NULL.
143 * @param reqtypes A bitwise or'd list of required event type features.
144 * @return The default _ev_ctx, or NULL on error. Call verto_free() when done.
147 verto_default(const char *impl, verto_ev_type reqtypes);
150 * Sets the default implementation to use by its name.
152 * This function returns 1 on success and 0 on failure. It can fail for the
154 * 1. The default implementation was already set via verto_set_default().
155 * 2. The implementation specified could not be found.
156 * 3. The implementation specified didn't support the features specified.
157 * 4. The impl argument was NULL.
158 * 5. verto_new() was already called.
159 * 6. verto_default() was already called.
160 * 7. verto_new_NAME() was already called.
161 * 8. verto_default_NAME() was already called.
162 * 9. verto_convert_NAME() was already called.
165 * @see verto_default()
166 * @param impl The implementation to use.
167 * @param reqtypes A bitwise or'd list of required event type features.
168 * @return The default _ev_ctx, or NULL on error. Call verto_free() when done.
171 verto_set_default(const char *impl, verto_ev_type reqtypes);
176 * When called on a default verto_ctx, the reference will be freed but the
177 * internal default loop will still be available via another call to
181 * @see verto_default()
182 * @param ctx The verto_ctx to free.
185 verto_free(verto_ctx *ctx);
188 * Run the verto_ctx forever, or at least until verto_break() is called.
191 * @param ctx The verto_ctx to run.
194 verto_run(verto_ctx *ctx);
197 * Run the verto_ctx once. May block.
199 * @param ctx The verto_ctx to run once.
202 verto_run_once(verto_ctx *ctx);
205 * Exits the currently running verto_ctx.
208 * @param ctx The verto_ctx to exit.
211 verto_break(verto_ctx *ctx);
214 * Re-initializes the verto_ctx.
216 * This function deletes all events, except those which have set the
217 * VERTO_EV_FLAG_REINITIABLE flag. If you fork(), you MUST call this in the
218 * child process after the fork!
221 * @see verto_default()
222 * @param ctx The verto_ctx to re-initialize.
225 verto_reinitialize(verto_ctx *ctx);
228 * Adds a callback executed when a file descriptor is ready to be read/written.
230 * All verto_ev events are automatically freed when their parent verto_ctx is
231 * freed. You do not need to free them manually. If VERTO_EV_FLAG_PERSIST is
232 * provided, the event will repeat until verto_del() is called. If
233 * VERTO_EV_FLAG_PERSIST is not provided, the event will be freed automatically
234 * after its execution. In either case, you may call verto_del() at any time
235 * to prevent the event from executing.
237 * NOTE: On Windows, the underlying select() only works with sockets. As such,
238 * any attempt to add a non-socket io event on Windows will produce undefined
239 * results and may even crash.
242 * @param ctx The verto_ctx which will fire the callback.
243 * @param flags The flags to set (at least one VERTO_EV_FLAG_IO* required).
244 * @param callback The callback to fire.
245 * @param fd The file descriptor to watch for reads.
246 * @return The verto_ev registered with the event context or NULL on error.
249 verto_add_io(verto_ctx *ctx, verto_ev_flag flags,
250 verto_callback *callback, int fd);
253 * Adds a callback executed after a period of time.
255 * All verto_ev events are automatically freed when their parent verto_ctx is
256 * freed. You do not need to free them manually. If VERTO_EV_FLAG_PERSIST is
257 * provided, the event will repeat until verto_del() is called. If
258 * VERTO_EV_FLAG_PERSIST is not provided, the event will be freed automatically
259 * after its execution. In either case, you may call verto_del() at any time
260 * to prevent the event from executing.
263 * @param ctx The verto_ctx which will fire the callback.
264 * @param flags The flags to set.
265 * @param callback The callback to fire.
266 * @param interval Time period to wait before firing (in milliseconds).
267 * @return The verto_ev registered with the event context.
270 verto_add_timeout(verto_ctx *ctx, verto_ev_flag flags,
271 verto_callback *callback, time_t interval);
274 * Adds a callback executed when there is nothing else to do.
276 * All verto_ev events are automatically freed when their parent verto_ctx is
277 * freed. You do not need to free them manually. If VERTO_EV_FLAG_PERSIST is
278 * provided, the event will repeat until verto_del() is called. If
279 * VERTO_EV_FLAG_PERSIST is not provided, the event will be freed automatically
280 * after its execution. In either case, you may call verto_del() at any time
281 * to prevent the event from executing.
284 * @param ctx The verto_ctx which will fire the callback.
285 * @param flags The flags to set.
286 * @param callback The callback to fire.
287 * @return The verto_ev registered with the event context.
290 verto_add_idle(verto_ctx *ctx, verto_ev_flag flags,
291 verto_callback *callback);
294 * Adds a callback executed when a signal is received.
296 * All verto_ev events are automatically freed when their parent verto_ctx is
297 * freed. You do not need to free them manually. If VERTO_EV_FLAG_PERSIST is
298 * provided, the event will repeat until verto_del() is called. If
299 * VERTO_EV_FLAG_PERSIST is not provided, the event will be freed automatically
300 * after its execution. In either case, you may call verto_del() at any time
301 * to prevent the event from executing.
303 * NOTE: If you attempt to ignore a signal without the VERTO_EV_FLAG_PERSIST
304 * flag, this function fails.
306 * NOTE: SIGCHLD is expressly not supported. If you want this notification,
307 * please use verto_add_child().
309 * WARNNIG: Signal events can only be reliably received in the default _ev_ctx
310 * in some implementations. Attempting to receive signal events in non-default
311 * loops may result in assert() failures.
313 * WARNING: While verto does its best to protect you from crashes, there is
314 * essentially no way to do signal events if you mix multiple implementations in
315 * a single process. Attempting to do so will result in undefined behavior,
316 * and potentially even a crash. You have been warned.
318 * @see verto_add_child()
319 * @see verto_repeat()
321 * @param ctx The verto_ctx which will fire the callback.
322 * @param flags The flags to set.
323 * @param callback The callback to fire.
324 * @param signal The signal to watch for.
325 * @return The verto_ev registered with the event context.
328 verto_add_signal(verto_ctx *ctx, verto_ev_flag flags,
329 verto_callback *callback, int signal);
332 * Adds a callback executed when a child process exits.
334 * This event will be freed automatically after its execution. Due to the
335 * nature of a process' life-cycle, child events cannot persist (processes only
336 * exit once). This function returns NULL if you attempt to use
337 * VERTO_EV_FLAG_PERSIST. You may, of course, call verto_del() at any time to
338 * prevent the callback from firing.
341 * @param ctx The verto_ctx which will fire the callback.
342 * @param flags The flags to set.
343 * @param callback The callback to fire.
344 * @param child The pid (POSIX) or handle (Win32) of the child to watch for.
345 * @return The verto_ev registered with the event context.
348 verto_add_child(verto_ctx *ctx, verto_ev_flag flags,
349 verto_callback *callback, verto_proc proc);
352 * Sets the private pointer of the verto_ev.
354 * The free callback will be called in two cases:
355 * 1. When the event is deleted (manually or automatically)
356 * 2. When verto_set_private() is called again, unless
359 * @see verto_get_private()
360 * @param ev The verto_ev
361 * @param priv The private value to store
362 * @param free The callback used to free the data or NULL
365 verto_set_private(verto_ev *ev, void *priv, verto_callback *free);
368 * Gets the private pointer of the verto_ev.
370 * @see verto_set_private()
371 * @param ev The verto_ev
372 * @return The verto_ev private pointer
375 verto_get_private(const verto_ev *ev);
378 * Gets the type of the verto_ev.
380 * @see verto_add_io()
381 * @see verto_add_timeout()
382 * @see verto_add_idle()
383 * @see verto_add_signal()
384 * @see verto_add_child()
385 * @param ev The verto_ev
386 * @return The verto_ev type
389 verto_get_type(const verto_ev *ev);
392 * Gets the flags associated with the given verto_ev.
394 * @see verto_add_io()
395 * @see verto_add_timeout()
396 * @see verto_add_idle()
397 * @see verto_add_signal()
398 * @see verto_add_child()
399 * @param ev The verto_ev
400 * @return The verto_ev type
403 verto_get_flags(const verto_ev *ev);
406 * Gets the file descriptor associated with a read/write verto_ev.
408 * @see verto_add_io()
409 * @param ev The verto_ev to retrieve the file descriptor from.
410 * @return The file descriptor, or -1 if not a read/write event.
413 verto_get_fd(const verto_ev *ev);
416 * Gets the interval associated with a timeout verto_ev.
418 * @see verto_add_timeout()
419 * @param ev The verto_ev to retrieve the interval from.
420 * @return The interval, or 0 if not a timeout event.
423 verto_get_interval(const verto_ev *ev);
426 * Gets the signal associated with a signal verto_ev.
428 * @see verto_add_signal()
429 * @param ev The verto_ev to retrieve the signal from.
430 * @return The signal, or -1 if not a signal event.
433 verto_get_signal(const verto_ev *ev);
436 * Gets the process associated with a child verto_ev.
438 * @see verto_add_child()
439 * @param ev The verto_ev to retrieve the process from.
440 * @return The pid/handle, or 0/NULL if not a child event (POSIX/Win32).
443 verto_get_proc(const verto_ev *ev);
446 * Gets the status of the process which caused this event to fire.
448 * @see verto_add_child()
449 * @param ev The verto_ev to retrieve the status from.
450 * @return The pid/handle status.
453 verto_get_proc_status(const verto_ev *ev);
456 * Removes an event from from the event context and frees it.
458 * The event and its contents cannot be used after this call.
460 * @see verto_add_io()
461 * @see verto_add_timeout()
462 * @see verto_add_idle()
463 * @see verto_add_signal()
464 * @see verto_add_child()
465 * @param ev The event to delete.
468 verto_del(verto_ev *ev);
471 * Returns the event types supported by this implementation.
473 * @param ctx The verto_ctx to query.
474 * @return The event types supported.
477 verto_get_supported_types(verto_ctx *ctx);
479 #endif /* VERTO_H_ */