diff --git a/core/actor.lisp b/core/actor.lisp index b86d40a..fdf98dd 100644 --- a/core/actor.lisp +++ b/core/actor.lisp @@ -47,12 +47,18 @@ (defun stop (mb) (send mb +quit-message+)) +(defvar *self* nil) + (defgeneric ac-loop (tsk bhv) (:method ((tsk async:mailbox) bhv) - (let ((msg (async:rcv tsk))) + (let ((*self* tsk) + (msg (async:rcv tsk))) (unless (eq (content msg) +quit-message+) (ac-loop tsk (or (funcall bhv msg) bhv)))))) +(defgeneric set-bhv (tsk bhv) + (:method ((tsk async:mailbox) bhv))) + ;;;; the core (classical, i.e. Hewitt) actor API (defun create (bhv) @@ -65,8 +71,9 @@ ;(util:lgi msg) (async:snd tsk msg))) -(defgeneric become (tsk bhv) - (:method ((tsk async:mailbox) bhv) bhv)) +(defun become (bhv) + (set-bhv *self* bhv) + bhv) ;;;; handling restartable tasks @@ -76,9 +83,9 @@ (if ok (if (eq (content msg) +quit-message+) (progn (async:set-status tsk :stopped) nil) - (progn + (let ((*self* tsk)) (async:set-status tsk :running) - (ac-vloop tsk (or (funcall bhv msg) bhv)))) + (ac-loop tsk (or (funcall bhv msg) bhv)))) (progn (async:set-status tsk :suspended) bhv)))) (defmethod send ((tsk async:restartable-task) msg) @@ -94,9 +101,8 @@ tsk (lambda () (ac-loop tsk (async:behavior tsk))))) (async:set-status :running))) -(defmethod become ((tsk async:task) bhv) - (setf (async:behavior tsk) bhv) - bhv) +(defmethod set-bhv ((tsk async:task) bhv) + (setf (async:behavior tsk) bhv)) ;;;; predefined behaviors @@ -114,7 +120,7 @@ (lambda (msg) ;(format t "calc ~a ~a~%" val (content msg)) (destructuring-bind (fn &optional param) (content msg) - (funcall fn msg val param)))) + (become (funcall fn msg val param))))) (defun plus (msg val param) (calculator (+ val param))) diff --git a/util/async.lisp b/util/async.lisp index 50ff129..724b750 100644 --- a/util/async.lisp +++ b/util/async.lisp @@ -4,7 +4,7 @@ (:use :common-lisp) (:local-nicknames (:lp :lparallel) (:lpq :lparallel.queue)) - (:export #:init #:finish #:make-ch #:make-mb #:make-task #:rcv #:try-rcv #:snd + (:export #:init #:finish #:make-mb #:make-task #:rcv #:try-rcv #:snd #:mailbox #:task #:restartable-task #:behavior #:get-status #:set-status #:submit-task #:receive-result #:try-receive-result))