File: basic_event_loop.c

package info (click to toggle)
spice 0.12.5-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 14,200 kB
  • sloc: ansic: 132,929; cpp: 30,397; sh: 11,648; python: 2,896; makefile: 728
file content (284 lines) | stat: -rw-r--r-- 7,606 bytes parent folder | download | duplicates (2)
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#include <config.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/time.h>
#include <signal.h>
#include <string.h>

#include "spice/macros.h"
#include "common/ring.h"
#include "test_util.h"
#include "basic_event_loop.h"

int debug = 0;

#define DPRINTF(x, format, ...) { \
    if (x <= debug) { \
        printf("%s: " format "\n" , __FUNCTION__, ## __VA_ARGS__); \
    } \
}

#define NOT_IMPLEMENTED printf("%s not implemented\n", __func__);

static SpiceCoreInterface core;

typedef struct SpiceTimer {
    RingItem link;
    SpiceTimerFunc func;
    struct timeval tv_start;
    int ms;
    void *opaque;
} Timer;

Ring timers;

static SpiceTimer* timer_add(SpiceTimerFunc func, void *opaque)
{
    SpiceTimer *timer = calloc(sizeof(SpiceTimer), 1);

    timer->func = func;
    timer->opaque = opaque;
    ring_add(&timers, &timer->link);
    return timer;
}

static void add_ms_to_timeval(struct timeval *tv, int ms)
{
    tv->tv_usec += 1000 * ms;
    while (tv->tv_usec >= 1000000) {
        tv->tv_sec++;
        tv->tv_usec -= 1000000;
    }
}

static void timer_start(SpiceTimer *timer, uint32_t ms)
{
    ASSERT(ms);
    gettimeofday(&timer->tv_start, NULL);
    timer->ms = ms;
    // already add ms to timer value
    add_ms_to_timeval(&timer->tv_start, ms);
}

static void timer_cancel(SpiceTimer *timer)
{
    timer->ms = 0;
}

static void timer_remove(SpiceTimer *timer)
{
    ring_remove(&timer->link);
}

struct SpiceWatch {
    RingItem link;
    int fd;
    int event_mask;
    SpiceWatchFunc func;
    int removed;
    void *opaque;
};

Ring watches;

int watch_count = 0;

static SpiceWatch *watch_add(int fd, int event_mask, SpiceWatchFunc func, void *opaque)
{
    SpiceWatch *watch = malloc(sizeof(SpiceWatch));

    DPRINTF(0, "adding %p, fd=%d at %d", watch,
        fd, watch_count);
    watch->fd = fd;
    watch->event_mask = event_mask;
    watch->func = func;
    watch->opaque = opaque;
    watch->removed = FALSE;
    ring_item_init(&watch->link);
    ring_add(&watches, &watch->link);
    watch_count++;
    return watch;
}

static void watch_update_mask(SpiceWatch *watch, int event_mask)
{
    DPRINTF(0, "fd %d to %d", watch->fd, event_mask);
    watch->event_mask = event_mask;
}

static void watch_remove(SpiceWatch *watch)
{
    DPRINTF(0, "remove %p (fd %d)", watch, watch->fd);
    watch_count--;
    watch->removed = TRUE;
}

static void channel_event(int event, SpiceChannelEventInfo *info)
{
    DPRINTF(0, "channel event con, type, id, event: %d, %d, %d, %d",
            info->connection_id, info->type, info->id, event);
}

SpiceTimer *get_next_timer(void)
{
    SpiceTimer *next, *min;

    if (ring_is_empty(&timers)) {
        return NULL;
    }
    min = next = (SpiceTimer*)ring_get_head(&timers);
    while ((next=(SpiceTimer*)ring_next(&timers, &next->link)) != NULL) {
        if (next->ms &&
            (next->tv_start.tv_sec < min->tv_start.tv_sec ||
             (next->tv_start.tv_sec == min->tv_start.tv_sec &&
              next->tv_start.tv_usec < min->tv_start.tv_usec))) {
             min = next;
        }
    }
    return min;
}

struct timeval now;

void tv_b_minus_a_return_le_zero(struct timeval *a, struct timeval *b, struct timeval *dest)
{
    dest->tv_usec = b->tv_usec - a->tv_usec;
    dest->tv_sec = b->tv_sec - a->tv_sec;
    while (dest->tv_usec < 0) {
        dest->tv_usec += 1000000;
        dest->tv_sec--;
    }
    if (dest->tv_sec < 0) {
        dest->tv_sec = 0;
        dest->tv_usec = 0;
    }
}

void calc_next_timeout(SpiceTimer *next, struct timeval *timeout)
{
    gettimeofday(&now, NULL);
    tv_b_minus_a_return_le_zero(&now, &next->tv_start, timeout);
}

void timeout_timers(void)
{
    SpiceTimer *next;
    struct timeval left;
    int count = 0;

    next = (SpiceTimer*)ring_get_head(&timers);
    while (next != NULL) {
        tv_b_minus_a_return_le_zero(&now, &next->tv_start, &left);
        if (next->ms && left.tv_usec == 0 && left.tv_sec == 0) {
            count++;
            DPRINTF(2, "calling timer");
            next->ms = 0;
            next->func(next->opaque);
        }
        next = (SpiceTimer*)ring_next(&timers, &next->link);
    }
    DPRINTF(2, "called %d timers", count);
}

void basic_event_loop_mainloop(void)
{
    fd_set rfds, wfds;
    int max_fd = -1;
    int i;
    int retval;
    SpiceWatch *watch;
    SpiceTimer *next_timer;
    RingItem *link;
    RingItem *next;
    struct timeval next_timer_timeout;
    struct timeval *timeout;

    while (1) {
        FD_ZERO(&rfds);
        FD_ZERO(&wfds);
        i = 0;
        RING_FOREACH_SAFE(link, next, &watches) {
            watch = (SpiceWatch*)link;
            if (watch->removed) {
                continue;
            }
            if (watch->event_mask & SPICE_WATCH_EVENT_READ) {
                FD_SET(watch->fd, &rfds);
                max_fd = watch->fd > max_fd ? watch->fd : max_fd;
            }
            if (watch->event_mask & SPICE_WATCH_EVENT_WRITE) {
                FD_SET(watch->fd, &wfds);
                max_fd = watch->fd > max_fd ? watch->fd : max_fd;
            }
            i++;
        }
        if ((next_timer = get_next_timer()) != NULL) {
            calc_next_timeout(next_timer, &next_timer_timeout);
            timeout = &next_timer_timeout;
            DPRINTF(2, "timeout of %ld.%06ld",
                    timeout->tv_sec, timeout->tv_usec);
        } else {
            timeout = NULL;
        }
        DPRINTF(1, "watching %d fds", i);
        retval = select(max_fd + 1, &rfds, &wfds, NULL, timeout);
        if (timeout != NULL) {
            calc_next_timeout(next_timer, &next_timer_timeout);
            if (next_timer_timeout.tv_sec == 0 &&
                next_timer_timeout.tv_usec == 0) {
                timeout_timers();
            }
        }
        if (retval == -1) {
            printf("error in select - exiting\n");
            abort();
        }
        if (retval) {
            RING_FOREACH_SAFE(link, next, &watches) {
                watch = SPICE_CONTAINEROF(link, SpiceWatch, link);
                if (!watch->removed && (watch->event_mask & SPICE_WATCH_EVENT_READ)
                     && FD_ISSET(watch->fd, &rfds)) {
                    watch->func(watch->fd, SPICE_WATCH_EVENT_READ, watch->opaque);
                }
                if (!watch->removed && (watch->event_mask & SPICE_WATCH_EVENT_WRITE)
                     && FD_ISSET(watch->fd, &wfds)) {
                    watch->func(watch->fd, SPICE_WATCH_EVENT_WRITE, watch->opaque);
                }
                if (watch->removed) {
                    printf("freeing watch %p\n", watch);
                    ring_remove(&watch->link);
                    free(watch);
                }
            }
        }
    }
}

static void ignore_sigpipe(void)
{
    struct sigaction act;

    memset(&act, 0, sizeof(act));
    sigfillset(&act.sa_mask);
    act.sa_handler = SIG_IGN;
    sigaction(SIGPIPE, &act, NULL);
}

SpiceCoreInterface *basic_event_loop_init(void)
{
    ring_init(&watches);
    ring_init(&timers);
    memset(&core, 0, sizeof(core));
    core.base.major_version = SPICE_INTERFACE_CORE_MAJOR;
    core.base.minor_version = SPICE_INTERFACE_CORE_MINOR; // anything less then 3 and channel_event isn't called
    core.timer_add = timer_add;
    core.timer_start = timer_start;
    core.timer_cancel = timer_cancel;
    core.timer_remove = timer_remove;
    core.watch_add = watch_add;
    core.watch_update_mask = watch_update_mask;
    core.watch_remove = watch_remove;
    core.channel_event = channel_event;
    ignore_sigpipe();
    return &core;
}