1 (use-modules (srfi srfi-9)
6 ;; @@: Using immutable agendas here, so wouldn't it make sense to
7 ;; replace this queue stuff with using pfds based immutable queues?
9 ;;; The agenda consists of:
10 ;;; - a queue of immediate items to handle
11 ;;; - sheduled future events to be added to a future queue
12 ;;; - a tag by which running processes can escape for some asynchronous
13 ;;; operation (from which they can be returned later)
14 ;;; - a mapping of ports to various handler procedures
16 ;;; The goal, eventually, is for this all to be immutable and functional.
17 ;;; However, we aren't there yet. Some tricky things:
18 ;;; - The schedule needs to be immutable, yet reasonably efficient.
19 ;;; - Need to use immutable queues (ijp's pfds library?)
20 ;;; - Modeling reading from ports as something repeatable,
21 ;;; and with reasonable separation from functional components?
23 (define-immutable-record-type <agenda>
24 (make-agenda-intern queue prompt-tag port-mapping schedule)
27 (prompt-tag agenda-prompt-tag)
28 (port-mapping agenda-port-mapping)
29 (schedule agenda-schedule))
31 (define (make-async-prompt-tag)
32 (make-prompt-tag "prompt"))
34 (define* (make-agenda #:key
36 (prompt (make-prompt-tag))
37 (port-mapping (make-port-mapping))
38 (schedule (make-schedule)))
39 (make-agenda-intern queue prompt port-mapping))
44 ;;; This is where we handle timed events for the future
46 ;; This section totally borrows from SICP
49 ;; NOTE: time is a cons of (seconds . microseconds)
51 (define-record-type <time-segment>
52 (make-time-segment-intern time queue)
54 (time time-segment-time)
55 (queue time-segment-queue time-segment-set-queue!))
57 (define* (make-time-segment time #:optional (queue (make-q)))
58 (let ((time (match time
59 ;; time was just an integer (just the second)
60 ((? integer? _) (cons time 0))
61 ;; time is already a cons of second and microsecnd
62 (((? integer? s) (? integer? u)) time)
63 (_ (throw 'invalid-time "Invalid time" time)))))
64 (make-time-segment-intern time queue)))
66 (define (make-schedule)
69 (define (schedule-add-new-segment! schedule time)
72 (define (schedule-add! schedule time proc)
73 ;; Find and add a schedule segment
76 (define (schedule-empty? schedule)
82 (define (make-port-mapping)
85 (define* (port-mapping-set! port-mapping port #:optional read write except)
86 "Sets port-mapping for reader / writer / exception handlers"
87 (if (not (or read write except))
88 (throw 'no-handlers-given "No handlers given for port" port))
89 (hashq-set! port-mapping port
90 `#(,read ,write ,except)))
92 (define (port-mapping-remove! port-mapping port)
93 (hashq-remove! port-mapping port))
95 ;; TODO: This is O(n), I'm pretty sure :\
96 ;; ... it might be worthwhile for us to have a
97 ;; port-mapping record that keeps a count of how many
98 ;; handlers (maybe via a promise?)
99 (define (port-mapping-empty? port-mapping)
100 "Is this port mapping empty?"
101 (eq? (hash-count (const #t) port-mapping) 0))
103 (define (port-mapping-non-empty? port-mapping)
104 "Whether this port-mapping contains any elements"
105 (not (port-mapping-empty? port-mapping)))
108 ;;; Execution of agenda, and current agenda
110 (define %current-agenda (make-parameter #f))
112 (define* (start-agenda agenda #:optional stop-condition)
113 (let loop ((agenda agenda))
115 ;; @@: Hm, maybe here would be a great place to handle
116 ;; select'ing on ports.
117 ;; We could compose over agenda-run-once and agenda-read-ports
118 (parameterize ((%current-agenda agenda))
119 (agenda-run-once agenda))))
120 (if (and stop-condition (stop-condition agenda))
122 (loop new-agenda)))))
124 (define (agenda-run-once agenda)
125 "Run once through the agenda, and produce a new agenda
126 based on the results"
127 (define (call-proc proc)
129 (agenda-prompt-tag agenda)
135 (let ((queue (agenda-queue agenda))
136 (next-queue (make-q)))
137 (while (not (q-empty? queue))
138 (let* ((proc (q-pop! queue))
139 (proc-result (call-proc proc))
142 (enq! next-queue new-proc))))
143 ;; @@: We might support delay-wrapped procedures here
145 ((? procedure? new-proc)
147 (((? procedure? new-procs) ...)
154 ;; TODO: Selecting on ports would happen here?
155 ;; Return new agenda, with next queue set
156 (set-field agenda (agenda-queue) next-queue)))