summaryrefslogtreecommitdiffstats
path: root/src/util/k5ev/verto-k5ev.c
blob: d244484c580b39f09c9fb058a771a86bf5f0bdb7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
 * Copyright 2011 Red Hat, Inc.
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation files
 * (the "Software"), to deal in the Software without restriction,
 * including without limitation the rights to use, copy, modify, merge,
 * publish, distribute, sublicense, and/or sell copies of the Software,
 * and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/* An edited version of verto's verto-libev.c, using an embedded libev with
 * renamed symbols. */

#include <stdlib.h>
#include <string.h>
#include <errno.h>

#include "verto-k5ev.h"
#include <verto-module.h>
#include "rename.h"
#include "autoconf.h"
#define EV_STANDALONE 1
/* Avoids using clock_gettime; we probably shouldn't have to do this. */
#define EV_USE_REALTIME 0
#define EV_FEATURES 0x5f        /* Everything but back ends */
#ifdef HAVE_POLL_H
#define EV_USE_POLL 1
#else
#define EV_USE_SELECT 1
#endif
#include "ev.c"

static void
k5ev_ctx_free(void *ctx)
{
    if (ctx != EV_DEFAULT)
        ev_loop_destroy(ctx);
}

static void
k5ev_ctx_run(void *ctx)
{
    ev_run(ctx, 0);
}

static void
k5ev_ctx_run_once(void *ctx)
{
    ev_run(ctx, EVRUN_ONCE);
}

static void
k5ev_ctx_break(void *ctx)
{
    ev_break(ctx, EVBREAK_ONE);
}

static void
libev_callback(EV_P_ ev_watcher *w, int revents)
{
    if (verto_get_type(w->data) == VERTO_EV_TYPE_CHILD)
        verto_set_proc_status(w->data, ((ev_child*) w)->rstatus);

    verto_fire(w->data);
}

#define setuptype(type, priv, ...) \
    type ## w = malloc(sizeof(ev_ ## type)); \
    if (!type ## w) \
        return NULL; \
    ev_ ## type ## _init(type ## w, (EV_CB(type, (*))) __VA_ARGS__); \
    type ## w->data = (void *) priv; \
    ev_ ## type ## _start(ctx, type ## w); \
    return type ## w

static void *
k5ev_ctx_add(void *ctx, const verto_ev *ev, verto_ev_flag *flags)
{
    ev_io *iow = NULL;
    ev_timer *timerw = NULL;
    ev_idle *idlew = NULL;
    ev_signal *signalw = NULL;
    ev_child *childw = NULL;
    ev_tstamp interval;
    int events = EV_NONE;

    *flags |= VERTO_EV_FLAG_PERSIST;
    switch (verto_get_type(ev)) {
        case VERTO_EV_TYPE_IO:
            if (verto_get_flags(ev) & VERTO_EV_FLAG_IO_READ)
                events |= EV_READ;
            if (verto_get_flags(ev) & VERTO_EV_FLAG_IO_WRITE)
                events |= EV_WRITE;
            setuptype(io, ev, libev_callback, verto_get_fd(ev), events);
        case VERTO_EV_TYPE_TIMEOUT:
            interval = ((ev_tstamp) verto_get_interval(ev)) / 1000.0;
            setuptype(timer, ev, libev_callback, interval, interval);
        case VERTO_EV_TYPE_IDLE:
            setuptype(idle, ev, libev_callback);
        case VERTO_EV_TYPE_SIGNAL:
            setuptype(signal, ev, libev_callback, verto_get_signal(ev));
        case VERTO_EV_TYPE_CHILD:
            *flags &= ~VERTO_EV_FLAG_PERSIST; /* Child events don't persist */
            setuptype(child, ev, libev_callback, verto_get_proc(ev), 0);
        default:
            return NULL; /* Not supported */
    }
}

static void
k5ev_ctx_del(void *ctx, const verto_ev *ev, void *evpriv)
{
    switch (verto_get_type(ev)) {
        case VERTO_EV_TYPE_IO:
            ev_io_stop(ctx, evpriv);
        case VERTO_EV_TYPE_TIMEOUT:
            ev_timer_stop(ctx, evpriv);
        case VERTO_EV_TYPE_IDLE:
            ev_idle_stop(ctx, evpriv);
        case VERTO_EV_TYPE_SIGNAL:
            ev_signal_stop(ctx, evpriv);
        case VERTO_EV_TYPE_CHILD:
            ev_child_stop(ctx, evpriv);
        default:
            break;
    }

    free(evpriv);
}

static verto_ctx *verto_convert_k5ev(struct ev_loop* loop);

VERTO_MODULE(k5ev, NULL,
             VERTO_EV_TYPE_IO |
             VERTO_EV_TYPE_TIMEOUT |
             VERTO_EV_TYPE_IDLE |
             VERTO_EV_TYPE_SIGNAL |
             VERTO_EV_TYPE_CHILD);

verto_ctx *
verto_new_k5ev()
{
    return verto_convert_k5ev(ev_loop_new(EVFLAG_AUTO));
}

verto_ctx *
verto_default_k5ev()
{
    return verto_convert_k5ev(ev_default_loop(EVFLAG_AUTO));
}

/* Don't export this since our underlying libev is hidden. */
static verto_ctx *
verto_convert_k5ev(struct ev_loop* loop)
{
    return verto_convert(k5ev, loop);
}