2018-04-06 10:37:59 +00:00
|
|
|
#lang racket/base
|
|
|
|
|
2018-04-08 10:44:32 +00:00
|
|
|
(provide make-dataspace ;; TODO: how to cleanly provide this?
|
2018-04-29 13:54:14 +00:00
|
|
|
with-current-facet ;; TODO: shouldn't be provided
|
2018-04-30 09:44:35 +00:00
|
|
|
with-non-script-context ;; TODO: shouldn't be provided
|
2018-04-08 10:44:32 +00:00
|
|
|
run-scripts! ;; TODO: how to cleanly provide this?
|
2018-05-03 21:09:33 +00:00
|
|
|
apply-patch! ;; TODO: DEFINITELY SHOULDN'T BE PROVIDED - needed by relay.rkt
|
2018-04-08 10:44:32 +00:00
|
|
|
|
|
|
|
dataspace?
|
2018-05-03 21:09:33 +00:00
|
|
|
dataspace-assertions ;; TODO: shouldn't be provided - needed by various tests
|
|
|
|
dataspace-routing-table ;; TODO: shouldn't be provided - needed by relay.rkt
|
2018-04-08 10:44:32 +00:00
|
|
|
generate-id! ;; TODO: shouldn't be provided - inline syntax.rkt??
|
|
|
|
|
|
|
|
actor?
|
|
|
|
actor-id
|
|
|
|
actor-name
|
2018-04-27 17:03:31 +00:00
|
|
|
actor-dataspace ;; TODO: should this be provided?
|
2018-04-08 10:44:32 +00:00
|
|
|
|
|
|
|
facet?
|
|
|
|
facet-actor
|
2018-05-01 19:58:43 +00:00
|
|
|
facet-live?
|
2018-04-08 10:44:32 +00:00
|
|
|
|
|
|
|
field-handle ;; TODO: shouldn't be provided - inline syntax.rkt??
|
|
|
|
field-handle?
|
|
|
|
field-handle-name
|
|
|
|
field-handle-id
|
|
|
|
field-handle-owner
|
|
|
|
field-handle-value
|
|
|
|
|
2018-04-27 22:53:31 +00:00
|
|
|
current-actor-crash-logger
|
|
|
|
|
2018-04-08 10:44:32 +00:00
|
|
|
current-actor
|
|
|
|
current-facet
|
|
|
|
in-script? ;; TODO: shouldn't be provided - inline syntax.rkt??
|
|
|
|
capture-facet-context ;; TODO: shouldn't be provided - inline syntax.rkt??
|
2018-04-09 09:23:22 +00:00
|
|
|
suspend-script* ;; TODO: shouldn't be provided - inline syntax.rkt??
|
2018-04-08 10:44:32 +00:00
|
|
|
|
|
|
|
add-facet!
|
|
|
|
stop-facet!
|
2018-04-19 16:55:52 +00:00
|
|
|
add-stop-script! ;; TODO: shouldn't be provided - inline syntax.rkt??
|
2018-04-08 10:44:32 +00:00
|
|
|
add-endpoint!
|
2018-04-29 13:54:14 +00:00
|
|
|
remove-endpoint!
|
2018-04-08 10:44:32 +00:00
|
|
|
terminate-facet! ;; TODO: shouldn't be provided - inline syntax.rkt??
|
|
|
|
schedule-script! ;; TODO: shouldn't be provided - inline syntax.rkt??
|
|
|
|
push-script! ;; TODO: shouldn't be provided - inline syntax.rkt??
|
|
|
|
ensure-in-script! ;; TODO: shouldn't be provided - inline syntax.rkt??
|
|
|
|
|
2018-04-11 11:28:09 +00:00
|
|
|
spawn! ;; TODO: should this be provided?
|
|
|
|
enqueue-send! ;; TODO: should this be provided?
|
2018-04-29 13:54:14 +00:00
|
|
|
enqueue-deferred-turn! ;; TODO: should this be provided?
|
2018-04-19 16:55:52 +00:00
|
|
|
adhoc-retract! ;; TODO: should this be provided?
|
|
|
|
adhoc-assert! ;; TODO: should this be provided?
|
|
|
|
actor-adhoc-assertions ;; TODO: should this be provided?
|
2018-04-08 10:44:32 +00:00
|
|
|
)
|
2018-04-08 06:39:39 +00:00
|
|
|
|
2018-04-06 10:37:59 +00:00
|
|
|
(require syndicate/functional-queue)
|
|
|
|
(require syndicate/dataflow)
|
|
|
|
(require racket/match)
|
|
|
|
(require racket/set)
|
2018-04-08 10:44:32 +00:00
|
|
|
(require (only-in racket/exn exn->string))
|
2018-04-06 10:37:59 +00:00
|
|
|
|
|
|
|
(require "skeleton.rkt")
|
|
|
|
(require "pattern.rkt")
|
|
|
|
(require "bag.rkt")
|
|
|
|
|
2018-04-08 06:58:17 +00:00
|
|
|
;; An `ActorID` uniquely identifies an actor in a `Dataspace`.
|
2018-04-08 07:52:37 +00:00
|
|
|
;; A `FID` is a Facet ID, uniquely identifying a facet in a `Dataspace`.
|
2018-04-06 10:37:59 +00:00
|
|
|
|
|
|
|
;; A `Dataspace` is a ... TODO
|
|
|
|
|
2018-04-27 16:30:26 +00:00
|
|
|
;; An `Action` is one of
|
|
|
|
;; - `(patch (MutableDeltaof Assertion))`
|
|
|
|
;; - `(message Assertion)`
|
|
|
|
;; - `(spawn Any BootProc (Set Assertion))`
|
2018-04-29 13:54:14 +00:00
|
|
|
;; - `(quit)`
|
|
|
|
;; - `(deferred-turn (-> Any))`
|
2018-04-06 10:37:59 +00:00
|
|
|
(struct patch (changes) #:prefab)
|
|
|
|
(struct message (body) #:prefab)
|
2018-04-08 06:39:39 +00:00
|
|
|
(struct spawn (name boot-proc initial-assertions) #:prefab)
|
2018-04-27 16:30:26 +00:00
|
|
|
(struct quit () #:prefab)
|
2018-04-29 13:54:14 +00:00
|
|
|
(struct deferred-turn (continuation) #:prefab)
|
2018-04-06 10:37:59 +00:00
|
|
|
|
|
|
|
(struct dataspace ([next-id #:mutable] ;; Nat
|
|
|
|
routing-table ;; Skeleton
|
2018-04-19 16:55:52 +00:00
|
|
|
;; v TODO: Caches have to be bags, not sets; once
|
|
|
|
;; this change is made, can I avoid keeping a bag
|
|
|
|
;; of assertions in the dataspace as a whole?
|
2018-04-27 08:59:03 +00:00
|
|
|
assertions ;; (MutableBagof Assertion)
|
2018-04-06 10:37:59 +00:00
|
|
|
dataflow ;; DataflowGraph
|
2018-04-11 11:28:09 +00:00
|
|
|
[runnable #:mutable] ;; (Listof Actor)
|
2018-04-22 20:02:40 +00:00
|
|
|
[pending-actions #:mutable] ;; (Queueof ActionGroup)
|
2018-04-11 11:28:09 +00:00
|
|
|
) #:transparent)
|
2018-04-06 10:37:59 +00:00
|
|
|
|
2018-04-08 07:52:37 +00:00
|
|
|
(struct actor (id ;; ActorID
|
2018-04-27 17:03:31 +00:00
|
|
|
dataspace ;; Dataspace
|
2018-04-08 07:52:37 +00:00
|
|
|
name ;; Any
|
|
|
|
[root-facet #:mutable] ;; (Option Facet)
|
2018-04-11 11:28:09 +00:00
|
|
|
[runnable? #:mutable] ;; Boolean
|
|
|
|
pending-scripts ;; (MutableVectorof (Queueof (-> Any)))
|
|
|
|
[pending-actions #:mutable] ;; (Queueof Action)
|
2018-04-27 08:59:03 +00:00
|
|
|
[adhoc-assertions #:mutable] ;; (Bagof Assertion)
|
2018-04-27 16:30:26 +00:00
|
|
|
[cleanup-changes #:mutable] ;; (Deltaof Assertion)
|
2018-04-08 07:52:37 +00:00
|
|
|
)
|
|
|
|
#:methods gen:custom-write
|
|
|
|
[(define (write-proc a p mode)
|
|
|
|
(fprintf p "#<actor ~a ~v>" (actor-id a) (actor-name a)))])
|
|
|
|
|
2018-04-22 20:02:40 +00:00
|
|
|
(struct action-group (actor ;; (U Actor 'meta)
|
|
|
|
actions ;; (Listof Action)
|
|
|
|
)
|
|
|
|
#:transparent)
|
|
|
|
|
2018-04-08 07:52:37 +00:00
|
|
|
(struct facet (id ;; FID
|
|
|
|
[live? #:mutable] ;; Boolean
|
|
|
|
actor ;; Actor
|
|
|
|
parent ;; (Option Facet)
|
2018-04-06 10:37:59 +00:00
|
|
|
endpoints ;; (MutableHash EID Endpoint)
|
|
|
|
[stop-scripts #:mutable] ;; (Listof Script) -- IN REVERSE ORDER
|
2018-04-08 07:52:37 +00:00
|
|
|
[children #:mutable] ;; (Seteqof Facet)
|
2018-04-06 10:37:59 +00:00
|
|
|
)
|
2018-04-08 07:52:37 +00:00
|
|
|
#:methods gen:custom-write
|
|
|
|
[(define (write-proc f p mode)
|
2018-04-22 20:00:35 +00:00
|
|
|
(local-require (only-in racket/string string-join))
|
|
|
|
(define (facet-id-chain f)
|
|
|
|
(if f
|
|
|
|
(cons (number->string (facet-id f)) (facet-id-chain (facet-parent f)))
|
|
|
|
'()))
|
2018-04-08 07:52:37 +00:00
|
|
|
(fprintf p "#<facet ~a ~v ~a>"
|
|
|
|
(actor-id (facet-actor f))
|
|
|
|
(actor-name (facet-actor f))
|
2018-04-22 20:00:35 +00:00
|
|
|
(string-join (facet-id-chain f) ":")))])
|
2018-04-06 10:37:59 +00:00
|
|
|
|
|
|
|
(struct endpoint (id ;; EID
|
|
|
|
[assertion #:mutable] ;; Assertion
|
2018-04-29 21:27:55 +00:00
|
|
|
[handler #:mutable] ;; (Option SkInterest)
|
|
|
|
update-fn ;; (-> (Values Assertion (Option SkInterest)))
|
2018-04-06 10:37:59 +00:00
|
|
|
)
|
2018-04-08 07:52:37 +00:00
|
|
|
#:methods gen:custom-write
|
|
|
|
[(define (write-proc e p mode)
|
|
|
|
(fprintf p "#<endpoint ~a>" (endpoint-id e)))])
|
2018-04-06 10:37:59 +00:00
|
|
|
|
2018-04-08 10:44:32 +00:00
|
|
|
;; TODO: the field ownership checks during field-ref/field-set! might
|
|
|
|
;; be quite expensive. Are they worth it?
|
2018-04-06 10:37:59 +00:00
|
|
|
(struct field-handle (name ;; Symbol
|
|
|
|
id ;; Nat
|
2018-04-08 07:52:37 +00:00
|
|
|
owner ;; Actor
|
2018-04-06 10:37:59 +00:00
|
|
|
[value #:mutable] ;; Any
|
|
|
|
)
|
|
|
|
#:methods gen:custom-write
|
|
|
|
[(define (write-proc f port mode)
|
|
|
|
(fprintf port "#<field-handle:~a:~a>" (field-handle-name f) (field-handle-id f)))]
|
|
|
|
#:property prop:procedure
|
|
|
|
(case-lambda
|
|
|
|
[(f)
|
2018-04-27 17:03:31 +00:00
|
|
|
(define ac (current-actor))
|
|
|
|
(when (not (eq? (field-handle-owner f) ac)) (field-scope-error 'field-ref f))
|
|
|
|
(dataflow-record-observation! (dataspace-dataflow (actor-dataspace ac)) f)
|
2018-04-06 10:37:59 +00:00
|
|
|
(field-handle-value f)]
|
|
|
|
[(f v)
|
2018-04-27 17:03:31 +00:00
|
|
|
(define ac (current-actor))
|
2018-04-08 10:44:32 +00:00
|
|
|
(when (not (eq? (field-handle-owner f) (current-actor))) (field-scope-error 'field-set! f))
|
2018-04-29 17:43:39 +00:00
|
|
|
(when (not (equal? (field-handle-value f) v))
|
|
|
|
(dataflow-record-damage! (dataspace-dataflow (actor-dataspace ac)) f)
|
|
|
|
(set-field-handle-value! f v))]))
|
2018-04-06 10:37:59 +00:00
|
|
|
|
2018-04-08 10:44:32 +00:00
|
|
|
(define (field-scope-error who f)
|
|
|
|
(error who "Field ~a used out-of-scope; owner = ~a, current = ~a"
|
|
|
|
f
|
|
|
|
(field-handle-owner f)
|
|
|
|
(current-actor)))
|
|
|
|
|
2018-04-27 22:53:31 +00:00
|
|
|
;; Parameterof (Actor Exn -> Void)
|
|
|
|
(define current-actor-crash-logger
|
|
|
|
(make-parameter
|
|
|
|
(lambda (a e)
|
|
|
|
(log-error "Actor ~a died with exception:\n~a" a (exn->string e)))))
|
|
|
|
|
2018-04-30 10:18:49 +00:00
|
|
|
(define (current-actor) (facet-actor (current-facet)))
|
2018-04-08 07:52:37 +00:00
|
|
|
|
|
|
|
;; Parameterof Facet
|
|
|
|
(define current-facet (make-parameter #f))
|
2018-04-06 10:37:59 +00:00
|
|
|
|
|
|
|
;; Parameterof Boolean
|
|
|
|
(define in-script? (make-parameter #t))
|
|
|
|
|
|
|
|
;;---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
;; Script priorities. These are used to ensure that the results of
|
|
|
|
;; some *side effects* are visible to certain pieces of code.
|
|
|
|
|
|
|
|
(module priorities racket/base
|
|
|
|
(require (for-syntax racket/base))
|
|
|
|
|
|
|
|
(define-syntax (define-priority-levels stx)
|
|
|
|
(let loop ((counter 0) (stx (syntax-case stx ()
|
|
|
|
[(_ level ...) #'(level ...)])))
|
|
|
|
(syntax-case stx ()
|
|
|
|
[()
|
|
|
|
#'(void)]
|
|
|
|
[(#:count c)
|
|
|
|
#`(begin (define c #,counter)
|
|
|
|
(provide c))]
|
|
|
|
[(this-level more ...)
|
|
|
|
#`(begin (define this-level #,counter)
|
|
|
|
(provide this-level)
|
|
|
|
#,(loop (+ counter 1) #'(more ...)))])))
|
|
|
|
|
|
|
|
(define-priority-levels ;; highest-priority to lowest-priority
|
|
|
|
*query-priority-high*
|
|
|
|
*query-priority*
|
|
|
|
*query-handler-priority*
|
|
|
|
*normal-priority*
|
|
|
|
*gc-priority*
|
|
|
|
*idle-priority*
|
|
|
|
#:count priority-count))
|
|
|
|
|
|
|
|
(require (submod "." priorities))
|
|
|
|
|
|
|
|
;;---------------------------------------------------------------------------
|
|
|
|
|
2018-04-25 18:30:45 +00:00
|
|
|
(define (make-dataspace boot-proc)
|
2018-04-11 11:28:09 +00:00
|
|
|
(dataspace 0
|
|
|
|
(make-empty-skeleton)
|
|
|
|
(make-bag)
|
|
|
|
(make-dataflow-graph)
|
|
|
|
'()
|
2018-04-25 18:30:45 +00:00
|
|
|
(enqueue (make-queue) (action-group 'meta (list (spawn #f boot-proc (set)))))))
|
2018-04-06 10:37:59 +00:00
|
|
|
|
|
|
|
(define (generate-id! ds)
|
|
|
|
(let ((id (dataspace-next-id ds)))
|
|
|
|
(set-dataspace-next-id! ds (+ id 1))
|
|
|
|
id))
|
|
|
|
|
2018-04-08 06:39:39 +00:00
|
|
|
(define (add-actor! ds name boot-proc initial-assertions)
|
2018-04-08 07:52:37 +00:00
|
|
|
(define the-actor-id (generate-id! ds))
|
2018-04-27 08:59:03 +00:00
|
|
|
(define filtered-initial-assertions (set-remove initial-assertions (void)))
|
2018-05-03 19:09:34 +00:00
|
|
|
(define initial-delta (set->bag filtered-initial-assertions +1))
|
2018-04-11 11:28:09 +00:00
|
|
|
(define the-actor (actor the-actor-id
|
2018-04-27 17:03:31 +00:00
|
|
|
ds
|
2018-04-11 11:28:09 +00:00
|
|
|
name
|
|
|
|
#f
|
|
|
|
#f
|
|
|
|
(make-vector priority-count (make-queue))
|
2018-04-19 16:55:52 +00:00
|
|
|
(make-queue)
|
2018-05-03 19:09:34 +00:00
|
|
|
initial-delta
|
|
|
|
(bag)))
|
|
|
|
(apply-patch! ds the-actor initial-delta)
|
2018-05-01 21:39:02 +00:00
|
|
|
;; Root facet is a dummy "system" facet that exists to hold one-or-more "user" "root" facets.
|
2018-04-27 17:03:31 +00:00
|
|
|
(add-facet! #f
|
2018-04-08 07:52:37 +00:00
|
|
|
the-actor
|
|
|
|
#f
|
|
|
|
(lambda ()
|
2018-05-01 21:39:02 +00:00
|
|
|
;; The "true root", user-visible facet.
|
|
|
|
(add-facet! #f
|
|
|
|
the-actor
|
|
|
|
(current-facet)
|
|
|
|
(lambda ()
|
|
|
|
(boot-proc)))
|
2018-04-27 08:59:03 +00:00
|
|
|
(for [(a filtered-initial-assertions)]
|
|
|
|
(adhoc-retract! the-actor a)))))
|
2018-04-08 07:52:37 +00:00
|
|
|
|
2018-04-30 10:18:49 +00:00
|
|
|
(define-syntax-rule (with-current-facet [f0] body ...)
|
|
|
|
(let ((f f0))
|
2018-05-01 21:39:50 +00:00
|
|
|
;; (when (not f)
|
|
|
|
;; (error 'with-current-facet "Cannot use with-current-facet this way"))
|
2018-04-30 10:18:49 +00:00
|
|
|
(parameterize ((current-facet f))
|
2018-04-08 06:39:39 +00:00
|
|
|
(with-handlers ([(lambda (e) (not (exn:break? e)))
|
2018-04-08 10:44:32 +00:00
|
|
|
(lambda (e)
|
2018-04-30 10:18:49 +00:00
|
|
|
(define a (current-actor))
|
2018-04-27 22:53:31 +00:00
|
|
|
((current-actor-crash-logger) a e)
|
2018-04-11 11:28:09 +00:00
|
|
|
(abandon-queued-work! a)
|
2018-11-02 12:15:04 +00:00
|
|
|
;; v Supply #f for `emit-patches?` here
|
|
|
|
;; because we are in an uncertain limbo after
|
|
|
|
;; discarding previously-queued actions.
|
|
|
|
;; Instead of emitting patches to orderly
|
|
|
|
;; tear down assertions from endpoints, we
|
|
|
|
;; rely on the recorded `cleanup-changes`.
|
|
|
|
(terminate-actor! a #f))]) ;; TODO: tracing
|
2018-04-09 09:23:22 +00:00
|
|
|
(call-with-syndicate-prompt
|
|
|
|
(lambda ()
|
|
|
|
body ...))
|
2018-04-08 06:39:39 +00:00
|
|
|
(void)))))
|
2018-04-06 10:37:59 +00:00
|
|
|
|
2018-04-30 09:44:35 +00:00
|
|
|
(define-syntax-rule (with-non-script-context body ...)
|
|
|
|
(parameterize ((in-script? #f))
|
|
|
|
body ...))
|
|
|
|
|
2018-04-06 10:37:59 +00:00
|
|
|
(define (capture-facet-context proc)
|
2018-04-30 10:18:49 +00:00
|
|
|
(let ((f (current-facet)))
|
2018-05-01 21:39:50 +00:00
|
|
|
;; (when (not f)
|
|
|
|
;; (error 'capture-facet-context "Cannot capture non-facet"))
|
2018-04-06 10:37:59 +00:00
|
|
|
(lambda args
|
2018-04-30 10:18:49 +00:00
|
|
|
(with-current-facet [f]
|
2018-04-06 10:37:59 +00:00
|
|
|
(apply proc args)))))
|
|
|
|
|
2018-04-11 11:28:09 +00:00
|
|
|
(define (pop-next-script! ac)
|
|
|
|
(define priority-levels (actor-pending-scripts ac))
|
2018-04-06 10:37:59 +00:00
|
|
|
(let loop ((level 0))
|
|
|
|
(and (< level (vector-length priority-levels))
|
|
|
|
(let ((q (vector-ref priority-levels level)))
|
|
|
|
(if (queue-empty? q)
|
|
|
|
(loop (+ level 1))
|
|
|
|
(let-values (((script q) (dequeue q)))
|
|
|
|
(vector-set! priority-levels level q)
|
|
|
|
script))))))
|
|
|
|
|
2018-04-11 11:28:09 +00:00
|
|
|
(define (run-actor-pending-scripts! ds ac)
|
|
|
|
(let loop ()
|
|
|
|
(let ((script (pop-next-script! ac)))
|
|
|
|
(and script
|
|
|
|
(begin (script)
|
|
|
|
(refresh-facet-assertions! ds)
|
|
|
|
(loop))))))
|
|
|
|
|
|
|
|
(define (refresh-facet-assertions! ds)
|
2018-04-30 09:44:35 +00:00
|
|
|
(with-non-script-context
|
|
|
|
(dataflow-repair-damage! (dataspace-dataflow ds)
|
|
|
|
(lambda (subject-id)
|
|
|
|
(match-define (list f eid) subject-id)
|
|
|
|
(when (facet-live? f) ;; TODO: necessary test, or tautological?
|
|
|
|
(define ac (facet-actor f))
|
2018-04-30 10:18:49 +00:00
|
|
|
(with-current-facet [f]
|
2018-04-30 09:44:35 +00:00
|
|
|
(define ep (hash-ref (facet-endpoints f) eid))
|
|
|
|
(match-define (endpoint _ old-assertion old-handler update-fn) ep)
|
|
|
|
(define-values (new-assertion new-handler) (update-fn))
|
|
|
|
(when (not (equal? old-assertion new-assertion))
|
|
|
|
(retract! ac old-assertion)
|
|
|
|
(when old-handler (dataspace-unsubscribe! ds old-handler))
|
|
|
|
(set-endpoint-assertion! ep new-assertion)
|
|
|
|
(set-endpoint-handler! ep new-handler)
|
|
|
|
(assert! ac new-assertion)
|
|
|
|
(when new-handler (dataspace-subscribe! ds new-handler)))))))))
|
2018-04-11 11:28:09 +00:00
|
|
|
|
|
|
|
(define (commit-actions! ds ac)
|
2018-04-30 10:19:03 +00:00
|
|
|
(define pending (queue->list (actor-pending-actions ac)))
|
|
|
|
;; (log-info "commit-actions!: ~a actions ~a" ac pending)
|
|
|
|
(when (pair? pending)
|
|
|
|
(set-actor-pending-actions! ac (make-queue))
|
|
|
|
(set-dataspace-pending-actions! ds (enqueue (dataspace-pending-actions ds)
|
|
|
|
(action-group ac pending)))))
|
2018-04-11 11:28:09 +00:00
|
|
|
|
2018-04-06 10:37:59 +00:00
|
|
|
(define (run-all-pending-scripts! ds)
|
2018-04-11 11:28:09 +00:00
|
|
|
(define runnable (dataspace-runnable ds))
|
|
|
|
(set-dataspace-runnable! ds '())
|
|
|
|
(for [(ac (in-list runnable))]
|
|
|
|
(run-actor-pending-scripts! ds ac)
|
|
|
|
(set-actor-runnable?! ac #f)
|
|
|
|
(commit-actions! ds ac)))
|
2018-04-06 10:37:59 +00:00
|
|
|
|
|
|
|
(define (perform-pending-actions! ds)
|
2018-04-22 20:02:40 +00:00
|
|
|
(define groups (queue->list (dataspace-pending-actions ds)))
|
2018-04-06 10:37:59 +00:00
|
|
|
(set-dataspace-pending-actions! ds (make-queue))
|
2018-04-22 20:02:40 +00:00
|
|
|
(for [(group (in-list groups))]
|
|
|
|
(match-define (action-group ac actions) group)
|
|
|
|
(for [(action (in-list actions))]
|
2018-04-29 13:54:14 +00:00
|
|
|
;; (log-info "~a in ~a performing ~a" ac (eq-hash-code ds) action)
|
2018-04-22 20:02:40 +00:00
|
|
|
(match action
|
|
|
|
[(patch delta)
|
2018-04-27 16:30:26 +00:00
|
|
|
(apply-patch! ds ac delta)]
|
2018-04-22 20:02:40 +00:00
|
|
|
[(message body)
|
|
|
|
(send-assertion! (dataspace-routing-table ds) body)]
|
|
|
|
[(spawn name boot-proc initial-assertions)
|
2018-04-27 16:30:26 +00:00
|
|
|
(add-actor! ds name boot-proc initial-assertions)]
|
|
|
|
[(quit)
|
2018-04-29 13:54:14 +00:00
|
|
|
(apply-patch! ds ac (actor-cleanup-changes ac))]
|
|
|
|
[(deferred-turn k)
|
2018-04-30 21:47:25 +00:00
|
|
|
(push-script! ac k)])
|
|
|
|
(run-all-pending-scripts! ds))))
|
2018-04-27 16:30:26 +00:00
|
|
|
|
2018-05-04 14:55:53 +00:00
|
|
|
(define (apply-patch! ds ac delta)
|
2018-05-03 21:56:40 +00:00
|
|
|
(when (not (bag-empty? delta))
|
|
|
|
(define ds-assertions (dataspace-assertions ds))
|
2018-05-04 15:04:42 +00:00
|
|
|
;; (log-info "apply-patch! ~a ~v" ac delta)
|
|
|
|
;; (for [((a c) (in-bag/count ds-assertions))] (log-info " . ~v = ~v" a c))
|
2018-05-04 15:56:56 +00:00
|
|
|
;; (for [((a c) (in-bag/count delta))] (log-info " → ~v = ~v" a c))
|
2018-05-03 21:56:40 +00:00
|
|
|
(define rt (dataspace-routing-table ds))
|
2018-11-02 00:11:59 +00:00
|
|
|
(define pending-removals '())
|
2018-05-03 21:56:40 +00:00
|
|
|
(define new-cleanup-changes
|
|
|
|
(for/fold [(cleanup-changes (actor-cleanup-changes ac))] [((a count) (in-bag/count delta))]
|
2018-05-04 14:55:53 +00:00
|
|
|
(match (bag-change! ds-assertions a count)
|
2018-11-02 00:11:59 +00:00
|
|
|
['present->absent (set! pending-removals (cons a pending-removals))]
|
2018-05-04 14:55:53 +00:00
|
|
|
['absent->present (add-assertion! rt a)]
|
2018-05-03 21:56:40 +00:00
|
|
|
;; 'absent->absent absurd
|
|
|
|
['present->present (void)]) ;; i.e. no visible change
|
|
|
|
(define-values (updated-bag _summary) (bag-change cleanup-changes a (- count)))
|
|
|
|
updated-bag))
|
2018-11-02 00:11:59 +00:00
|
|
|
(for [(a (in-list pending-removals))]
|
|
|
|
(remove-assertion! rt a))
|
2018-05-03 21:56:40 +00:00
|
|
|
(set-actor-cleanup-changes! ac new-cleanup-changes)))
|
2018-04-06 10:37:59 +00:00
|
|
|
|
|
|
|
(define (run-scripts! ds)
|
2018-04-11 11:28:09 +00:00
|
|
|
(run-all-pending-scripts! ds)
|
|
|
|
(perform-pending-actions! ds)
|
2018-04-06 10:37:59 +00:00
|
|
|
;; TODO: figure out when a dataspace should quit itself. Given the
|
|
|
|
;; mutable nature of the implementation, maybe never? It might be
|
|
|
|
;; being held elsewhere!
|
2018-04-30 21:47:25 +00:00
|
|
|
(not (and (null? (dataspace-runnable ds))
|
|
|
|
(queue-empty? (dataspace-pending-actions ds)))))
|
2018-04-06 10:37:59 +00:00
|
|
|
|
2018-04-27 17:03:31 +00:00
|
|
|
(define (add-facet! where actor parent boot-proc)
|
2018-04-08 06:39:39 +00:00
|
|
|
(when (and (not (in-script?)) where)
|
|
|
|
(error 'add-facet!
|
|
|
|
"~a: Cannot add facet outside script; are you missing an (on ...)?"
|
|
|
|
where))
|
2018-04-27 17:03:31 +00:00
|
|
|
(define f (facet (generate-id! (actor-dataspace actor))
|
2018-04-08 07:52:37 +00:00
|
|
|
#t
|
|
|
|
actor
|
|
|
|
parent
|
2018-04-06 10:37:59 +00:00
|
|
|
(make-hash)
|
|
|
|
'()
|
2018-04-08 07:52:37 +00:00
|
|
|
(seteq)))
|
|
|
|
(if parent
|
|
|
|
(set-facet-children! parent (set-add (facet-children parent) f))
|
2018-04-22 20:03:01 +00:00
|
|
|
(begin
|
|
|
|
(when (actor-root-facet actor)
|
2018-05-01 21:39:02 +00:00
|
|
|
;; This should never happen. We deliberately create an
|
|
|
|
;; otherwise-dummy root facet for each actor specifically to
|
|
|
|
;; hold user facets, and there should be no way for the user
|
|
|
|
;; to stop that root facet explicitly, which means user code
|
|
|
|
;; can't start any replacements for it at all, let alone
|
|
|
|
;; more than one!
|
2018-04-22 20:03:01 +00:00
|
|
|
(error 'add-facet! "INTERNAL ERROR: Attempt to add second root facet"))
|
|
|
|
(set-actor-root-facet! actor f)))
|
2018-04-30 10:18:49 +00:00
|
|
|
(with-current-facet [f]
|
2018-04-30 09:44:35 +00:00
|
|
|
(with-non-script-context
|
|
|
|
(boot-proc)))
|
2018-04-27 17:03:31 +00:00
|
|
|
(push-script! actor (lambda ()
|
|
|
|
(when (or (and parent (not (facet-live? parent))) (facet-inert? f))
|
|
|
|
(terminate-facet! f)))))
|
2018-04-08 07:52:37 +00:00
|
|
|
|
2018-04-27 17:03:31 +00:00
|
|
|
(define (facet-inert? f)
|
2018-04-08 07:52:37 +00:00
|
|
|
(and (hash-empty? (facet-endpoints f))
|
2018-04-06 10:37:59 +00:00
|
|
|
(set-empty? (facet-children f))))
|
|
|
|
|
2018-04-27 17:03:31 +00:00
|
|
|
(define (schedule-script! #:priority [priority *normal-priority*] ac thunk)
|
|
|
|
(push-script! #:priority priority ac (capture-facet-context thunk)))
|
2018-04-06 10:37:59 +00:00
|
|
|
|
2018-04-27 17:03:31 +00:00
|
|
|
(define (push-script! #:priority [priority *normal-priority*] ac thunk-with-context)
|
2018-04-11 11:28:09 +00:00
|
|
|
(when (not (actor-runnable? ac))
|
|
|
|
(set-actor-runnable?! ac #t)
|
2018-04-27 17:03:31 +00:00
|
|
|
(let ((ds (actor-dataspace ac)))
|
|
|
|
(set-dataspace-runnable! ds (cons ac (dataspace-runnable ds)))))
|
2018-04-11 11:28:09 +00:00
|
|
|
(define v (actor-pending-scripts ac))
|
2018-04-08 10:44:32 +00:00
|
|
|
(vector-set! v priority (enqueue (vector-ref v priority) thunk-with-context)))
|
2018-04-08 06:39:39 +00:00
|
|
|
|
2018-11-02 12:15:04 +00:00
|
|
|
(define (retract-facet-assertions-and-subscriptions! f emit-patches?)
|
2018-04-11 11:28:09 +00:00
|
|
|
(define ac (facet-actor f))
|
2018-04-27 17:03:31 +00:00
|
|
|
(define ds (actor-dataspace ac))
|
|
|
|
(push-script! ac (lambda ()
|
|
|
|
(for [((eid ep) (in-hash (facet-endpoints f)))]
|
2018-11-02 12:15:04 +00:00
|
|
|
(destroy-endpoint! ds ac f ep emit-patches?))
|
2018-05-02 17:10:52 +00:00
|
|
|
(hash-clear! (facet-endpoints f)))))
|
2018-04-11 11:28:09 +00:00
|
|
|
|
|
|
|
(define (abandon-queued-work! ac)
|
|
|
|
(set-actor-pending-actions! ac (make-queue))
|
|
|
|
(let ((scripts (actor-pending-scripts ac)))
|
|
|
|
(for [(i (in-range (vector-length scripts)))]
|
|
|
|
(vector-set! scripts i (make-queue)))))
|
2018-04-08 06:39:39 +00:00
|
|
|
|
|
|
|
;; Abruptly terminates an entire actor, without running stop-scripts etc.
|
2018-11-02 12:15:04 +00:00
|
|
|
(define (terminate-actor! the-actor emit-patches?)
|
|
|
|
(when emit-patches?
|
|
|
|
(push-script! the-actor (lambda () (for [(a (in-bag (actor-adhoc-assertions the-actor)))]
|
|
|
|
(retract! the-actor a)))))
|
2018-04-08 07:52:37 +00:00
|
|
|
(let ((f (actor-root-facet the-actor)))
|
2018-04-08 06:39:39 +00:00
|
|
|
(when f
|
2018-04-08 07:52:37 +00:00
|
|
|
(let abort-facet! ((f f))
|
|
|
|
(set-facet-live?! f #f)
|
|
|
|
(for [(child (in-set (facet-children f)))] (abort-facet! child))
|
2018-11-02 12:15:04 +00:00
|
|
|
(retract-facet-assertions-and-subscriptions! f emit-patches?))))
|
2018-04-27 17:03:31 +00:00
|
|
|
(push-script! the-actor (lambda () (enqueue-action! the-actor (quit)))))
|
2018-04-08 06:39:39 +00:00
|
|
|
|
|
|
|
;; Cleanly terminates a facet and its children, running stop-scripts etc.
|
2018-04-27 17:03:31 +00:00
|
|
|
(define (terminate-facet! f)
|
2018-04-08 07:52:37 +00:00
|
|
|
(when (facet-live? f)
|
2018-04-11 11:28:09 +00:00
|
|
|
(define ac (facet-actor f))
|
2018-04-08 07:52:37 +00:00
|
|
|
(define parent (facet-parent f))
|
2018-04-27 16:30:26 +00:00
|
|
|
(if parent
|
|
|
|
(set-facet-children! parent (set-remove (facet-children parent) f))
|
|
|
|
(set-actor-root-facet! ac #f))
|
2018-04-06 10:37:59 +00:00
|
|
|
|
2018-04-08 07:52:37 +00:00
|
|
|
(set-facet-live?! f #f)
|
2018-04-06 10:37:59 +00:00
|
|
|
|
2018-04-27 17:03:31 +00:00
|
|
|
(for [(child (in-set (facet-children f)))] (terminate-facet! child))
|
2018-04-06 10:37:59 +00:00
|
|
|
|
|
|
|
;; Run stop-scripts after terminating children. This means that
|
|
|
|
;; children's stop-scripts run before ours.
|
2018-05-01 21:39:26 +00:00
|
|
|
(push-script! ac (lambda ()
|
|
|
|
(with-current-facet [f]
|
|
|
|
(for [(script (in-list (reverse (facet-stop-scripts f))))]
|
|
|
|
(script)))))
|
2018-04-06 10:37:59 +00:00
|
|
|
|
2018-11-02 12:15:04 +00:00
|
|
|
(retract-facet-assertions-and-subscriptions! f #t)
|
2018-04-06 10:37:59 +00:00
|
|
|
|
2018-04-27 17:03:31 +00:00
|
|
|
(push-script! #:priority *gc-priority* ac
|
2018-04-11 11:28:09 +00:00
|
|
|
(lambda ()
|
2018-04-27 16:30:26 +00:00
|
|
|
(if parent
|
2018-04-27 17:03:31 +00:00
|
|
|
(when (facet-inert? parent) (terminate-facet! parent))
|
2018-11-02 12:15:04 +00:00
|
|
|
(terminate-actor! ac #t))))))
|
2018-04-06 10:58:49 +00:00
|
|
|
|
2018-04-27 17:03:31 +00:00
|
|
|
(define (stop-facet! f stop-script)
|
2018-04-11 11:28:09 +00:00
|
|
|
(define ac (facet-actor f))
|
2018-04-30 10:18:49 +00:00
|
|
|
(with-current-facet [(facet-parent f)] ;; run in parent context wrt terminating facet
|
2018-04-27 17:03:31 +00:00
|
|
|
(schedule-script! ac (lambda ()
|
|
|
|
(terminate-facet! f)
|
|
|
|
(schedule-script! ac stop-script)))))
|
2018-04-06 10:37:59 +00:00
|
|
|
|
2018-04-27 17:03:31 +00:00
|
|
|
(define (add-stop-script! f script-proc)
|
2018-04-08 07:52:37 +00:00
|
|
|
(set-facet-stop-scripts! f (cons script-proc (facet-stop-scripts f))))
|
2018-04-08 06:39:39 +00:00
|
|
|
|
2018-04-29 21:27:55 +00:00
|
|
|
(define (add-endpoint! f where dynamic? update-fn)
|
2018-04-06 10:37:59 +00:00
|
|
|
(when (in-script?)
|
|
|
|
(error 'add-endpoint!
|
|
|
|
"~a: Cannot add endpoint in script; are you missing a (react ...)?"
|
|
|
|
where))
|
2018-04-27 17:03:31 +00:00
|
|
|
(define ds (actor-dataspace (facet-actor f)))
|
2018-04-06 10:37:59 +00:00
|
|
|
(define eid (generate-id! ds))
|
2018-04-29 21:27:55 +00:00
|
|
|
(define-values (assertion handler)
|
|
|
|
(parameterize ((current-dataflow-subject-id (if dynamic? (list f eid) #f)))
|
|
|
|
(call-with-syndicate-prompt update-fn)))
|
|
|
|
(define ep (endpoint eid assertion handler update-fn))
|
2018-04-11 11:28:09 +00:00
|
|
|
(assert! (facet-actor f) assertion)
|
2018-04-06 10:37:59 +00:00
|
|
|
(when handler (dataspace-subscribe! ds handler))
|
2018-04-29 13:54:14 +00:00
|
|
|
(hash-set! (facet-endpoints f) eid ep)
|
|
|
|
eid)
|
|
|
|
|
|
|
|
(define (remove-endpoint! f eid)
|
|
|
|
(define eps (facet-endpoints f))
|
|
|
|
(define ep (hash-ref eps eid #f))
|
|
|
|
(when ep
|
|
|
|
(define ac (facet-actor f))
|
|
|
|
(define ds (actor-dataspace ac))
|
2018-11-02 12:15:04 +00:00
|
|
|
(destroy-endpoint! ds ac f ep #t)
|
2018-05-02 17:10:52 +00:00
|
|
|
(hash-remove! eps eid)))
|
2018-04-29 13:54:14 +00:00
|
|
|
|
2018-11-02 12:15:04 +00:00
|
|
|
(define (destroy-endpoint! ds ac f ep emit-patches?)
|
2018-04-29 21:27:55 +00:00
|
|
|
(match-define (endpoint eid assertion handler _update-fn) ep)
|
2018-04-29 13:54:14 +00:00
|
|
|
(dataflow-forget-subject! (dataspace-dataflow ds) (list f eid))
|
2018-11-02 12:15:04 +00:00
|
|
|
(when emit-patches? (retract! ac assertion))
|
2018-04-29 13:54:14 +00:00
|
|
|
(when handler (dataspace-unsubscribe! ds handler)))
|
2018-04-06 10:37:59 +00:00
|
|
|
|
2018-04-11 11:28:09 +00:00
|
|
|
(define (enqueue-action! ac action)
|
|
|
|
(set-actor-pending-actions! ac (enqueue (actor-pending-actions ac) action)))
|
2018-04-08 06:39:39 +00:00
|
|
|
|
2018-04-11 11:28:09 +00:00
|
|
|
(define (ensure-patch-action! ac)
|
|
|
|
(let ((q (actor-pending-actions ac)))
|
2018-04-08 06:39:39 +00:00
|
|
|
(when (or (queue-empty? q) (not (patch? (queue-last q))))
|
2018-04-11 11:28:09 +00:00
|
|
|
(enqueue-action! ac (patch (make-bag)))))
|
|
|
|
(patch-changes (queue-last (actor-pending-actions ac))))
|
2018-04-06 10:37:59 +00:00
|
|
|
|
2018-04-11 11:28:09 +00:00
|
|
|
(define (retract! ac assertion)
|
2018-04-06 10:37:59 +00:00
|
|
|
(when (not (void? assertion))
|
2018-04-11 11:28:09 +00:00
|
|
|
(bag-change! (ensure-patch-action! ac) assertion -1)))
|
2018-04-06 10:37:59 +00:00
|
|
|
|
2018-04-11 11:28:09 +00:00
|
|
|
(define (assert! ac assertion)
|
2018-04-06 10:37:59 +00:00
|
|
|
(when (not (void? assertion))
|
2018-04-11 11:28:09 +00:00
|
|
|
(bag-change! (ensure-patch-action! ac) assertion +1)))
|
2018-04-06 10:37:59 +00:00
|
|
|
|
2018-05-01 19:58:26 +00:00
|
|
|
(define (adhoc-retract! ac assertion [count 1])
|
2018-04-27 08:59:03 +00:00
|
|
|
(when (not (void? assertion))
|
2018-04-30 21:48:27 +00:00
|
|
|
(define-values (new-assertions summary)
|
2018-05-01 19:58:26 +00:00
|
|
|
(bag-change (actor-adhoc-assertions ac) assertion (- count) #:clamp? #t))
|
2018-04-27 08:59:03 +00:00
|
|
|
(set-actor-adhoc-assertions! ac new-assertions)
|
|
|
|
(match summary
|
2018-04-30 21:48:27 +00:00
|
|
|
;; 'absent->present absurd (if the call to `adhoc-retract!`
|
|
|
|
;; matches a previous `adhoc-assert!`)
|
2018-04-27 08:59:03 +00:00
|
|
|
['present->absent (retract! ac assertion)]
|
2018-04-30 21:48:27 +00:00
|
|
|
['present->present (void)]
|
|
|
|
['absent->absent (void)]))) ;; can happen if we're exploiting the clamping
|
2018-04-19 16:55:52 +00:00
|
|
|
|
2018-05-01 19:58:26 +00:00
|
|
|
(define (adhoc-assert! ac assertion [count 1])
|
2018-04-27 08:59:03 +00:00
|
|
|
(when (not (void? assertion))
|
2018-05-01 19:58:26 +00:00
|
|
|
(define-values (new-assertions summary)
|
|
|
|
(bag-change (actor-adhoc-assertions ac) assertion count))
|
2018-04-27 08:59:03 +00:00
|
|
|
(set-actor-adhoc-assertions! ac new-assertions)
|
|
|
|
(match summary
|
|
|
|
;; 'absent->absent and 'present->absent absurd (assuming there
|
|
|
|
;; haven't been too many calls to `adhoc-retract!` in the past)
|
|
|
|
['absent->present (assert! ac assertion)]
|
|
|
|
['present->present (void)])))
|
2018-04-19 16:55:52 +00:00
|
|
|
|
2018-04-06 10:37:59 +00:00
|
|
|
(define (dataspace-unsubscribe! ds h)
|
|
|
|
(remove-interest! (dataspace-routing-table ds) h))
|
|
|
|
|
|
|
|
(define (dataspace-subscribe! ds h)
|
|
|
|
(add-interest! (dataspace-routing-table ds) h))
|
|
|
|
|
2018-04-08 10:44:32 +00:00
|
|
|
(define (ensure-in-script! who)
|
|
|
|
(when (not (in-script?))
|
|
|
|
(error who "Attempt to perform action outside script; are you missing an (on ...)?")))
|
|
|
|
|
2018-04-11 11:28:09 +00:00
|
|
|
(define (enqueue-send! ac body)
|
|
|
|
(enqueue-action! ac (message body)))
|
2018-04-08 06:39:39 +00:00
|
|
|
|
2018-04-29 13:54:14 +00:00
|
|
|
(define (enqueue-deferred-turn! ac k)
|
|
|
|
(enqueue-action! ac (deferred-turn (capture-facet-context k))))
|
|
|
|
|
2018-04-11 11:28:09 +00:00
|
|
|
(define (spawn! ac name boot-proc initial-assertions)
|
|
|
|
(enqueue-action! ac (spawn name boot-proc initial-assertions)))
|
2018-04-06 10:37:59 +00:00
|
|
|
|
2018-04-09 09:23:22 +00:00
|
|
|
;;---------------------------------------------------------------------------
|
|
|
|
;; Script suspend-and-resume.
|
|
|
|
|
|
|
|
(define prompt-tag (make-continuation-prompt-tag 'syndicate))
|
|
|
|
|
|
|
|
(define (call-with-syndicate-prompt thunk)
|
|
|
|
(call-with-continuation-prompt thunk prompt-tag))
|
|
|
|
|
|
|
|
(define (suspend-script* where proc)
|
|
|
|
(when (not (in-script?))
|
|
|
|
(error 'suspend-script
|
|
|
|
"~a: Cannot suspend script outside script; are you missing an (on ...)?"
|
|
|
|
where))
|
|
|
|
(call-with-composable-continuation
|
|
|
|
(lambda (k)
|
|
|
|
(abort-current-continuation
|
|
|
|
prompt-tag
|
|
|
|
(lambda ()
|
|
|
|
(define in? (in-script?))
|
|
|
|
(define raw-resume-parent
|
|
|
|
(capture-facet-context
|
|
|
|
(lambda results
|
|
|
|
(parameterize ((in-script? in?))
|
|
|
|
(apply k results)))))
|
|
|
|
(define resume-parent
|
|
|
|
(lambda results
|
2018-04-27 17:03:31 +00:00
|
|
|
(push-script! (current-actor)
|
2018-04-11 11:28:09 +00:00
|
|
|
(lambda () (apply raw-resume-parent results)))))
|
2018-04-09 09:23:22 +00:00
|
|
|
(proc resume-parent))))
|
|
|
|
prompt-tag))
|