• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..01-Sep-2021-

README.rstH A D01-Sep-20212.6 KiB7753

c_eventloop.cH A D01-Sep-202115 KiB619391

fileio.cH A D01-Sep-20211 KiB7049

main.cH A D01-Sep-20215.3 KiB257197

ncurses.cH A D01-Sep-20212.1 KiB10675

poll.cH A D01-Sep-20212.5 KiB11279

socket.cH A D01-Sep-20215.7 KiB287233

README.rst

1==================
2Eventloop examples
3==================
4
5Overview and usage
6==================
7
8A few examples on how an event loop can be implemented with Duktape, mainly
9illlustrating how the Duktape interface works (not how event loops should be
10built otherwise).
11
12To test (Linux only, perhaps other Unix)::
13
14  $ make
15  $ ./evloop curses-timers.js     # run with Ecmascript eventloop
16  $ ./evloop -c curses-timers.js  # run with C eventloop
17
18Implementation approaches
19=========================
20
21There are several approaches to implementation timers.  Here we demonstrate
22two main approaches:
23
241. Using a C eventloop which calls into Javascript.  All the event loop state
25   like timers, sockets, etc, is held in C structures.
26   (See ``c_eventloop.c`` and ``c_eventloop.js``.)
27
282. Using an Ecmascript eventloop which never returns.  All the event loop state
29   can be managed with Ecmascript code instead of C structures.  The Ecmascript
30   eventloop calls a Duktape/C helper to do the lowest level poll() call.
31   (See ``ecma_eventloop.js``.)
32
33Services provided
34=================
35
36The event loop API provided by both examples is the same, and includes:
37
38* Timers: setTimeout, clearTimeout, setInterval, clearInterval
39
40* Sockets: simple network sockets
41
42In addition there are a few synchronous API bindings which are not event loop
43related:
44
45* File I/O
46
47* Curses, for doing beautiful character graphics
48
49Limitations
50===========
51
52This is **not** a production quality event loop.  This is on purpose, to
53keep the example somewhat simple.  Some shortcomings include:
54
55* A production quality event loop would track its internal state (active
56  timers and sockets) much more efficiently.  In general memory usage and
57  code footprint can be reduced.
58
59* Buffer churn caused by allocating a new buffer for every socket read
60  should be eliminated by reusing buffers where appropriate.  Although
61  churn doesn't increase memory footprint with reference counting, it
62  is slower than reusing buffers and might increase memory fragmentation.
63
64* There is no way to suspend reading or writing in the example.  Adding
65  them is straightforward: the poll set needs to be managed dynamically.
66
67* The example uses poll() while one should use epoll() on Linux, kqueue()
68  on BSD systems, etc.
69
70* Timers are not very accurate, e.g. setInterval() does not try to guarantee
71  a steady schedule.  Instead, the next interval is scheduled after the
72  current callback has finished.  This is not the best behavior for some
73  environments, but avoids bunching callbacks.
74
75* Error handling is mostly missing.  Debug prints don't interact well
76  with curses.
77