File: c_eventloop.c

package info (click to toggle)
duktape 2.7.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 21,160 kB
  • sloc: ansic: 215,359; python: 5,961; javascript: 4,555; makefile: 477; cpp: 205
file content (626 lines) | stat: -rw-r--r-- 16,130 bytes parent folder | download | duplicates (3)
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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
/*
 *  C eventloop example.
 *
 *  Timer management is similar to eventloop.js but implemented in C.
 *  In particular, timer insertion is an O(n) operation; in a real world
 *  eventloop based on a heap insertion would be O(log N).
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <sys/time.h>
#include <poll.h>

#include "duktape.h"
#include "c_eventloop.h"

#if !defined(DUKTAPE_EVENTLOOP_DEBUG)
#define DUKTAPE_EVENTLOOP_DEBUG 0       /* set to 1 to debug with printf */
#endif

#define  TIMERS_SLOT_NAME       "eventTimers"
#define  MIN_DELAY              1.0
#define  MIN_WAIT               1.0
#define  MAX_WAIT               60000.0
#define  MAX_EXPIRIES           10

#define  MAX_FDS                256
#define  MAX_TIMERS             4096     /* this is quite excessive for embedded use, but good for testing */

typedef struct {
	int64_t id;       /* numeric ID (returned from e.g. setTimeout); zero if unused */
	double target;    /* next target time */
	double delay;     /* delay/interval */
	int oneshot;      /* oneshot=1 (setTimeout), repeated=0 (setInterval) */
	int removed;      /* timer has been requested for removal */

	/* The callback associated with the timer is held in the "global stash",
	 * in <stash>.eventTimers[String(id)].  The references must be deleted
	 * when a timer struct is deleted.
	 */
} ev_timer;

/* Active timers.  Dense list, terminates to end of list or first unused timer.
 * The list is sorted by 'target', with lowest 'target' (earliest expiry) last
 * in the list.  When a timer's callback is being called, the timer is moved
 * to 'timer_expiring' as it needs special handling should the user callback
 * delete that particular timer.
 */
static ev_timer timer_list[MAX_TIMERS];
static ev_timer timer_expiring;
static int timer_count;  /* last timer at timer_count - 1 */
static int64_t timer_next_id = 1;

/* Socket poll state. */
static struct pollfd poll_list[MAX_FDS];
static int poll_count = 0;

/* Misc */
static int exit_requested = 0;

/* Get Javascript compatible 'now' timestamp (millisecs since 1970). */
static double get_now(void) {
	struct timeval tv;
	int rc;

	rc = gettimeofday(&tv, NULL);
	if (rc != 0) {
		/* Should never happen, so return whatever. */
		return 0.0;
	}
	return ((double) tv.tv_sec) * 1000.0 + ((double) tv.tv_usec) / 1000.0;
}

static ev_timer *find_nearest_timer(void) {
	/* Last timer expires first (list is always kept sorted). */
	if (timer_count <= 0) {
		return NULL;
	}
	return timer_list + timer_count - 1;
}

/* Bubble last timer on timer list backwards until it has been moved to
 * its proper sorted position (based on 'target' time).
 */
static void bubble_last_timer(void) {
	int i;
	int n = timer_count;
	ev_timer *t;
	ev_timer tmp;

	for (i = n - 1; i > 0; i--) {
		/* Timer to bubble is at index i, timer to compare to is
		 * at i-1 (both guaranteed to exist).
		 */
		t = timer_list + i;
		if (t->target <= (t-1)->target) {
			/* 't' expires earlier than (or same time as) 't-1', so we're done. */
			break;
		} else {
			/* 't' expires later than 't-1', so swap them and repeat. */
			memcpy((void *) &tmp, (void *) (t - 1), sizeof(ev_timer));
			memcpy((void *) (t - 1), (void *) t, sizeof(ev_timer));
			memcpy((void *) t, (void *) &tmp, sizeof(ev_timer));
		}
	}
}

static void expire_timers(duk_context *ctx) {
	ev_timer *t;
	int sanity = MAX_EXPIRIES;
	double now;
	int rc;

	/* Because a user callback can mutate the timer list (by adding or deleting
	 * a timer), we expire one timer and then rescan from the end again.  There
	 * is a sanity limit on how many times we do this per expiry round.
	 */

	duk_push_global_stash(ctx);
	duk_get_prop_string(ctx, -1, TIMERS_SLOT_NAME);

	/* [ ... stash eventTimers ] */

	now = get_now();
	while (sanity-- > 0) {
		/*
		 *  If exit has been requested, exit without running further
		 *  callbacks.
		 */

		if (exit_requested) {
#if DUKTAPE_EVENTLOOP_DEBUG > 0
			fprintf(stderr, "exit requested, exiting timer expiry loop\n");
			fflush(stderr);
#endif
			break;
		}

		/*
		 *  Expired timer(s) still exist?
		 */

		if (timer_count <= 0) {
			break;
		}
		t = timer_list + timer_count - 1;
		if (t->target > now) {
			break;
		}

		/*
		 *  Move the timer to 'expiring' for the duration of the callback.
		 *  Mark a one-shot timer deleted, compute a new target for an interval.
		 */

		memcpy((void *) &timer_expiring, (void *) t, sizeof(ev_timer));
		memset((void *) t, 0, sizeof(ev_timer));
		timer_count--;
		t = &timer_expiring;

		if (t->oneshot) {
			t->removed = 1;
		} else {
			t->target = now + t->delay;  /* XXX: or t->target + t->delay? */
		}

		/*
		 *  Call timer callback.  The callback can operate on the timer list:
		 *  add new timers, remove timers.  The callback can even remove the
		 *  expired timer whose callback we're calling.  However, because the
		 *  timer being expired has been moved to 'timer_expiring', we don't
		 *  need to worry about the timer's offset changing on the timer list.
		 */

#if DUKTAPE_EVENTLOOP_DEBUG > 0
		fprintf(stderr, "calling user callback for timer id %d\n", (int) t->id);
		fflush(stderr);
#endif

		duk_push_number(ctx, (double) t->id);
		duk_get_prop(ctx, -2);  /* -> [ ... stash eventTimers func ] */
		rc = duk_pcall(ctx, 0 /*nargs*/);  /* -> [ ... stash eventTimers retval ] */
		if (rc != 0) {
#if DUKTAPE_EVENTLOOP_DEBUG > 0
			fprintf(stderr, "timer callback failed for timer %d: %s\n", (int) t->id, duk_to_string(ctx, -1));
			fflush(stderr);
#endif
		}
		duk_pop(ctx);    /* ignore errors for now -> [ ... stash eventTimers ] */

		if (t->removed) {
			/* One-shot timer (always removed) or removed by user callback. */
#if DUKTAPE_EVENTLOOP_DEBUG > 0
			fprintf(stderr, "deleting callback state for timer %d\n", (int) t->id);
			fflush(stderr);
#endif
			duk_push_number(ctx, (double) t->id);
			duk_del_prop(ctx, -2);
		} else {
			/* Interval timer, not removed by user callback.  Queue back to
			 * timer list and bubble to its final sorted position.
			 */
#if DUKTAPE_EVENTLOOP_DEBUG > 0
			fprintf(stderr, "queueing timer %d back into active list\n", (int) t->id);
			fflush(stderr);
#endif
			if (timer_count >= MAX_TIMERS) {
				(void) duk_error(ctx, DUK_ERR_RANGE_ERROR, "out of timer slots");
			}
			memcpy((void *) (timer_list + timer_count), (void *) t, sizeof(ev_timer));
			timer_count++;
			bubble_last_timer();
		}
	}

	memset((void *) &timer_expiring, 0, sizeof(ev_timer));

	duk_pop_2(ctx);  /* -> [ ... ] */
}

static void compact_poll_list(void) {
	int i, j, n;

	/* i = input index
	 * j = output index (initially same as i)
	 */

	n = poll_count;
	for (i = 0, j = 0; i < n; i++) {
		struct pollfd *pfd = poll_list + i;
		if (pfd->fd == 0) {
			/* keep output index the same */
#if DUKTAPE_EVENTLOOP_DEBUG > 0
			fprintf(stderr, "remove pollfd (index %d): fd=%d, events=%d, revents=%d\n",
			        i, pfd->fd, pfd->events, pfd->revents),
			fflush(stderr);
#endif

			continue;
		}
#if DUKTAPE_EVENTLOOP_DEBUG > 0
		fprintf(stderr, "keep pollfd (index %d -> %d): fd=%d, events=%d, revents=%d\n",
		        i, j, pfd->fd, pfd->events, pfd->revents),
		fflush(stderr);
#endif
		if (i != j) {
			/* copy only if indices have diverged */
			memcpy((void *) (poll_list + j), (void *) (poll_list + i), sizeof(struct pollfd));
		}
		j++;
	}

	if (j < poll_count) {
		/* zeroize unused entries for sanity */
		memset((void *) (poll_list + j), 0, (poll_count - j) * sizeof(struct pollfd));
	}

	poll_count = j;
}

duk_ret_t eventloop_run(duk_context *ctx, void *udata) {
	ev_timer *t;
	double now;
	double diff;
	int timeout;
	int rc;
	int i, n;
	int idx_eventloop;
	int idx_fd_handler;

	(void) udata;

	/* The ECMAScript poll handler is passed through EventLoop.fdPollHandler
	 * which c_eventloop.js sets before we come here.
	 */
	duk_push_global_object(ctx);
	duk_get_prop_string(ctx, -1, "EventLoop");
	duk_get_prop_string(ctx, -1, "fdPollHandler");  /* -> [ global EventLoop fdPollHandler ] */
	idx_fd_handler = duk_get_top_index(ctx);
	idx_eventloop = idx_fd_handler - 1;

	for (;;) {
		/*
		 *  Expire timers.
		 */

		expire_timers(ctx);

		/*
		 *  If exit requested, bail out as fast as possible.
		 */

		if (exit_requested) {
#if DUKTAPE_EVENTLOOP_DEBUG > 0
			fprintf(stderr, "exit requested, exiting event loop\n");
			fflush(stderr);
#endif
			break;
		}

		/*
		 *  Compact poll list by removing pollfds with fd == 0.
		 */

		compact_poll_list();

		/*
		 *  Determine poll() timeout (as close to poll() as possible as
		 *  the wait is relative).
		 */

		now = get_now();
		t = find_nearest_timer();
		if (t) {
			diff = t->target - now;
			if (diff < MIN_WAIT) {
				diff = MIN_WAIT;
			} else if (diff > MAX_WAIT) {
				diff = MAX_WAIT;
			}
			timeout = (int) diff;  /* clamping ensures that fits */
		} else {
			if (poll_count == 0) {
#if DUKTAPE_EVENTLOOP_DEBUG > 0
				fprintf(stderr, "no timers and no sockets to poll, exiting\n");
				fflush(stderr);
#endif
				break;
			}
			timeout = (int) MAX_WAIT;
		}

		/*
		 *  Poll for activity or timeout.
		 */

#if DUKTAPE_EVENTLOOP_DEBUG > 0
		fprintf(stderr, "going to poll, timeout %d ms, pollfd count %d\n", timeout, poll_count);
		fflush(stderr);
#endif

		rc = poll(poll_list, poll_count, timeout);
#if DUKTAPE_EVENTLOOP_DEBUG > 0
		fprintf(stderr, "poll rc: %d\n", rc);
		fflush(stderr);
#endif
		if (rc < 0) {
			/* error */
		} else if (rc == 0) {
			/* timeout */
		} else {
			/* 'rc' fds active */
		}

		/*
		 *  Check socket activity, handle all sockets.  Handling is offloaded to
		 *  ECMAScript code (fd + revents).
		 *
		 *  If FDs are removed from the poll list while we're processing callbacks,
		 *  the entries are simply marked unused (fd set to 0) without actually
		 *  removing them from the poll list.  This ensures indices are not
		 *  disturbed.  The poll list is compacted before next poll().
		 */

		n = (rc == 0 ? 0 : poll_count);  /* if timeout, no need to check pollfd */
		for (i = 0; i < n; i++) {
			struct pollfd *pfd = poll_list + i;

			if (pfd->fd == 0) {
				/* deleted, perhaps by previous callback */
				continue;
			}

			if (pfd->revents) {
#if DUKTAPE_EVENTLOOP_DEBUG > 0
				fprintf(stderr, "fd %d has revents: %d\n", (int) pfd->fd, (int) pfd->revents);
				fflush(stderr);
#endif
				duk_dup(ctx, idx_fd_handler);
				duk_dup(ctx, idx_eventloop);
				duk_push_int(ctx, pfd->fd);
				duk_push_int(ctx, pfd->revents);
				rc = duk_pcall_method(ctx, 2 /*nargs*/);
				if (rc) {
#if DUKTAPE_EVENTLOOP_DEBUG > 0
					fprintf(stderr, "fd callback failed for fd %d: %s\n", (int) pfd->fd, duk_to_string(ctx, -1));
					fflush(stderr);
#endif
				}
				duk_pop(ctx);

				pfd->revents = 0;
			}

		}
	}

	duk_pop_n(ctx, 3);

	return 0;
}

static int create_timer(duk_context *ctx) {
	double delay;
	int oneshot;
	int idx;
	int64_t timer_id;
	double now;
	ev_timer *t;

	now = get_now();

	/* indexes:
	 *   0 = function (callback)
	 *   1 = delay
	 *   2 = boolean: oneshot
	 */

	delay = duk_require_number(ctx, 1);
	if (delay < MIN_DELAY) {
		delay = MIN_DELAY;
	}
	oneshot = duk_require_boolean(ctx, 2);

	if (timer_count >= MAX_TIMERS) {
		(void) duk_error(ctx, DUK_ERR_RANGE_ERROR, "out of timer slots");
	}
	idx = timer_count++;
	timer_id = timer_next_id++;
	t = timer_list + idx;

	memset((void *) t, 0, sizeof(ev_timer));
	t->id = timer_id;
	t->target = now + delay;
	t->delay = delay;
	t->oneshot = oneshot;
	t->removed = 0;

	/* Timer is now at the last position; use swaps to "bubble" it to its
	 * correct sorted position.
	 */

	bubble_last_timer();

	/* Finally, register the callback to the global stash 'eventTimers' object. */

	duk_push_global_stash(ctx);
	duk_get_prop_string(ctx, -1, TIMERS_SLOT_NAME);  /* -> [ func delay oneshot stash eventTimers ] */
	duk_push_number(ctx, (double) timer_id);
	duk_dup(ctx, 0);
	duk_put_prop(ctx, -3);  /* eventTimers[timer_id] = callback */

	/* Return timer id. */

	duk_push_number(ctx, (double) timer_id);
#if DUKTAPE_EVENTLOOP_DEBUG > 0
	fprintf(stderr, "created timer id: %d\n", (int) timer_id);
	fflush(stderr);
#endif
	return 1;
}

static int delete_timer(duk_context *ctx) {
	int i, n;
	int64_t timer_id;
	ev_timer *t;
	int found = 0;

	/* indexes:
	 *   0 = timer id
	 */

	timer_id = (int64_t) duk_require_number(ctx, 0);

	/*
	 *  Unlike insertion, deletion needs a full scan of the timer list
	 *  and an expensive remove.  If no match is found, nothing is deleted.
	 *  Caller gets a boolean return code indicating match.
	 *
	 *  When a timer is being expired and its user callback is running,
	 *  the timer has been moved to 'timer_expiring' and its deletion
	 *  needs special handling: just mark it to-be-deleted and let the
	 *  expiry code remove it.
	 */

	t = &timer_expiring;
	if (t->id == timer_id) {
		t->removed = 1;
		duk_push_true(ctx);
#if DUKTAPE_EVENTLOOP_DEBUG > 0
		fprintf(stderr, "deleted expiring timer id: %d\n", (int) timer_id);
		fflush(stderr);
#endif
		return 1;
	}

	n = timer_count;
	for (i = 0; i < n; i++) {
		t = timer_list + i;
		if (t->id == timer_id) {
			found = 1;

			/* Shift elements downwards to keep the timer list dense
			 * (no need if last element).
			 */
			if (i < timer_count - 1) {
				memmove((void *) t, (void *) (t + 1), (timer_count - i - 1) * sizeof(ev_timer));
			}

			/* Zero last element for clarity. */
			memset((void *) (timer_list + n - 1), 0, sizeof(ev_timer));

			/* Update timer_count. */
			timer_count--;

			/* The C state is now up-to-date, but we still need to delete
			 * the timer callback state from the global 'stash'.
			 */

			duk_push_global_stash(ctx);
			duk_get_prop_string(ctx, -1, TIMERS_SLOT_NAME);  /* -> [ timer_id stash eventTimers ] */
			duk_push_number(ctx, (double) timer_id);
			duk_del_prop(ctx, -2);  /* delete eventTimers[timer_id] */

#if DUKTAPE_EVENTLOOP_DEBUG > 0
			fprintf(stderr, "deleted timer id: %d\n", (int) timer_id);
			fflush(stderr);
#endif
			break;
		}
	}

#if DUKTAPE_EVENTLOOP_DEBUG > 0
	if (!found) {
		fprintf(stderr, "trying to delete timer id %d, but not found; ignoring\n", (int) timer_id);
		fflush(stderr);
	}
#endif

	duk_push_boolean(ctx, found);
	return 1;
}

static int listen_fd(duk_context *ctx) {
	int fd = duk_require_int(ctx, 0);
	int events = duk_require_int(ctx, 1);
	int i, n;
	struct pollfd *pfd;

#if DUKTAPE_EVENTLOOP_DEBUG > 0
	fprintf(stderr, "listen_fd: fd=%d, events=%d\n", fd, events);
	fflush(stderr);
#endif
	/* events == 0 means stop listening to the FD */

	n = poll_count;
	for (i = 0; i < n; i++) {
		pfd = poll_list + i;
		if (pfd->fd == fd) {
#if DUKTAPE_EVENTLOOP_DEBUG > 0
			fprintf(stderr, "listen_fd: fd found at index %d\n", i);
			fflush(stderr);
#endif
			if (events == 0) {
				/* mark to-be-deleted, cleaned up by next poll */
				pfd->fd = 0;
			} else {
				pfd->events = events;
			}
			return 0;
		}
	}

	/* not found, append to list */
#if DUKTAPE_EVENTLOOP_DEBUG > 0
	fprintf(stderr, "listen_fd: fd not found on list, add new entry\n");
	fflush(stderr);
#endif

	if (poll_count >= MAX_FDS) {
		(void) duk_error(ctx, DUK_ERR_ERROR, "out of fd slots");
	}

	pfd = poll_list + poll_count;
	pfd->fd = fd;
	pfd->events = events;
	pfd->revents = 0;
	poll_count++;

	return 0;
}

static int request_exit(duk_context *ctx) {
	(void) ctx;
	exit_requested = 1;
	return 0;
}

static duk_function_list_entry eventloop_funcs[] = {
	{ "createTimer", create_timer, 3 },
	{ "deleteTimer", delete_timer, 1 },
	{ "listenFd", listen_fd, 2 },
	{ "requestExit", request_exit, 0 },
	{ NULL, NULL, 0 }
};

void eventloop_register(duk_context *ctx) {
	memset((void *) timer_list, 0, MAX_TIMERS * sizeof(ev_timer));
	memset((void *) &timer_expiring, 0, sizeof(ev_timer));
	memset((void *) poll_list, 0, MAX_FDS * sizeof(struct pollfd));

	/* Set global 'EventLoop'. */
	duk_push_global_object(ctx);
	duk_push_object(ctx);
	duk_put_function_list(ctx, -1, eventloop_funcs);
	duk_put_prop_string(ctx, -2, "EventLoop");
	duk_pop(ctx);

	/* Initialize global stash 'eventTimers'. */
	duk_push_global_stash(ctx);
	duk_push_object(ctx);
	duk_put_prop_string(ctx, -2, TIMERS_SLOT_NAME);
	duk_pop(ctx);
}