From a33071906f8d90c28aa52ef407f0a5e6005f3ce5 Mon Sep 17 00:00:00 2001 From: Helmut Merz Date: Sun, 1 Jun 2025 19:14:29 +0200 Subject: [PATCH] new actor implementation basically working --- core/actor-ng.lisp | 17 ++++++++++------- core/core.lisp | 17 +++++++++-------- test/test-core.lisp | 16 ++++++++++++---- 3 files changed, 31 insertions(+), 19 deletions(-) diff --git a/core/actor-ng.lisp b/core/actor-ng.lisp index 1ca2842..bb93fa8 100644 --- a/core/actor-ng.lisp +++ b/core/actor-ng.lisp @@ -40,7 +40,7 @@ ch))) (defun stop (mb) - (send mb +quit-message+)) + (send mb (message +quit-message+))) (defun ac-loop (mb bhv) (let ((next (ac-step mb bhv))) @@ -49,7 +49,9 @@ (defun ac-step (mb bhv) (let ((msg (lpq:pop-queue mb))) - (funcall bhv msg))) + (if (eq (content msg) +quit-message+) + +quit-message+ + (funcall bhv msg)))) ;;;; the core (classical, i.e. Hewitt) actor API ;;; there is no `become` operation: the behavior just returns the new behavior @@ -83,16 +85,17 @@ ;;;; example behavior: calculator (defun calculator (&optional (val 0)) - #'(lambda (msg) - (destructuring-bind (fn &optional param) (content msg) - (funcall fn msg val param)))) + (lambda (msg) + (format t "calc ~a ~a~%" val (content msg)) + (destructuring-bind (fn &optional param) (content msg) + (funcall fn msg val param)))) (defun plus (msg val param) (calculator (+ val param))) (defun minus (msg val param) (calculator (- val param))) (defun show (msg val param) - (send (or (customer msg) *logger*) val)) + (send (or (customer msg) *logger*) (message val))) (defun send-value (msg val param) - (send (customer msg) val)) + (send (customer msg) (message val))) diff --git a/core/core.lisp b/core/core.lisp index db5a6b5..c09ac8e 100644 --- a/core/core.lisp +++ b/core/core.lisp @@ -92,19 +92,20 @@ (dolist (c (reverse (config:children cfg))) (add-service *root* c))) - (defun run-services (&optional (cfg config:*root*)) - (unwind-protect - (progn - (setup-services cfg) - (actor:start *root*)) - (shutdown))) +(defun run-services (&optional (cfg config:*root*)) + (unwind-protect + (progn + (setup-services cfg) + (actor:start *root*)) + (shutdown))) - (defun shutdown () +(defun shutdown () (dolist (ctx (alx:hash-table-values (services *root*))) (funcall (config:shutdown (config ctx)) ctx)) (if (task *root*) (actor:stop *root*)) - (async:finish)) + (async:finish) +) (defun add-action (ctx pat hdlr) (let* ((acts (actions ctx)) diff --git a/test/test-core.lisp b/test/test-core.lisp index 96b040d..082242a 100644 --- a/test/test-core.lisp +++ b/test/test-core.lisp @@ -146,10 +146,18 @@ (deftest test-actor () ;(async:init) - (let (calc (actor:create (actor:calculator))) - ;(actor:stop calc) - ) -) + (let* ((calc (actor:create (actor:calculator))) + val + (collector + (actor:create (lambda (msg) (setf val (actor:content msg)))))) + (actor:send calc (actor:message '(actor:plus 2))) + (actor:send calc (actor:message '(actor:minus 3))) + (actor:send calc (actor:message '(actor:show))) + (actor:send calc (actor:message '(actor:send-value) collector)) + (actor:stop calc) + (sleep 0.1) + (== val -1) +)) (deftest test-send () (let ((rcvr (receiver t:*test-suite*))