## Larceny Note #9: The I/O system

Lars T Hansen / November 14, 1997

This note is being written

## The I/O architecture

The I/O system is strongly influenced by the Modula-3 I/O system presented in Greg Nelson's book, Systems Programming in Modula-3. The I/O system is designed with performance and user-extensibity in mind.

Needs to be documented.

See `Lib/iosys.sch`, `Lib/fileio.sch`, `Lib/conio.sch`, `Lib/stdio.sch`, and (for the low-level stuff) `Lib/unix.sch`.

## Thread-aware I/O

To support thread-aware I/O, we need two things:

- Mutual exclusion in access to the I/O structures.
- Blocking I/O that is compatible with the threads system.

Mutual exclusion is not hard; the procedures in `Lib/stdio.sch` can be wrapped in a `without-interrupts` form. The lock should probably be a public part of the port structure so that it's possible for (system) code to acquire it once and then call low-level primitives for better performance.

Since the threads system is (currently) written in Scheme on top of continuation, blocking system calls are no good. Instead, I/O system calls that may block indefinitely must be avoided.

The right thing to do seems to consider two subtypes of I/O ports, along the lines of the Modula-3 I/O system. A port is classified either as intermittent or not. Intermittent ports may have to wait an unbounded amount of time before input is available or output is accepted. Currently, the only intermittent ports are console I/O ports, but when the extensible I/O system goes public, we'll have sockets pretty quickly.

Intermittent ports havs the following unique attribute: the underlying read and write methods return `would-block` if no work was accomplished (no input was ready or no output would be accepted). If that token is returned to the fill or flush methods, then the operation on the port must block until the port is ready. This blocking can be done either by polling or by interrupts. If I/O interrupts are available, then the I/O system must enable them and set up an I/O event handler. If not, the I/O system must register an I/O poll procedure for the port as a periodic system task. In either event, the I/O system will then block the thread on a condition variable that will be signalled by the ready handler, whichever method is used.

I think that the actual underlying mechanism chosen for unblocking threads can and should be independent of the Scheme I/O system. This is possible if the I/O system supports an installable "ioblock" handler that it will call to wait for I/O on a port. System code will then install the correct ioblock handler for the I/O event system chosen on the particular platform.

$Id: note9-iosys.html 87 1998-11-25 14:38:41Z lth $
larceny@ccs.neu.edu