generalize coroutines so the pause function optionally takes
into account the amount of cpu time used
This commit is contained in:
parent
6ddf433c3e
commit
134144a4ce
|
@ -1,5 +1,6 @@
|
||||||
#lang racket/base
|
#lang racket/base
|
||||||
(require racket/contract)
|
(require racket/contract
|
||||||
|
(for-syntax racket/base))
|
||||||
|
|
||||||
(provide coroutine)
|
(provide coroutine)
|
||||||
(provide
|
(provide
|
||||||
|
@ -8,30 +9,57 @@
|
||||||
[coroutine-runnable? (-> coroutine? boolean?)]
|
[coroutine-runnable? (-> coroutine? boolean?)]
|
||||||
[coroutine-value (-> coroutine? any/c)]))
|
[coroutine-value (-> coroutine? any/c)]))
|
||||||
|
|
||||||
(define-syntax-rule
|
(define-syntax (coroutine stx)
|
||||||
(coroutine pause-id first-id exp1 exp2 ...)
|
(define-values (timeout more-stx)
|
||||||
(coroutine/proc (λ (pause-id first-id) exp1 exp2 ...)))
|
(syntax-case stx ()
|
||||||
|
[(_ #:at-least msec-expr . more)
|
||||||
|
(values #'msec-expr #'more)]
|
||||||
|
[(_ . more) (values #'#f #'more)]))
|
||||||
|
(syntax-case more-stx ()
|
||||||
|
[(pause-id first-id exp1 exp2 ...)
|
||||||
|
#`(coroutine/proc #,timeout (λ (pause-id first-id) exp1 exp2 ...))]))
|
||||||
|
|
||||||
(struct coroutine ([run-proc #:mutable] [val #:mutable] tag)
|
(struct coroutine ([run-proc #:mutable]
|
||||||
|
[val #:mutable]
|
||||||
|
tag
|
||||||
|
[last-start #:mutable]
|
||||||
|
expiry)
|
||||||
#:omit-define-syntaxes
|
#:omit-define-syntaxes
|
||||||
#:extra-constructor-name
|
#:extra-constructor-name
|
||||||
make-coroutine)
|
make-coroutine)
|
||||||
|
|
||||||
(define (coroutine/proc cproc)
|
(define (coroutine/proc expiry cproc)
|
||||||
(define tag (make-continuation-prompt-tag 'coroutine))
|
(define tag (make-continuation-prompt-tag 'coroutine))
|
||||||
(define (pauser)
|
(define (pauser)
|
||||||
(call-with-composable-continuation
|
(define actually-pause?
|
||||||
(λ (k) (abort-current-continuation tag k))
|
(cond
|
||||||
tag))
|
[(coroutine-last-start the-coroutine)
|
||||||
(make-coroutine (λ (first-val) (values (cproc pauser first-val) #t))
|
=>
|
||||||
#f
|
(λ (start-time)
|
||||||
tag))
|
(define now (get-time))
|
||||||
|
((- now start-time) . >= . (coroutine-expiry the-coroutine)))]
|
||||||
|
[else #t]))
|
||||||
|
(when actually-pause?
|
||||||
|
(call-with-composable-continuation
|
||||||
|
(λ (k) (abort-current-continuation tag k))
|
||||||
|
tag)))
|
||||||
|
(define the-coroutine
|
||||||
|
(make-coroutine (λ (first-val) (values (cproc pauser first-val) #t))
|
||||||
|
#f
|
||||||
|
tag
|
||||||
|
#f
|
||||||
|
expiry))
|
||||||
|
the-coroutine)
|
||||||
|
|
||||||
|
(define (get-time) (current-process-milliseconds (current-thread)))
|
||||||
|
|
||||||
(define (coroutine-run a-coroutine val)
|
(define (coroutine-run a-coroutine val)
|
||||||
(cond
|
(cond
|
||||||
[(coroutine-run-proc a-coroutine)
|
[(coroutine-run-proc a-coroutine)
|
||||||
=>
|
=>
|
||||||
(λ (proc)
|
(λ (proc)
|
||||||
|
(when (coroutine-expiry a-coroutine)
|
||||||
|
(set-coroutine-last-start! a-coroutine (get-time)))
|
||||||
(define-values (res done?)
|
(define-values (res done?)
|
||||||
(call-with-continuation-prompt
|
(call-with-continuation-prompt
|
||||||
(λ () (proc val))
|
(λ () (proc val))
|
||||||
|
@ -116,5 +144,22 @@
|
||||||
(check-equal? (with-stdout (λ () (coroutine-run c2 2)))
|
(check-equal? (with-stdout (λ () (coroutine-run c2 2)))
|
||||||
(list #t "1 => 2\n"))
|
(list #t "1 => 2\n"))
|
||||||
(check-equal? (coroutine-value c2)
|
(check-equal? (coroutine-value c2)
|
||||||
2))
|
2)
|
||||||
|
|
||||||
|
(check-equal?
|
||||||
|
(let ([c (coroutine
|
||||||
|
#:at-least 100
|
||||||
|
pause first
|
||||||
|
(pause)
|
||||||
|
(printf "hi"))])
|
||||||
|
(with-stdout (λ () (coroutine-run c 'whatever))))
|
||||||
|
(list #t "hi"))
|
||||||
|
|
||||||
|
(check-equal?
|
||||||
|
(let ([c (coroutine
|
||||||
|
#:at-least 0
|
||||||
|
pause first
|
||||||
|
(pause)
|
||||||
|
(printf "hi"))])
|
||||||
|
(with-stdout (λ () (coroutine-run c 'whatever))))
|
||||||
|
(list #f "")))
|
||||||
|
|
Loading…
Reference in New Issue
Block a user