scopes/core, work in progress: use new actor-g implementation

This commit is contained in:
Helmut Merz 2025-06-02 12:05:59 +02:00
parent a33071906f
commit 50dc19c974
3 changed files with 29 additions and 32 deletions

View file

@ -32,8 +32,8 @@
(when (not (boundp '+quit-message+)) (when (not (boundp '+quit-message+))
(defconstant +quit-message+ (gensym "QUIT")))) (defconstant +quit-message+ (gensym "QUIT"))))
(defun start (mb bhv &key fore-ground) (defun start (mb bhv &key foreground)
(if fore-ground (if foreground
(ac-loop mb bhv) (ac-loop mb bhv)
(let ((ch (lp:make-channel))) (let ((ch (lp:make-channel)))
(lp:submit-task ch (lambda () (ac-loop mb bhv))) (lp:submit-task ch (lambda () (ac-loop mb bhv)))
@ -43,15 +43,9 @@
(send mb (message +quit-message+))) (send mb (message +quit-message+)))
(defun ac-loop (mb bhv) (defun ac-loop (mb bhv)
(let ((next (ac-step mb bhv)))
(unless (eq next +quit-message+)
(ac-loop mb (or next bhv)))))
(defun ac-step (mb bhv)
(let ((msg (lpq:pop-queue mb))) (let ((msg (lpq:pop-queue mb)))
(if (eq (content msg) +quit-message+) (unless (eq (content msg) +quit-message+)
+quit-message+ (ac-loop mb (or (funcall bhv msg) bhv)))))
(funcall bhv msg))))
;;;; the core (classical, i.e. Hewitt) actor API ;;;; the core (classical, i.e. Hewitt) actor API
;;; there is no `become` operation: the behavior just returns the new behavior ;;; there is no `become` operation: the behavior just returns the new behavior

View file

@ -2,7 +2,7 @@
(defpackage :scopes/core (defpackage :scopes/core
(:use :common-lisp) (:use :common-lisp)
(:local-nicknames (:actor :scopes/core/actor) (:local-nicknames (:actor :scopes/core/actor-ng)
(:async :scopes/util/async) (:async :scopes/util/async)
(:config :scopes/config) (:config :scopes/config)
(:message :scopes/core/message) (:message :scopes/core/message)
@ -12,9 +12,9 @@
(:lp :lparallel) (:lp :lparallel)
(:lpq :lparallel.queue)) (:lpq :lparallel.queue))
(:export #:action-spec #:define-actions (:export #:action-spec #:define-actions
#:*root* #:default-setup #:make-setup #:actions #:*root* #:make-setup #:actions
#:find-service #:run-services #:setup-services #:shutdown #:find-service #:run-services #:setup-services #:shutdown
#:base-context #:context #:service #:add-action #:config #:name #:base-context #:context #:add-action #:config #:mailbox #:name
#:handle-message #:handle-message
#:do-print #:echo)) #:do-print #:echo))
@ -60,26 +60,26 @@
(defvar *root* nil) (defvar *root* nil)
(defclass base-context (actor:bg-actor) (defclass base-context ()
((actions :accessor actions :initform nil))) ((actions :accessor actions :initform nil)
(mailbox :accessor mailbox :initform nil)))
(defclass context (base-context) (defclass context (base-context)
((config :reader config :initarg :config) ((config :reader config :initarg :config)
(name :reader name :initarg :name) (name :reader name :initarg :name)
(services :reader services :initform (make-hash-table)))) (services :reader services :initform (make-hash-table))))
(defclass service (context actor:bg-actor) (defun x-default-setup (cfg &optional (cls 'context) &rest args &key &allow-other-keys)
((task :accessor task :initform nil)))
(defclass root-service (service actor:fg-actor) ())
(defun default-setup (cfg &optional (cls 'context) &rest args &key &allow-other-keys)
(apply #'actor:create #'handle-message cls :config cfg :name (config:name cfg) args)) (apply #'actor:create #'handle-message cls :config cfg :name (config:name cfg) args))
(defun make-setup (&rest args &key (class 'context) (behavior #'handle-message) (defun make-setup (&key (class 'context) (behavior #'handle-message) args)
&allow-other-keys) ;(lambda (cfg)
#'(lambda (cfg) ; (apply #'actor:create behavior class :config cfg :name (config:name cfg) args))
(apply #'actor:create behavior class :config cfg :name (config:name cfg) args))) (lambda (cfg)
(let* ((ctx (apply #'make-instance class :config cfg :name (config:name cfg) args))
(bhv (lambda (msg) (funcall behavior ctx msg))))
(setf (mailbox ctx) (actor:create bhv))
ctx)))
(defun find-service (name &optional (parent *root*)) (defun find-service (name &optional (parent *root*))
(with-slots (services) parent (with-slots (services) parent
@ -88,7 +88,11 @@
(defun setup-services (&optional (cfg config:*root*)) (defun setup-services (&optional (cfg config:*root*))
(async:init) (async:init)
(setf *root* (actor:make-actor #'handle-message 'root-service :config cfg)) ;(setf *root* (actor:make-actor #'handle-message 'root-service :config cfg))
(let* ((ctx (make-instance 'context :config cfg))
(bhv (lambda (msg) (funcall #'handle-message ctx msg))))
(setf (mailbox ctx) (lpq:make-queue))
(setf *root* ctx))
(dolist (c (reverse (config:children cfg))) (dolist (c (reverse (config:children cfg)))
(add-service *root* c))) (add-service *root* c)))
@ -96,14 +100,13 @@
(unwind-protect (unwind-protect
(progn (progn
(setup-services cfg) (setup-services cfg)
(actor:start *root*)) (actor:start (mailbox *root*)))
(shutdown))) (shutdown)))
(defun shutdown () (defun shutdown ()
(dolist (ctx (alx:hash-table-values (services *root*))) (dolist (ctx (alx:hash-table-values (services *root*)))
(funcall (config:shutdown (config ctx)) ctx)) (funcall (config:shutdown (config ctx)) ctx))
(if (task *root*) ;(actor:stop *root*)
(actor:stop *root*))
(async:finish) (async:finish)
) )
@ -125,7 +128,7 @@
(defgeneric handle-message (ctx msg) (defgeneric handle-message (ctx msg)
(:method ((ctx base-context) msg) (:method ((ctx base-context) msg)
(do-actions ctx msg))) (do-actions ctx (actor:content msg))))
(defun do-actions (ctx msg &optional (acts #'actions)) (defun do-actions (ctx msg &optional (acts #'actions))
(let ((hdlrs (select msg (funcall acts ctx)))) (let ((hdlrs (select msg (funcall acts ctx))))

View file

@ -27,7 +27,7 @@
:initform (make-hash-table :test #'equalp)))) :initform (make-hash-table :test #'equalp))))
(defun setup (cfg) (defun setup (cfg)
(core:default-setup cfg 'test-receiver)) (funcall (core:make-setup :class 'test-receiver) cfg))
(defun check-message (ctx msg) (defun check-message (ctx msg)
(let ((key (shape:head msg))) (let ((key (shape:head msg)))
@ -165,4 +165,4 @@
(msg-exp (message:create '(:test :dummy) :data "dummy payload"))) (msg-exp (message:create '(:test :dummy) :data "dummy payload")))
(expect rcvr msg-exp) (expect rcvr msg-exp)
(== (core:name rcvr) :test-receiver) (== (core:name rcvr) :test-receiver)
(actorx:send rcvr msg))) (actor:send (core:mailbox rcvr) (actor:message msg))))