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
25 /* An edited version of verto's verto-libev.c, using an embedded libev with
32 #include "verto-k5ev.h"
33 #include <verto-module.h>
36 #define EV_STANDALONE 1
37 /* Avoids using clock_gettime; we probably shouldn't have to do this. */
38 #define EV_USE_REALTIME 0
39 #define EV_FEATURES 0x5f /* Everything but back ends */
43 /* ev.c explicitly disables poll() on Mac or FreeBSD; fall back to select(). */
44 #define EV_USE_SELECT 1
48 k5ev_ctx_free(void *ctx)
50 if (ctx != EV_DEFAULT)
55 k5ev_ctx_run(void *ctx)
61 k5ev_ctx_run_once(void *ctx)
63 ev_run(ctx, EVRUN_ONCE);
67 k5ev_ctx_break(void *ctx)
69 ev_break(ctx, EVBREAK_ONE);
73 k5ev_ctx_reinitialize(void *ctx)
79 libev_callback(EV_P_ ev_watcher *w, int revents)
81 if (verto_get_type(w->data) == VERTO_EV_TYPE_CHILD)
82 verto_set_proc_status(w->data, ((ev_child*) w)->rstatus);
87 #define setuptype(type, priv, ...) \
88 type ## w = malloc(sizeof(ev_ ## type)); \
91 ev_ ## type ## _init(type ## w, (EV_CB(type, (*))) __VA_ARGS__); \
92 type ## w->data = (void *) priv; \
93 ev_ ## type ## _start(ctx, type ## w); \
97 k5ev_ctx_add(void *ctx, const verto_ev *ev, verto_ev_flag *flags)
100 ev_timer *timerw = NULL;
101 ev_idle *idlew = NULL;
102 ev_signal *signalw = NULL;
103 ev_child *childw = NULL;
105 int events = EV_NONE;
107 *flags |= VERTO_EV_FLAG_PERSIST;
108 switch (verto_get_type(ev)) {
109 case VERTO_EV_TYPE_IO:
110 if (verto_get_flags(ev) & VERTO_EV_FLAG_IO_READ)
112 if (verto_get_flags(ev) & VERTO_EV_FLAG_IO_WRITE)
114 setuptype(io, ev, libev_callback, verto_get_fd(ev), events);
115 case VERTO_EV_TYPE_TIMEOUT:
116 interval = ((ev_tstamp) verto_get_interval(ev)) / 1000.0;
117 setuptype(timer, ev, libev_callback, interval, interval);
118 case VERTO_EV_TYPE_IDLE:
119 setuptype(idle, ev, libev_callback);
120 case VERTO_EV_TYPE_SIGNAL:
121 setuptype(signal, ev, libev_callback, verto_get_signal(ev));
122 case VERTO_EV_TYPE_CHILD:
123 *flags &= ~VERTO_EV_FLAG_PERSIST; /* Child events don't persist */
124 setuptype(child, ev, libev_callback, verto_get_proc(ev), 0);
126 return NULL; /* Not supported */
131 k5ev_ctx_del(void *ctx, const verto_ev *ev, void *evpriv)
133 switch (verto_get_type(ev)) {
134 case VERTO_EV_TYPE_IO:
135 ev_io_stop(ctx, evpriv);
137 case VERTO_EV_TYPE_TIMEOUT:
138 ev_timer_stop(ctx, evpriv);
140 case VERTO_EV_TYPE_IDLE:
141 ev_idle_stop(ctx, evpriv);
143 case VERTO_EV_TYPE_SIGNAL:
144 ev_signal_stop(ctx, evpriv);
146 case VERTO_EV_TYPE_CHILD:
147 ev_child_stop(ctx, evpriv);
156 static verto_ctx *verto_convert_k5ev(struct ev_loop* loop);
158 VERTO_MODULE(k5ev, NULL,
160 VERTO_EV_TYPE_TIMEOUT |
162 VERTO_EV_TYPE_SIGNAL |
163 VERTO_EV_TYPE_CHILD);
168 return verto_convert_k5ev(ev_loop_new(EVFLAG_AUTO));
172 verto_default_k5ev(void)
174 return verto_convert_k5ev(ev_default_loop(EVFLAG_AUTO));
177 /* Don't export this since our underlying libev is hidden. */
179 verto_convert_k5ev(struct ev_loop* loop)
181 return verto_convert(k5ev, loop);