moduleifying things
[8sync.git] / loopy.scm
1 (define-module (loopy agenda)
2   #:use-module (srfi srfi-9)
3   #:use-module (srfi srfi-9 gnu)
4   #:use-module (ice-9 q)
5   #:use-module (ice-9 match))
6
7 ;; @@: Using immutable agendas here, so wouldn't it make sense to
8 ;;   replace this queue stuff with using pfds based immutable queues?
9
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
16 ;;;
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?
23
24 (define-immutable-record-type <agenda>
25   (make-agenda-intern queue prompt-tag port-mapping schedule)
26   agenda?
27   (queue agenda-queue)
28   (prompt-tag agenda-prompt-tag)
29   (port-mapping agenda-port-mapping)
30   (schedule agenda-schedule))
31
32 (define (make-async-prompt-tag)
33   (make-prompt-tag "prompt"))
34
35 (define* (make-agenda #:key
36                       (queue (make-q))
37                       (prompt (make-prompt-tag))
38                       (port-mapping (make-port-mapping))
39                       (schedule (make-schedule)))
40   (make-agenda-intern queue prompt port-mapping schedule))
41
42
43 ;;; Schedule
44 ;;;
45 ;;; This is where we handle timed events for the future
46
47 ;; This section totally borrows from SICP
48 ;; <3 <3 <3
49
50 ;; NOTE: time is a cons of (seconds . microseconds)
51
52 (define-record-type <time-segment>
53   (make-time-segment-intern time queue)
54   time-segment?
55   (time time-segment-time)
56   (queue time-segment-queue time-segment-set-queue!))
57
58 (define (time-segment-right-format time)
59   (match 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))))
65
66 (define* (make-time-segment time #:optional (queue (make-q)))
67   (make-time-segment-intern time queue))
68
69 (define (make-schedule)
70   '())
71
72 (define (time-< time1 time2)
73   (cond ((< (car time1)
74             (car time2))
75          #t)
76         ((and (= (car time1)
77                  (car time2))
78               (< (cdr time1)
79                  (cdr time2)))
80          #t)
81         (else #f)))
82
83 (define (time-= time1 time2)
84   (and (= (car time1) (car time2))
85        (= (cdr time1) (cdr time2))))
86
87 (define (schedule-add! time proc schedule)
88   (let ((time (time-segment-right-format time)))
89     (define (belongs-before? segments)
90       (or (null? segments)
91           (error))
92     )
93
94   ;; Find and add a schedule segment
95   (error)))
96
97 (define (schedule-empty? schedule)
98   (eq? schedule '()))
99
100
101 ;;; Port handling
102
103 (define (make-port-mapping)
104   (make-hash-table))
105
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)))
112
113 (define (port-mapping-remove! port-mapping port)
114   (hashq-remove! port-mapping port))
115
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))
123
124 (define (port-mapping-non-empty? port-mapping)
125   "Whether this port-mapping contains any elements"
126   (not (port-mapping-empty? port-mapping)))
127
128
129 ;;; Execution of agenda, and current agenda
130
131 (define %current-agenda (make-parameter #f))
132
133 (define* (start-agenda agenda #:optional stop-condition)
134   (let loop ((agenda agenda))
135     (let ((new-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))
142           'done
143           (loop new-agenda)))))
144
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)
149     (call-with-prompt
150         (agenda-prompt-tag agenda)
151       (lambda ()
152         (proc))
153       ;; TODO
154       (lambda (k) k)))
155
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))
161              (enqueue
162               (lambda (new-proc)
163                 (enq! next-queue new-proc))))
164         ;; @@: We might support delay-wrapped procedures here
165         (match proc-result
166           ((? procedure? new-proc)
167            (enqueue new-proc))
168           (((? procedure? new-procs) ...)
169            (for-each
170             (lambda (new-proc)
171               (enqueue new-proc))
172             new-procs))
173           ;; do nothing
174           (_ #f))))
175     ;; TODO: Selecting on ports would happen here?
176     ;; Return new agenda, with next queue set
177     (set-field agenda (agenda-queue) next-queue)))