2001-03-23 07:45:31 -05:00
|
|
|
; Copyright (c) 1999-2001 by Martin Gasbichler. See file COPYING.
|
|
|
|
|
|
|
|
;;; Functional event system.
|
|
|
|
;;; System by Olin Shivers, implementation by Martin Gasbichler
|
|
|
|
|
|
|
|
(define-record-type sigevent :sigevent
|
|
|
|
(really-make-sigevent type next)
|
|
|
|
sigevent?
|
|
|
|
(type sigevent-type set-sigevent-type!)
|
|
|
|
(next sigevent-next set-sigevent-next!))
|
|
|
|
|
|
|
|
(define (make-sigevent type)
|
|
|
|
(really-make-sigevent type #f))
|
|
|
|
|
|
|
|
(define empty-sigevent (make-sigevent #f))
|
|
|
|
|
|
|
|
(define *most-recent-sigevent* empty-sigevent)
|
|
|
|
|
|
|
|
(define (most-recent-sigevent) *most-recent-sigevent*)
|
|
|
|
|
|
|
|
(define sigevent-thread-queue #f)
|
|
|
|
|
|
|
|
;Wait for an sigevent of a certain type.
|
|
|
|
(define (rts-next-sigevent pre-sigevent set type-in-set?)
|
|
|
|
(with-interrupts-inhibited
|
|
|
|
(lambda ()
|
|
|
|
(let lp ((pre-sigevent pre-sigevent))
|
|
|
|
(let ((sigevent (sigevent-next pre-sigevent)))
|
|
|
|
(if sigevent
|
|
|
|
(if (type-in-set? (sigevent-type sigevent) set)
|
|
|
|
sigevent
|
|
|
|
(lp sigevent))
|
2002-08-16 10:11:50 -04:00
|
|
|
(begin (block-on-queue sigevent-thread-queue)
|
2001-03-23 07:45:31 -05:00
|
|
|
(lp pre-sigevent))))))))
|
|
|
|
|
|
|
|
; same as above, but don't block
|
|
|
|
(define (rts-next-sigevent/no-wait pre-sigevent set type-in-set?)
|
|
|
|
(let ((sigevent (sigevent-next pre-sigevent)))
|
|
|
|
(if sigevent
|
|
|
|
(if (type-in-set? (sigevent-type sigevent) set)
|
|
|
|
sigevent
|
|
|
|
(rts-next-sigevent/no-wait (sigevent-next sigevent)
|
|
|
|
set
|
|
|
|
type-in-set?))
|
|
|
|
#f)))
|
|
|
|
|
|
|
|
|
|
|
|
;Called when the interrupt actually happened.
|
|
|
|
;;; TODO w-i-i is problaly not necessary since they're off already
|
|
|
|
(define (register-interrupt type)
|
|
|
|
(let ((waiters (with-interrupts-inhibited
|
|
|
|
(lambda ()
|
|
|
|
(set-sigevent-next! *most-recent-sigevent* (make-sigevent type))
|
|
|
|
(set! *most-recent-sigevent* (sigevent-next *most-recent-sigevent*))
|
2002-08-16 10:11:50 -04:00
|
|
|
(do ((waiters '() (cons (maybe-dequeue-thread! sigevent-thread-queue)
|
2001-03-23 07:45:31 -05:00
|
|
|
waiters)))
|
|
|
|
((thread-queue-empty? sigevent-thread-queue)
|
|
|
|
waiters))))))
|
|
|
|
(for-each make-ready waiters)))
|
|
|
|
|
2003-02-25 03:11:32 -05:00
|
|
|
|
|
|
|
;;; Records whether the sigevent system is running.
|
|
|
|
;;; If set to #f we ignore threads waiting for a sigevent.
|
|
|
|
(define sigevents-running? #f)
|
|
|
|
|
2001-03-23 07:45:31 -05:00
|
|
|
;;; has to be called with interrupts disabled
|
|
|
|
(define (waiting-for-sigevent?)
|
2003-02-25 03:11:32 -05:00
|
|
|
(if sigevents-running?
|
|
|
|
(not (thread-queue-empty? sigevent-thread-queue))
|
|
|
|
#f))
|
2001-03-23 07:45:31 -05:00
|
|
|
|
2003-02-25 03:11:32 -05:00
|
|
|
(define (with-sigevents thunk)
|
2002-08-16 10:11:50 -04:00
|
|
|
(set! sigevent-thread-queue (make-queue))
|
2001-03-23 07:45:31 -05:00
|
|
|
(set-interrupt-handler! (enum interrupt os-signal)
|
2002-06-10 04:46:08 -04:00
|
|
|
(lambda (type enabled-interrupts)
|
2003-02-25 03:11:32 -05:00
|
|
|
; type is already set in the unix signal handler
|
2001-03-23 07:45:31 -05:00
|
|
|
(register-interrupt type)))
|
|
|
|
(set-interrupt-handler! (enum interrupt keyboard)
|
|
|
|
(lambda (enabled-interrupts)
|
|
|
|
(register-interrupt (enum interrupt keyboard))))
|
2003-02-25 03:11:32 -05:00
|
|
|
(dynamic-wind
|
|
|
|
(lambda ()
|
|
|
|
(set! sigevents-running? #t))
|
|
|
|
thunk
|
|
|
|
(lambda ()
|
|
|
|
(set! sigevents-running? #f))))
|
|
|
|
|
|
|
|
|
2001-03-23 07:45:31 -05:00
|
|
|
;;; the vm uses the timer for the scheduler
|
|
|
|
(define (schedule-timer-interrupt! msec)
|
|
|
|
(spawn (lambda ()
|
|
|
|
(sleep msec)
|
|
|
|
(register-interrupt (enum interrupt alarm)))))
|
|
|
|
|