Skip to content

Instantly share code, notes, and snippets.

@tonyg
Created October 22, 2011 15:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tonyg/1306106 to your computer and use it in GitHub Desktop.
Save tonyg/1306106 to your computer and use it in GitHub Desktop.
Sketch of exit-status preserving threads
#lang racket/base
;; Standard Thread
(provide exit-status?
exit-status-exception?
exit-status-value
current-thread-exit-status
exit-status-evt
standard-thread)
(struct exit-status (thread
[exception? #:mutable]
[value #:mutable]
ready))
(define *current-thread-exit-status* (make-parameter #f))
(define (current-thread-exit-status)
(define v (*current-thread-exit-status*))
(if (exit-status? v)
(if (eq? (current-thread) (exit-status-thread v))
v
(begin (*current-thread-exit-status* #f)
#f))
#f))
(define (exit-status-evt es)
(wrap-evt (semaphore-peek-evt (exit-status-ready es))
(lambda (dummy) es)))
(define (fill-exit-status! es exn? v)
(set-exit-status-exception?! es exn?)
(set-exit-status-value! es v)
(semaphore-post (exit-status-ready es))
v)
(define (call-capturing-exit-status thunk)
(define es (exit-status (current-thread) #f #f (make-semaphore 0)))
(parameterize ((*current-thread-exit-status* es))
(with-handlers
((exn? (lambda (e)
(raise (fill-exit-status! es #t e)))))
(fill-exit-status! es #f (thunk)))))
(define (standard-thread thunk)
(thread (lambda ()
(call-capturing-exit-status thunk))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment