[PATCH guile-goblins] examples: Add timer.

Janneke Nieuwenhuizen <[email protected]>
Newsgroups gmane.lisp.guile.user
Organization AvatarAcademy.nl
Message-ID <[email protected]>
Hi!

Starting to play with Goblins I had some trouble creating a timer.
Thanks to the kind help on IRC, I was able to create this example.

There are two things I had to discover: how to "sleep" in Goblins and
the fact that in some(?) cases you need do to some extra work to keep a
vat alive/running, and how to do that.  I tried several kind of loops
and other stuff until I found conditions.

Possibly others can find use for it.

Greetings,
Janneke


-- 
Janneke Nieuwenhuizen <[email protected]>  | GNU LilyPond https://LilyPond.org
Freelance IT https://www.JoyOfSource.com | Avatar® https://AvatarAcademy.com
0001-examples-Add-timer.patch (text/x-patch, 3.4 KB)
From 3be0f50c30e5b9e8250d5653abd9027c8b8feccc Mon Sep 17 00:00:00 2001
From: Janneke Nieuwenhuizen <[email protected]>
Date: Thu, 6 Mar 2025 21:51:25 +0100
Subject: [PATCH guile-goblins] examples: Add timer.

---
 Makefile.am        |  1 +
 examples/timer.scm | 60 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 61 insertions(+)
 create mode 100644 examples/timer.scm

diff --git a/Makefile.am b/Makefile.am
index 330b87d..4ee8de5 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -196,6 +196,7 @@ js/goblins.wasm: js/goblins.scm
 js: js/goblins.wasm js/reflect.js js/reflect.wasm js/wtf8.wasm
 
 EXAMPLES = examples/cauldron.scm		\
+           examples/timer.scm			\
            examples/try-captp-onion.scm		\
            examples/try-base-netlayer.scm	\
            examples/prelay-server.scm
diff --git a/examples/timer.scm b/examples/timer.scm
new file mode 100644
index 0000000..dd6fd2d
--- /dev/null
+++ b/examples/timer.scm
@@ -0,0 +1,60 @@
+;;; This file is part of Guile Goblins.
+
+;;; Copyright 2025 Jannekxe Nieuwenhuizen <[email protected]>
+;;;
+;;; Licensed under the Apache License, Version 2.0 (the "License");
+;;; you may not use this file except in compliance with the License.
+;;; You may obtain a copy of the License at
+;;;
+;;;    http://www.apache.org/licenses/LICENSE-2.0
+;;;
+;;; Unless required by applicable law or agreed to in writing, software
+;;; distributed under the License is distributed on an "AS IS" BASIS,
+;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+;;; See the License for the specific language governing permissions and
+;;; limitations under the License.
+
+(use-modules (srfi srfi-26)
+             ((fibers) #:select ((sleep . fibers:sleep))) ;avoid #:replace warning
+             ((fibers conditions) #:select (make-condition signal-condition! wait))
+             ((goblins) #:select ((spawn . spawn-actor) ;avoid #:replace warning
+                                  ($ . $$)  ;support (ice-9 match) ($ <record-type>)
+                                  <-
+                                  define-actor
+                                  on
+                                  spawn-vat with-vat))
+             ((goblins actor-lib methods) #:select (methods))
+             ((goblins vat) #:select (spawn-fibrous-vow)))
+
+(define (timeout-vow duration)
+  (spawn-fibrous-vow (lambda _ (fibers:sleep duration) #t)))
+
+(define-actor (^timer bcom #:key canceled?)
+  (methods
+   ((create target timeout)
+    (on (timeout-vow timeout)
+        (lambda _ (unless canceled? (<- target 'timeout target timeout))))
+    (bcom (^timer bcom)))
+   ((cancel)
+    (bcom (^timer bcom #:canceled? #t)))))
+
+(define-actor (^handler bcom thunk #:key timer)
+  (methods
+   ((timeout self timeout)
+    (thunk)
+    (when timer
+      (<- timer 'create self timeout)))))
+
+(let ((vat   (spawn-vat))
+      (exit? (make-condition)))
+  (with-vat vat
+    (let* ((timer (spawn-actor ^timer))
+           (boom  (spawn-actor ^handler (cute display "boom\n") #:timer timer))
+           (clap  (spawn-actor ^handler (cute display "*clap*\n") #:timer timer))
+           (exit  (spawn-actor ^handler (cute signal-condition! exit?))))
+      ($$ timer 'create boom 0.29)
+      ($$ timer 'create clap 0.6)
+      (on (timeout-vow 1) (lambda _ ($$ timer 'cancel)))
+      ($$ timer 'create exit 2.5)))
+  (wait exit?)
+  (display "That's all folks!\n"))
-- 
2.47.1
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.