(declare (usual-integrations))
\f
+(define (carefully procedure if-overflow if-timeout)
+ (let ((thread #f)
+ (mutex (make-thread-mutex))
+ (condvar (make-condition-variable))
+ (gc-env (->environment '(runtime garbage-collector))))
+ (define (start-it)
+ (with-thread-mutex-lock mutex
+ (lambda ()
+ (do () (thread)
+ (condition-variable-wait! condvar mutex))))
+ (let ((default/stack-overflow (access default/stack-overflow gc-env)))
+ (define (give-up)
+ (if (eq? thread (current-thread))
+ (exit-current-thread (if-overflow))
+ (default/stack-overflow)))
+ (call-with-current-continuation
+ (lambda (abort)
+ (fluid-let (((access hook/stack-overflow gc-env)
+ (lambda () (within-continuation abort give-up))))
+ (exit-current-thread (procedure)))))))
+ (define (stop-it)
+ (assert thread)
+ (signal-thread-event thread
+ (lambda ()
+ (exit-current-thread (if-timeout)))))
+ (let ((t (create-thread #f start-it)))
+ (with-thread-mutex-lock mutex
+ (lambda ()
+ (set! thread t)
+ (condition-variable-broadcast! condvar))))
+ (let ((result #f))
+ (define (done-it thread* value)
+ (assert (eq? thread* thread))
+ (set! result value))
+ (join-thread thread done-it)
+ (let ((timer))
+ (dynamic-wind
+ (lambda () (set! timer (register-timer-event 1000 stop-it)))
+ (lambda () (do () (result) (suspend-current-thread)))
+ (lambda () (deregister-timer-event (set! timer))))))))
+
+(define-test 'delay-force-loop
+ (lambda ()
+ (expect-failure
+ (lambda ()
+ (assert-error
+ (lambda ()
+ (carefully (lambda ()
+ (define p (delay-force p))
+ (force p))
+ (lambda () 'stack-overflow)
+ (lambda () 'timeout))))))))
+
(define-test 'force-force-delay-delay
(lambda ()
(assert-eqv (force (force (delay (delay 0)))) 0)))