Christian Jaeger wrote:
I hope that by calling ##continuation-graft-with-winding you could throw an exception in the context of that thread (not sure, though)).
Ok, it didn't let me rest and I tested it out. Seems my hope is void: you can call a continuation from a foreign thread, but it is then not executed in the original thread, but in the current thread instead (the original thread continues normal execution). That makes sense, of course, since that allows one to transfer 'agents' to other threads (just what Termite is doing).
So you have to find out how to evaluate code in the context of another thread (I'd examine the code implementing the default signal handler), or let Marc tell you.
Christian.
(define cont #f);; note: this is to hold a raw continuation datastructure, not wrapped in a closure
(define-macro (%thread . body) `(thread-start! (make-thread (lambda () ,@body))))
(define t1 (%thread (let ((t (current-thread))) (println "thread t1, " t ", started") (##continuation-capture (lambda (c) (set! cont c) (let lp () (thread-sleep! 2) (println "thread " (current-thread) " is awake") (lp)))) (println "thread t1, " t " (" (current-thread) ") ending"))))
(define t2 (%thread (let ((t (current-thread))) (println "thread t2, " t ", started") (thread-sleep! 5) (##continuation-graft ;; hm, ##continuation-graft-with-winding does not exist anymore in Gambit v4.0.1 cont (lambda () ;;(error "an error from t2 in the context of t1?") "a value")) (println "thread t2, " t " (" (current-thread) ") ending"))))
with the error statement uncommented:
(load "threadcont")
"/tmp/chris/threadcont.scm"
thread t1, #<thread #2>, started
thread t2, #<thread #3>, started thread #<thread #2> is awake thread #<thread #2> is awake thread #<thread #2> is awake thread #<thread #2> is awake thread #<thread #2> is awake thread #<thread #2> is awake thread #<thread #2> is awake thread #<thread #2> is awake (thread-join! t2) ;; returns immediately *** ERROR IN (console)@2.1 -- Uncaught exception: #<error-exception #4> (thread-join! '#<thread #3>) 1> thread #<thread #2> is awake thread #<thread #2> is awake (thread-join! t1) ;; 'hangs' thread #<thread #2> is awake thread #<thread #2> is awake <---(hitting ctl-c) ------------- REPL is now in #<thread #2> ------------- *** INTERRUPTED IN ##thread-sleep!
with the "a value" return:
(load "threadcont")
"/tmp/chris/threadcont.scm"
thread t1, #<thread #2>, started
thread t2, #<thread #3>, started thread #<thread #2> is awake thread #<thread #2> is awake thread t1, #<thread #2> (#<thread #3>) ending thread #<thread #2> is awake thread #<thread #2> is awake thread #<thread #2> is awake (thread-join! t2) ;; returns immediately
thread #<thread #2> is awake
thread #<thread #2> is awake (thread-join! t1) ;; 'hangs' thread #<thread #2> is awake thread #<thread #2> is awake thread #<thread #2> is awake thread #<thread #2> is awake <---- (hitting ctl-c) ------------- REPL is now in #<thread #2> ------------- *** INTERRUPTED IN ##thread-sleep!
(thread-join! t2) ;; the void value, of course, since "a value" is lost and the result of the "ending" println returned