Skip to content

Instantly share code, notes, and snippets.

@danking
Created November 14, 2013 19:29
Show Gist options
  • Select an option

  • Save danking/7472839 to your computer and use it in GitHub Desktop.

Select an option

Save danking/7472839 to your computer and use it in GitHub Desktop.
#lang racket
(define-syntax custodian-protect
(syntax-rules ()
((custodian-protect main-body ...)
(let ((protection #f)
(original #f))
(dynamic-wind (lambda ()
(set! original (current-custodian))
(set! protection (make-custodian original))
(current-custodian protection))
(lambda () main-body ...)
(lambda ()
(custodian-shutdown-all protection)
(current-custodian original)))))))
(define kont
(custodian-protect
(call/cc (lambda (k)
(thread (lambda () ((lambda (x) (begin (printf "Hey there")(x x)))
(lambda (x) (begin (printf "Hey there") (sleep 1)(x x))))))
(custodian-protect
(define x (open-input-file "/tmp/foo"))
(define
port
(call/cc (lambda (k2)
(k (list k2 x)))))
(display (read port)))
(sleep 5)(k #f)))))
(define kontinuation (first kont))
(define port (second kont))
(kontinuation port)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment