1 (define-module (loopy agenda)
2 #:use-module (srfi srfi-9)
3 #:use-module (srfi srfi-9 gnu)
5 #:use-module (ice-9 match)
8 agenda-queue agenda-prompt-tag
9 agenda-port-pmapping agenda-schedule
15 time-segment-time time-segment-queue
20 schedule-add! schedule-empty?
23 port-mapping-set! port-mapping-remove!
24 port-mapping-empty? port-mapping-non-empty?
27 start-agenda agenda-run-once))
29 ;; @@: Using immutable agendas here, so wouldn't it make sense to
30 ;; replace this queue stuff with using pfds based immutable queues?
36 ;;; The agenda consists of:
37 ;;; - a queue of immediate items to handle
38 ;;; - sheduled future events to be added to a future queue
39 ;;; - a tag by which running processes can escape for some asynchronous
40 ;;; operation (from which they can be returned later)
41 ;;; - a mapping of ports to various handler procedures
43 ;;; The goal, eventually, is for this all to be immutable and functional.
44 ;;; However, we aren't there yet. Some tricky things:
45 ;;; - The schedule needs to be immutable, yet reasonably efficient.
46 ;;; - Need to use immutable queues (ijp's pfds library?)
47 ;;; - Modeling reading from ports as something repeatable,
48 ;;; and with reasonable separation from functional components?
50 (define-immutable-record-type <agenda>
51 (make-agenda-intern queue prompt-tag port-mapping schedule)
54 (prompt-tag agenda-prompt-tag)
55 (port-mapping agenda-port-mapping)
56 (schedule agenda-schedule))
58 (define (make-async-prompt-tag)
59 (make-prompt-tag "prompt"))
61 (define* (make-agenda #:key
63 (prompt (make-prompt-tag))
64 (port-mapping (make-port-mapping))
65 (schedule (make-schedule)))
66 (make-agenda-intern queue prompt port-mapping schedule))
73 ;;; This is where we handle timed events for the future
75 ;; This section totally borrows from SICP
78 ;; NOTE: time is a cons of (seconds . microseconds)
80 (define-record-type <time-segment>
81 (make-time-segment-intern time queue)
83 (time time-segment-time)
84 (queue time-segment-queue))
86 (define (time-segment-right-format time)
88 ;; time is already a cons of second and microsecnd
89 (((? integer? s) (? integer? u)) time)
90 ;; time was just an integer (just the second)
91 ((? integer? _) (cons time 0))
92 (_ (throw 'invalid-time "Invalid time" time))))
94 (define* (make-time-segment time #:optional (queue (make-q)))
95 (make-time-segment-intern time queue))
97 (define (time-< time1 time2)
108 (define (time-= time1 time2)
109 (and (= (car time1) (car time2))
110 (= (cdr time1) (cdr time2))))
112 (define (make-schedule)
115 (define (schedule-add! time proc schedule)
116 (let ((time (time-segment-right-format time)))
117 (define (belongs-before? segments)
122 ;; Find and add a schedule segment
125 (define (schedule-empty? schedule)
133 (define (make-port-mapping)
136 (define* (port-mapping-set! port-mapping port #:optional read write except)
137 "Sets port-mapping for reader / writer / exception handlers"
138 (if (not (or read write except))
139 (throw 'no-handlers-given "No handlers given for port" port))
140 (hashq-set! port-mapping port
141 `#(,read ,write ,except)))
143 (define (port-mapping-remove! port-mapping port)
144 (hashq-remove! port-mapping port))
146 ;; TODO: This is O(n), I'm pretty sure :\
147 ;; ... it might be worthwhile for us to have a
148 ;; port-mapping record that keeps a count of how many
149 ;; handlers (maybe via a promise?)
150 (define (port-mapping-empty? port-mapping)
151 "Is this port mapping empty?"
152 (eq? (hash-count (const #t) port-mapping) 0))
154 (define (port-mapping-non-empty? port-mapping)
155 "Whether this port-mapping contains any elements"
156 (not (port-mapping-empty? port-mapping)))
160 ;;; Execution of agenda, and current agenda
161 ;;; =======================================
163 (define %current-agenda (make-parameter #f))
165 (define* (start-agenda agenda #:optional stop-condition)
166 (let loop ((agenda agenda))
168 ;; @@: Hm, maybe here would be a great place to handle
169 ;; select'ing on ports.
170 ;; We could compose over agenda-run-once and agenda-read-ports
171 (parameterize ((%current-agenda agenda))
172 (agenda-run-once agenda))))
173 (if (and stop-condition (stop-condition agenda))
175 (loop new-agenda)))))
177 (define (agenda-run-once agenda)
178 "Run once through the agenda, and produce a new agenda
179 based on the results"
180 (define (call-proc proc)
182 (agenda-prompt-tag agenda)
188 (let ((queue (agenda-queue agenda))
189 (next-queue (make-q)))
190 (while (not (q-empty? queue))
191 (let* ((proc (q-pop! queue))
192 (proc-result (call-proc proc))
195 (enq! next-queue new-proc))))
196 ;; @@: We might support delay-wrapped procedures here
198 ((? procedure? new-proc)
200 (((? procedure? new-procs) ...)
207 ;; TODO: Selecting on ports would happen here?
208 ;; Return new agenda, with next queue set
209 (set-field agenda (agenda-queue) next-queue)))