-
Notifications
You must be signed in to change notification settings - Fork 72
/
mc_event.c
460 lines (414 loc) · 10.8 KB
/
mc_event.c
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
/*
* Copyright (C) 2010-2016 Marvell International Ltd.
* Copyright (C) 2002-2010 Kinoma, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "mc_stdio.h"
#include "mc_ipc.h"
#include "mc_time.h"
#include "mc_event.h"
#if mxMC
#include <lwip/sockets.h>
#else
#include "mc_compat.h"
#include <sys/time.h>
#include <semaphore.h>
#endif
static struct {
mc_event_callback_f callback;
void *closure;
int fd; /* only for any */
} mc_event_callbacks[FD_SETSIZE], mc_event_callback_any; /* assume FD_SETSIZE is small */
static int maxfd = -1;
static fd_set reads, writes;
#define MC_MAX_TIMEOUTS 24
static struct mc_timeout {
enum {MC_TIMEOUT_UNUSED, MC_TIMEOUT_RUNNING, MC_TIMEOUT_STOP} status;
unsigned long interval;
mc_timeout_callback_f callback;
void *closure;
uint64_t timeout;
int pri;
} mc_timeout_callbacks[MC_MAX_TIMEOUTS] = {{0}};
static mc_event_shutdown_callback_t *mc_shutdown_callback = NULL;
static int g_status = -1;
static int g_exit_status = 0;
static uint64_t
mc_event_get_time()
{
struct timeval tv;
mc_gettimeofday(&tv, NULL);
return (uint64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
}
void
mc_event_set_time(const struct timeval *tv)
{
/* adjust timer */
int i;
uint64_t ct = mc_event_get_time();
uint64_t nt = (uint64_t)tv->tv_sec * 1000 + tv->tv_usec / 1000;
uint64_t diff = nt - ct;
for (i = 0; i < MC_MAX_TIMEOUTS; i++) {
struct mc_timeout *tc = &mc_timeout_callbacks[i];
if (tc->status != MC_TIMEOUT_UNUSED)
tc->timeout += diff;
}
}
void
mc_event_shutdown()
{
int i;
mc_log_debug("event: shutting down\n");
if (mc_shutdown_callback != NULL)
(*mc_shutdown_callback->f)(g_exit_status, mc_shutdown_callback->closure);
for (i = 0; i < FD_SETSIZE; i++) {
if (mc_event_callbacks[i].callback != NULL)
(*mc_event_callbacks[i].callback)(i, MC_SOCK_CLOSE, mc_event_callbacks[i].closure);
}
}
void
mc_event_exit(int status)
{
mc_event_shutdown();
g_exit_status = status;
if (status < 0)
g_status++; /* force quit */
}
void
mc_event_register(int s, unsigned int flags, mc_event_callback_f callback, void *closure)
{
if (flags == MC_SOCK_ANY) {
mc_event_callback_any.callback = callback;
mc_event_callback_any.closure = closure;
mc_event_callback_any.fd = s;
return;
}
if (s < 0 || s >= FD_SETSIZE)
mc_fatal("PANIC! bad fd! %d\n", s);
if (flags & MC_SOCK_READ)
FD_SET(s, &reads);
else
FD_CLR(s, &reads);
if (flags & MC_SOCK_WRITE)
FD_SET(s, &writes);
else
FD_CLR(s, &writes);
mc_event_callbacks[s].callback = callback;
mc_event_callbacks[s].closure = closure;
if (s > maxfd)
maxfd = s;
}
void
mc_event_unregister(int s)
{
if (s < 0 || s >= FD_SETSIZE) {
if (mc_event_callback_any.fd == s) {
mc_event_callback_any.callback = NULL;
mc_event_callback_any.closure = NULL;
mc_event_callback_any.fd = 0;
}
return;
}
FD_CLR(s, &reads);
FD_CLR(s, &writes);
mc_event_callbacks[s].callback = NULL;
mc_event_callbacks[s].closure = NULL;
if (s == maxfd) {
while (--maxfd >= 0 && mc_event_callbacks[maxfd].callback == NULL)
;
}
}
static uint64_t
mc_event_process_timeout(xsMachine *the)
{
int n;
uint64_t t_current, t_min, t;
struct mc_timeout *tc, *tcmin;
for (n = 0; n < MC_MAX_TIMEOUTS; n++)
mc_timeout_callbacks[n].pri = 0;
t_current = mc_event_get_time();
again:
t_min = ~0U;
for (n = MC_MAX_TIMEOUTS, tc = mc_timeout_callbacks, tcmin = NULL; --n >= 0; tc++) {
if (tc->status == MC_TIMEOUT_RUNNING) {
if (tc->timeout <= t_current) {
if (tcmin == NULL || tc->timeout < tcmin->timeout || tc->pri < tcmin->pri)
tcmin = tc;
}
else if ((t = tc->timeout - t_current) < t_min)
t_min = t;
}
}
if (tcmin != NULL) {
(*tcmin->callback)(the, tcmin, tcmin->closure);
tcmin->pri++;
if (tcmin->status == MC_TIMEOUT_RUNNING)
tcmin->timeout = t_current + tcmin->interval;
goto again;
}
return t_min != ~0U ? t_min : 0U;
}
struct mc_timeout *
mc_interval_set(unsigned long interval, mc_timeout_callback_f callback, void *closure)
{
int i;
for (i = 0; i < MC_MAX_TIMEOUTS; i++) {
if (mc_timeout_callbacks[i].status == MC_TIMEOUT_UNUSED) {
struct mc_timeout *tc = &mc_timeout_callbacks[i];
tc->interval = interval;
tc->status = MC_TIMEOUT_STOP;
tc->callback = callback;
tc->closure = closure;
return tc;
}
}
mc_log_error("# Error: timeout: no slot!\n");
return NULL;
}
void
mc_interval_start(struct mc_timeout *tc)
{
tc->status = MC_TIMEOUT_RUNNING;
tc->timeout = mc_event_get_time() + tc->interval;
}
void
mc_interval_stop(struct mc_timeout *tc)
{
tc->status = MC_TIMEOUT_STOP;
}
void
mc_interval_reschedule(struct mc_timeout *tc, unsigned long newInterval)
{
tc->interval = newInterval;
mc_interval_start(tc);
}
void
mc_interval_reset(struct mc_timeout *tc)
{
tc->status = MC_TIMEOUT_UNUSED;
}
unsigned long
mc_interval_get_interval(struct mc_timeout *tc)
{
return tc->interval;
}
void
mc_interval_stat()
{
int i;
mc_log_notice("current time = %lld\n", mc_event_get_time());
for (i = 0; i < MC_MAX_TIMEOUTS; i++) {
struct mc_timeout *tc = &mc_timeout_callbacks[i];
if (tc->status != MC_TIMEOUT_UNUSED)
mc_log_notice("[%d] interval = %ld, timeout = %lld\n", i, tc->interval, tc->timeout);
}
}
typedef struct {
mc_event_thread_callback_f callback;
void *closure;
mc_semaphore_t sem;
uint32_t flags;
} mc_local_event_t;
#define MC_LOCAL_EVENT_PORT 9999
#define USE_SEMAPHORE 0
#if USE_SEMAPHORE
static void
mc_task_init(mc_local_event_t *ev)
{
mc_semaphore_create(&ev->sem);
}
static void
mc_task_sleep(mc_local_event_t *ev)
{
mc_semaphore_wait(&ev->sem);
}
static void
mc_task_wake(mc_local_event_t *ev)
{
mc_semaphore_post(&ev->sem);
}
static void
mc_task_fin(mc_local_event_t *ev)
{
mc_semaphore_delete(&ev->sem);
}
#endif
static void
mc_event_thread_call_local(mc_event_thread_callback_f callback, void *closure, uint32_t flags)
{
int s;
struct sockaddr_in sin;
mc_local_event_t ev;
int fl;
if ((s = lwip_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
return;
fl = lwip_fcntl(s, F_GETFL, 0);
lwip_fcntl(s, F_SETFL, fl | O_NONBLOCK);
sin.sin_family = AF_INET;
sin.sin_port = htons(MC_LOCAL_EVENT_PORT);
sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
ev.callback = callback;
ev.closure = closure;
ev.flags = flags;
#if USE_SEMAPHORE
mc_task_init(&ev);
#endif
lwip_sendto(s, &ev, sizeof(ev), 0, (struct sockaddr *)&sin, sizeof(sin));
#if !USE_SEMAPHORE
if (!(flags & MC_CALL_ASYNC))
lwip_recv(s, &ev, sizeof(ev), 0);
#endif
lwip_close(s);
#if USE_SEMAPHORE
if (!(flags & MC_CALL_ASYNC)) {
mc_task_sleep(&ev);
mc_task_fin(&ev);
}
#endif
}
static void
mc_event_local_callback(int s, unsigned int flags, void *closure)
{
xsMachine *the = closure;
mc_local_event_t ev;
struct sockaddr_in sin;
socklen_t slen = sizeof(sin);
int n;
if (flags & MC_SOCK_READ) {
n = lwip_recvfrom(s, &ev, sizeof(ev), 0, (struct sockaddr *)&sin, &slen);
if (n == sizeof(ev) && ev.callback != NULL)
(*ev.callback)(the, ev.closure);
#if !USE_SEMAPHORE
if (!(ev.flags & MC_CALL_ASYNC))
lwip_sendto(s, &ev, sizeof(ev), 0, (struct sockaddr *)&sin, sizeof(sin));
#endif
}
else {
mc_event_unregister(s);
lwip_close(s);
}
#if USE_SEMAPHORE
if (!(ev.flags & MC_CALL_ASYNC))
mc_task_wake(&ev);
#endif
}
typedef struct {
mc_event_thread_callback_f callback;
void *closure;
uint32_t flags;
} mc_delegation_event_t;
static mc_queue_t mc_delegation_queue;
static void
mc_delegation_main(void *data)
{
mc_delegation_event_t ev = {NULL, NULL};
do {
if (mc_queue_recv(&mc_delegation_queue, &ev, sizeof(ev)) == sizeof(ev) && ev.callback != NULL)
mc_event_thread_call_local(ev.callback, ev.closure, ev.flags);
} while (ev.callback != NULL);
mc_queue_delete(&mc_delegation_queue);
}
static int
mc_event_delegation_init()
{
if (mc_queue_create(&mc_delegation_queue, sizeof(mc_delegation_event_t)) != 0)
return -1;
if (mc_thread_create(mc_delegation_main, NULL, -1) != 0) {
mc_queue_delete(&mc_delegation_queue);
return -1;
}
return 0;
}
static void
mc_event_delegation_call(mc_event_thread_callback_f callback, void *closure, uint32_t flags)
{
mc_delegation_event_t ev;
ev.callback = callback;
ev.closure = closure;
ev.flags = flags;
mc_queue_send(&mc_delegation_queue, &ev, sizeof(ev));
}
static void
mc_event_delegation_fin()
{
mc_event_delegation_call(NULL, NULL, 0); /* terminate the thread */
}
int
mc_event_thread_call(mc_event_thread_callback_f callback, void *closure, uint32_t flags)
{
if (g_status != 0)
return -1;
if (flags & MC_CALL_CRITICAL)
mc_event_delegation_call(callback, closure, flags);
else
mc_event_thread_call_local(callback, closure, flags);
return 0;
}
int
mc_event_main(xsMachine *the, mc_event_shutdown_callback_t *cb)
{
struct timeval tv;
fd_set rs, ws;
int n = -1, i;
unsigned int flags;
uint64_t timeInterval;
struct sockaddr_in sin;
int localevent_sock;
mc_shutdown_callback = cb;
g_status = -1; /* not ready yet */
if ((localevent_sock = lwip_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
return -1;
sin.sin_family = AF_INET;
sin.sin_port = htons(MC_LOCAL_EVENT_PORT);
sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
lwip_bind(localevent_sock, (struct sockaddr *)&sin, sizeof(sin));
mc_event_register(localevent_sock, MC_SOCK_READ, mc_event_local_callback, the);
mc_event_delegation_init();
g_status = 0; /* running */
while (!g_status) {
timeInterval = mc_event_process_timeout(the);
if (timeInterval != 0) {
tv.tv_sec = timeInterval / 1000;
tv.tv_usec = (timeInterval % 1000) * 1000;
}
if (maxfd < 0) { /* @@ what if only timer is running?? */
mc_log_debug("event: no one is waiting!\n");
break; /* no one is waiting for anything! will be blocked forever unless break */
}
rs = reads;
ws = writes;
n = lwip_select(maxfd + 1, &rs, &ws, NULL, timeInterval > 0 ? &tv : NULL);
if (mc_event_callback_any.callback != NULL)
(*mc_event_callback_any.callback)(mc_event_callback_any.fd, (unsigned int)n, mc_event_callback_any.closure);
if (n > 0) {
for (i = 0; i <= maxfd; i++) {
flags = 0;
if (FD_ISSET(i, &rs))
flags |= MC_SOCK_READ;
if (FD_ISSET(i, &ws))
flags |= MC_SOCK_WRITE;
if (flags) {
if (mc_event_callbacks[i].callback == NULL) {
mc_log_error("event: %s ready for closed socket(%d)!?\n", flags & MC_SOCK_WRITE ? "write" : "read", i);
continue;
}
(*mc_event_callbacks[i].callback)(i, flags, mc_event_callbacks[i].closure);
}
}
}
}
mc_event_delegation_fin();
lwip_close(localevent_sock);
return g_exit_status;
}