(define next-actor-address 1)
(define (make-actor behaviour)
- (make-actor-with-id next-actor-address behaviour)
+ (make-actor-with-address next-actor-address behaviour)
(let ((address next-actor-address))
(set! next-actor-address (+ next-actor-address 1))
address))
;;;
(define (dispatch-message address message)
- (let ((behaviour (hash-table-ref actor-table address)))
- (unless (null? behaviour)
- ((hash-table-ref actor-table address)))) message)
+ (print "Dispatching message " message " to " address)
+ (let* ((behaviour (hash-table-ref actor-table address))
+ (value (behaviour address message)))
+ (match value
+ ('done (hash-table-delete! actor-table address))
+ ('sleep 'do-nothing)
+ (else
+ hash-table-set! actor-table address value))))
;;;
;;; FIFO queue implementation
(fifo-pop message-queue)))
(define (send-message actor message)
+ (print "Queued message " message " to " actor)
(fifo-push message-queue (cons actor message)))
(define (run)
(let ((addressed-msg (next-addressed-msg)))
- (if (null? msg)
+ (if (null? addressed-msg)
'done
- (begin
- (apply dispatch-message addressed-msg)
+ (let ((address (car addressed-msg))
+ (message (cdr addressed-msg)))
+ (dispatch-message address message)
(run)))))
;;;
-;;; Send
+;;; Testing
;;;
+
+(define factorial
+ (make-actor
+ (lambda (self message)
+ (match message
+ ((n) (send-message self (list n 1)) 'sleep)
+ ((0 acc) (print acc) 'done)
+ ((n acc) (send-message self (list (- n 1) (* acc n))) 'sleep)))))
+
+;(send-message factorial '(5))