From: Adam T. Dingle
Subject: SUMMARY: Timer interrupts in Scheme
Date: 
Message-ID: <DINGLE.91Oct23121512@codornicesparc.Berkeley.EDU>
Thanks to everyone who responded to my inquiry about timer interrupts
in Scheme.  The responses I received generally fell into two
categories: those suggesting that timer interrupts could be simulated
by using macros to redefine Scheme primitives such as lambda, and
those discussing how operating system signals can be used in actual
implementations such as MIT Scheme.

Regarding the simulation of timer interrupts by using macros, Bob Hieb
(····@gable.cs.indiana.edu) wrote:

>Kent Dybvig and myself published a description of implementing engines
>(as they exist in Chez Scheme as described in Dybvig's book "The Scheme
>Programming Language").  See "Engines from Continuations" in Computer
>Languages, volume 14, number 2, 1989, pages 109--123.
>
>In the absence of a timer you can force a counter to be decremented on
>procedure calls, or whenever you think appropriate, and trap when it
>hits zero.  Although this technique is slow compared to a built-in
>timer, and may not accurately reflect work done by system procedures,
>it is often adequate for experimental purposes.
>
>The simplest way to do this is via the expander, by redefining "lambda"
>and perhaps "set!", and whatever other core forms you find necessary.
>Doing so requires a relatively powerful macro system.  I think either
>"syntactic closures" or "expansion-passing-style" macros will do the
>trick.  MIT Scheme I believe uses the former.
>
>Bob Hieb

A number of people sent mail or news discussing MIT Scheme.  Guillermo J.
Rozas (····@zurich.ai.mit.edu) posted the following to the net:

>MIT Scheme has primitive support for timer interrupts, but it is 
>not documented.  The following may be of assistance:
>
>(make-primitive-procedure 'process-timer-set)
>(make-primitive-procedure 'real-timer-set)
>
>both return procedures of two arguments, a time interval to the first
>interrupt, and a period for subsequent interrupts, both in
>milliseconds.
>
>(make-primitive-procedure 'real-timer-clear)
>(make-primitive-procedure 'process-timer-clear)
>
>return procedures of no arguments to clear the corresponding
>interrupts.
>
>The "global" variable TIMER-INTERRUPT should have a procedure of no
>arguments as its value.  This procedure will be invoked when a timer
>interrupt (of either kind) is raised.

Chris Hanson (···@altdorf.ai.mit.edu) sent some sample code:

>It can be done in MIT Scheme (release 7.1):
>
>(define real-timer-set
>  (make-primitive-procedure 'real-timer-set))
>
>(define real-timer-clear
>  (make-primitive-procedure 'real-timer-clear))
>
>(define clear-interrupts!
>  (make-primitive-procedure clear-interrupts!))
>
>(define (with-timer-interrupts interval handler thunk)
>  (fluid-let ((timer-interrupt handler))
>    (dynamic-wind (lambda ()
>		    (real-timer-set interval interval))
>		  thunk
>		  (lambda ()
>		    (real-timer-clear)
>		    (clear-interrupts! interrupt-bit/timer)))))

Arthur Gleckler (······@altdorf.ai.mit.edu) wrote:

>I've written a time-sliced multi-process program for MIT Scheme, just
>as a hack, that you might take a look at to see how to use timer
>interrupts.  Also, ······@astro.psu.edu (Andrew Wilcox) has written a
>full package supporting concurrent processes in Scheme, and asked me
>for the same information you've requested.  After I sent him my code,
>he replied that he had incorporated it into his system so that he now
>supports time-sliced multi-processing.  You can send mail to him
>directly for more information, but I've placed his package and my
>code on altdorf.ai.mit.edu for anonymous ftp.  The files are in
>pub/arthur/ in the files threads.tar.Z and slice.scm.

Andrew Wilcox (······@astro.psu.edu), mentioned in the above note,
wrote:

>I have timer interrupts running in MIT C-Scheme in my Thread library.
>The only problem is that the MIT C-Scheme library is not locked
>against interrupts, which means that calling library functions might
>break if you don't lock them yourself.  In particular, you have to
>lock I/O.  I haven't done much with MIT C-Scheme myself because all I
>have is a Sparc, and they don't have a compiler for that machine.

Finally, Wayne Allen (·······················@mcc.com) noted:

>The Scheme->C system from DEC Western Research Labs supports this. You
>can ftp it from gatekeeper.dec.com.

Thanks again to everyone who responded.

-Adam Dingle
 ······@cs.berkeley.edu