diff options
-rw-r--r-- | chickadee/scripting/channel.scm | 22 | ||||
-rw-r--r-- | doc/api.texi | 17 |
2 files changed, 31 insertions, 8 deletions
diff --git a/chickadee/scripting/channel.scm b/chickadee/scripting/channel.scm index 4d8583f..7ec11ce 100644 --- a/chickadee/scripting/channel.scm +++ b/chickadee/scripting/channel.scm @@ -23,7 +23,9 @@ #:use-module (chickadee scripting script) #:export (make-channel channel? + channel-get! channel-get + channel-put! channel-put)) ;; A very simplified notion of channels compared to guile-fibers. In @@ -55,18 +57,30 @@ (get-cont data) (put-cont))))))) +(define (channel-get! channel proc) + "Asynchronously retrieve a value from CHANNEL and call PROC with +that value." + (enqueue! (channel-get-queue channel) proc) + (maybe-deliver channel)) + (define (channel-get channel) "Retrieve a value from CHANNEL. The current script suspends until a value is available." (yield (lambda (cont) - (enqueue! (channel-get-queue channel) cont) - (maybe-deliver channel)))) + (channel-get! channel cont)))) + +(define noop (lambda () #t)) + +(define* (channel-put! channel data #:optional (thunk noop)) + "Asynchronously send DATA to CHANNEL and call THUNK after it has +been received." + (enqueue! (channel-put-queue channel) (cons data thunk)) + (maybe-deliver channel)) (define (channel-put channel data) "Send DATA to CHANNEL. The current script suspends until another script is available to retrieve the value." (yield (lambda (cont) - (enqueue! (channel-put-queue channel) (cons data cont)) - (maybe-deliver channel)))) + (channel-put! channel data cont)))) diff --git a/doc/api.texi b/doc/api.texi index 4c34cf4..c79af20 100644 --- a/doc/api.texi +++ b/doc/api.texi @@ -1934,10 +1934,6 @@ procedure @var{handler}. Wait @var{duration} before resuming the current script. @end deffn -@deffn {Procedure} channel-get @var{channel} -Wait for a message from @var{channel}. -@end deffn - @deffn {Syntax} forever @var{body} @dots{} Evaluate @var{body} in an endless loop. @end deffn @@ -2014,3 +2010,16 @@ until a value is available. Send @var{data} to @var{channel}. The current script suspends until another script is available to retrieve the value. @end deffn + +A low-level API also exists for using channels outside of a script via +callback procedures: + +@deffn {Procedure} channel-get! @var{channel} @var{proc} +Asynchronously retrieve a value from @var{channel} and call @var{proc} +with that value. +@end deffn + +@deffn {Procedure} channel-put! @var{channel} @var{data} [@var{thunk}] +Asynchronously send @var{data} to @var{channel} and call @var{thunk} +after it has been received. +@end deffn |