1 (define-module (loopy agenda)
2 #:use-module (srfi srfi-9)
3 #:use-module (srfi srfi-9 gnu)
5 #:use-module (ice-9 match))
7 ;; @@: Using immutable agendas here, so wouldn't it make sense to
8 ;; replace this queue stuff with using pfds based immutable queues?
10 ;;; The agenda consists of:
11 ;;; - a queue of immediate items to handle
12 ;;; - sheduled future events to be added to a future queue
13 ;;; - a tag by which running processes can escape for some asynchronous
14 ;;; operation (from which they can be returned later)
15 ;;; - a mapping of ports to various handler procedures
17 ;;; The goal, eventually, is for this all to be immutable and functional.
18 ;;; However, we aren't there yet. Some tricky things:
19 ;;; - The schedule needs to be immutable, yet reasonably efficient.
20 ;;; - Need to use immutable queues (ijp's pfds library?)
21 ;;; - Modeling reading from ports as something repeatable,
22 ;;; and with reasonable separation from functional components?
24 (define-immutable-record-type <agenda>
25 (make-agenda-intern queue prompt-tag port-mapping schedule)
28 (prompt-tag agenda-prompt-tag)
29 (port-mapping agenda-port-mapping)
30 (schedule agenda-schedule))
32 (define (make-async-prompt-tag)
33 (make-prompt-tag "prompt"))
35 (define* (make-agenda #:key
37 (prompt (make-prompt-tag))
38 (port-mapping (make-port-mapping))
39 (schedule (make-schedule)))
40 (make-agenda-intern queue prompt port-mapping schedule))
45 ;;; This is where we handle timed events for the future
47 ;; This section totally borrows from SICP
50 ;; NOTE: time is a cons of (seconds . microseconds)
52 (define-record-type <time-segment>
53 (make-time-segment-intern time queue)
55 (time time-segment-time)
56 (queue time-segment-queue time-segment-set-queue!))
58 (define (time-segment-right-format time)
60 ;; time is already a cons of second and microsecnd
61 (((? integer? s) (? integer? u)) time)
62 ;; time was just an integer (just the second)
63 ((? integer? _) (cons time 0))
64 (_ (throw 'invalid-time "Invalid time" time))))
66 (define* (make-time-segment time #:optional (queue (make-q)))
67 (make-time-segment-intern time queue))
69 (define (make-schedule)
72 (define (time-< time1 time2)
83 (define (time-= time1 time2)
84 (and (= (car time1) (car time2))
85 (= (cdr time1) (cdr time2))))
87 (define (schedule-add! time proc schedule)
88 (let ((time (time-segment-right-format time)))
89 (define (belongs-before? segments)
94 ;; Find and add a schedule segment
97 (define (schedule-empty? schedule)
103 (define (make-port-mapping)
106 (define* (port-mapping-set! port-mapping port #:optional read write except)
107 "Sets port-mapping for reader / writer / exception handlers"
108 (if (not (or read write except))
109 (throw 'no-handlers-given "No handlers given for port" port))
110 (hashq-set! port-mapping port
111 `#(,read ,write ,except)))
113 (define (port-mapping-remove! port-mapping port)
114 (hashq-remove! port-mapping port))
116 ;; TODO: This is O(n), I'm pretty sure :\
117 ;; ... it might be worthwhile for us to have a
118 ;; port-mapping record that keeps a count of how many
119 ;; handlers (maybe via a promise?)
120 (define (port-mapping-empty? port-mapping)
121 "Is this port mapping empty?"
122 (eq? (hash-count (const #t) port-mapping) 0))
124 (define (port-mapping-non-empty? port-mapping)
125 "Whether this port-mapping contains any elements"
126 (not (port-mapping-empty? port-mapping)))
129 ;;; Execution of agenda, and current agenda
131 (define %current-agenda (make-parameter #f))
133 (define* (start-agenda agenda #:optional stop-condition)
134 (let loop ((agenda agenda))
136 ;; @@: Hm, maybe here would be a great place to handle
137 ;; select'ing on ports.
138 ;; We could compose over agenda-run-once and agenda-read-ports
139 (parameterize ((%current-agenda agenda))
140 (agenda-run-once agenda))))
141 (if (and stop-condition (stop-condition agenda))
143 (loop new-agenda)))))
145 (define (agenda-run-once agenda)
146 "Run once through the agenda, and produce a new agenda
147 based on the results"
148 (define (call-proc proc)
150 (agenda-prompt-tag agenda)
156 (let ((queue (agenda-queue agenda))
157 (next-queue (make-q)))
158 (while (not (q-empty? queue))
159 (let* ((proc (q-pop! queue))
160 (proc-result (call-proc proc))
163 (enq! next-queue new-proc))))
164 ;; @@: We might support delay-wrapped procedures here
166 ((? procedure? new-proc)
168 (((? procedure? new-procs) ...)
175 ;; TODO: Selecting on ports would happen here?
176 ;; Return new agenda, with next queue set
177 (set-field agenda (agenda-queue) next-queue)))