From: Tim Vaughan Date: Mon, 26 Apr 2021 20:16:04 +0000 (+0200) Subject: Local proof-of-principle working. X-Git-Url: https://thelambdalab.xyz/gitweb/index.cgi?a=commitdiff_plain;h=6406acb5e3365ee9d47d5766393ac260dab3d019;p=sam.git Local proof-of-principle working. --- diff --git a/sam.scm b/sam.scm index 82f3d7f..656338d 100644 --- a/sam.scm +++ b/sam.scm @@ -20,7 +20,16 @@ (define this-machine (make-machine "localhost" 1234)) -(define next-actor-id 1) +(define next-actor-id + (let ((mutex (make-mutex "actor id mutex")) + (next-id 1)) + (lambda () + (let ((res #f)) + (mutex-lock! mutex) + (set! res next-id) + (set! next-id (+ next-id 1)) + (mutex-unlock! mutex) + res)))) (define (address-id address) (car address)) (define (address-machine address) (cdr address)) @@ -34,7 +43,7 @@ (define actor-table (make-hash-table)) (define (make-actor beh) - (let* ((id next-actor-id)) + (let* ((id (next-actor-id))) (hash-table-set! actor-table id beh) (make-address id this-machine))) @@ -44,9 +53,9 @@ (if (null? behaviour) (print "Warning: discarded message" message " to unknown actor " address) (match (apply (hash-table-ref actor-table id) (cons address message)) - ('done (hash-table-delete! actor-table actor)) + ('done (hash-table-delete! actor-table id)) ('sleep 'do-nothing) - (new-beh (hash-table-set! actor-table actor new-beh))))))) + (new-beh (hash-table-set! actor-table id new-beh))))))) ;; Scheduler @@ -59,7 +68,7 @@ (apply (if (address-local? address) send-local-message send-network-message) - message)) + (cons address message))) (define (send-local-message address . message) (mutex-lock! local-queue-mutex) @@ -87,24 +96,77 @@ (mutex-unlock! local-queue-mutex) res)) -(define scheduler-thread - (make-thread +(define (start-scheduler) + (thread-start! (lambda () - (let loop ((next-addressed-message (next-local-message))) - (apply deliver-message next-addressed-message) - (loop (next-local-message)))))) + (let loop () + (apply deliver-message (next-local-message)) + (loop))))) +;; Console -;; Testing - -(thread-start! scheduler-thread) +(define reader-queue-mutex (make-mutex "reader queue")) +(define reader-available-mutex (make-mutex "reader available")) +(mutex-lock! reader-available-mutex #f #f) +(define reader-queue (make-fifo)) -(define println +(define console (make-actor (lambda (self . message) - (apply print message) + (mutex-lock! reader-queue-mutex) + (fifo-push reader-queue (car message)) + (mutex-unlock! reader-available-mutex) + (mutex-unlock! reader-queue-mutex) 'sleep))) -(print println) -(send-message println "Hello, world!") +(define (next-reader) + (let ((res #f)) + (mutex-lock! reader-available-mutex #f #f) + (mutex-lock! reader-queue-mutex) + (set! res (fifo-pop reader-queue)) + (if (not (fifo-empty? reader-queue)) + (mutex-unlock! reader-available-mutex)) + (mutex-unlock! reader-queue-mutex) + res)) + +(define (start-console) + (let loop () + (let ((reader (next-reader))) + (##sys#thread-block-for-i/o! (current-thread) 0 #t) + (thread-yield!) + (send-message reader (read-line))) + (loop))) + + +;; System interface + +(define system + (make-actor (lambda (self . message) + (match message + (('shutdown) + (print "## System actor received shutdown message.") + (exit 0) + 'done) + (('println strings ...) + (apply print strings) + 'sleep))))) + +;; Testing + + +(send-message system 'println "Hello, what is your name?") +(send-message console + (make-actor (lambda (self . message) + (match message + ((name) + (send-message system 'println "Hello, " name "!") + 'done))))) + +(thread-start! + (lambda () + (thread-sleep! 10) + (send-message system 'shutdown))) + +(start-scheduler) +(start-console) -(thread-join! scheduler-thread) +;; (thread-join! scheduler-thread)