|
|
|
|
@ -1,6 +1,6 @@
|
|
|
|
|
#lang racket/base |
|
|
|
|
|
|
|
|
|
;; syncp and syncp* produce promises that are fulfilled upon the |
|
|
|
|
;; sync/pr and sync/pr* produce promises that are fulfilled upon the |
|
|
|
|
;; completion of a synchronizable event. |
|
|
|
|
;; They set up a separate thread which watches for synchronization. |
|
|
|
|
|
|
|
|
|
@ -8,11 +8,11 @@
|
|
|
|
|
(submod "../core.rkt" np-extern) |
|
|
|
|
racket/match) |
|
|
|
|
|
|
|
|
|
(provide syncp* syncp) |
|
|
|
|
(provide sync/pr* sync/pr) |
|
|
|
|
|
|
|
|
|
;; This one is also cancelable, but it returns two values to its continuation. |
|
|
|
|
;; It only accepts one argument, so you'll have to choice-evt yourself if need be. |
|
|
|
|
(define (syncp* evt #:timeout [timeout #f]) |
|
|
|
|
(define (sync/pr* evt #:timeout [timeout #f]) |
|
|
|
|
(define-values (promise resolver) |
|
|
|
|
(spawn-promise-values)) |
|
|
|
|
(define cancel-semaphore (make-semaphore)) |
|
|
|
|
@ -34,15 +34,15 @@
|
|
|
|
|
(values promise cancel!)) |
|
|
|
|
|
|
|
|
|
;; No way to cancel, just returns the promise |
|
|
|
|
(define (syncp #:timeout [timeout #f] . evts) |
|
|
|
|
(define (sync/pr #:timeout [timeout #f] . evts) |
|
|
|
|
(match evts |
|
|
|
|
[(list evt) |
|
|
|
|
(define-values (promise _cancel) |
|
|
|
|
(syncp* evt #:timeout timeout)) |
|
|
|
|
(sync/pr* evt #:timeout timeout)) |
|
|
|
|
promise] |
|
|
|
|
[evts |
|
|
|
|
(define-values (promise _cancel) |
|
|
|
|
(syncp* (apply choice-evt evts) #:timeout timeout)) |
|
|
|
|
(sync/pr* (apply choice-evt evts) #:timeout timeout)) |
|
|
|
|
promise])) |
|
|
|
|
|
|
|
|
|
(module+ test |
|
|
|
|
@ -54,7 +54,7 @@
|
|
|
|
|
(let ([result-ch (make-channel)] |
|
|
|
|
[incoming-ch (make-channel)]) |
|
|
|
|
(vat-run |
|
|
|
|
(on (syncp incoming-ch) |
|
|
|
|
(on (sync/pr incoming-ch) |
|
|
|
|
(lambda (val) |
|
|
|
|
(channel-put result-ch `(success ,val))) |
|
|
|
|
#:catch |
|
|
|
|
@ -62,7 +62,7 @@
|
|
|
|
|
(channel-put result-ch `(failure ,err))))) |
|
|
|
|
(channel-put incoming-ch 'hello-there) |
|
|
|
|
(test-equal? |
|
|
|
|
"syncp simple success case" |
|
|
|
|
"sync/pr simple success case" |
|
|
|
|
(sync/timeout 1 result-ch) |
|
|
|
|
'(success (hello-there)))) |
|
|
|
|
|
|
|
|
|
@ -71,7 +71,7 @@
|
|
|
|
|
(define cancel |
|
|
|
|
(vat-run |
|
|
|
|
(define-values (p cancel) |
|
|
|
|
(syncp* incoming-ch)) |
|
|
|
|
(sync/pr* incoming-ch)) |
|
|
|
|
(on p |
|
|
|
|
(lambda (val) |
|
|
|
|
(channel-put result-ch `(success ,val))) |
|
|
|
|
@ -81,6 +81,6 @@
|
|
|
|
|
cancel)) |
|
|
|
|
(cancel) |
|
|
|
|
(test-equal? |
|
|
|
|
"syncp* canceled" |
|
|
|
|
"sync/pr* canceled" |
|
|
|
|
(sync/timeout 1 result-ch) |
|
|
|
|
'(failure canceled)))) |