More comments and stub code
[8sync.git] / loopy.scm
1 (use-modules (srfi srfi-9)
2              (srfi srfi-9 gnu)
3              (ice-9 q)
4              (ice-9 match))
5
6 ;; @@: Using immutable agendas here, so wouldn't it make sense to
7 ;;   replace this queue stuff with using pfds based immutable queues?
8
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
15 ;;;
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?
22
23 (define-immutable-record-type <agenda>
24   (make-agenda-intern queue prompt-tag port-mapping schedule)
25   agenda?
26   (queue agenda-queue)
27   (prompt-tag agenda-prompt-tag)
28   (port-mapping agenda-port-mapping)
29   (schedule agenda-schedule))
30
31 (define (make-async-prompt-tag)
32   (make-prompt-tag "prompt"))
33
34 (define* (make-agenda #:key
35                       (queue (make-q))
36                       (prompt (make-prompt-tag))
37                       (port-mapping (make-port-mapping))
38                       (schedule (make-schedule)))
39   (make-agenda-intern queue prompt port-mapping))
40
41
42 ;;; Schedule
43 ;;;
44 ;;; This is where we handle timed events for the future
45
46 ;; This section totally borrows from SICP
47 ;; <3 <3 <3
48
49 ;; NOTE: time is a cons of (seconds . microseconds)
50
51 (define-record-type <time-segment>
52   (make-time-segment-intern time queue)
53   time-segment?
54   (time time-segment-time)
55   (queue time-segment-queue time-segment-set-queue!))
56
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)))
65
66 (define (make-schedule)
67   '())
68
69 (define (schedule-add-new-segment! schedule time)
70   (error))
71
72 (define (schedule-add! schedule time proc)
73   ;; Find and add a schedule segment
74   (error))
75
76 (define (schedule-empty? schedule)
77   (eq? schedule '()))
78
79
80 ;;; Port handling
81
82 (define (make-port-mapping)
83   (make-hash-table))
84
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)))
91
92 (define (port-mapping-remove! port-mapping port)
93   (hashq-remove! port-mapping port))
94
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))
102
103 (define (port-mapping-non-empty? port-mapping)
104   "Whether this port-mapping contains any elements"
105   (not (port-mapping-empty? port-mapping)))
106
107
108 ;;; Execution of agenda, and current agenda
109
110 (define %current-agenda (make-parameter #f))
111
112 (define* (start-agenda agenda #:optional stop-condition)
113   (let loop ((agenda agenda))
114     (let ((new-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))
121           'done
122           (loop new-agenda)))))
123
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)
128     (call-with-prompt
129         (agenda-prompt-tag agenda)
130       (lambda ()
131         (proc))
132       ;; TODO
133       (lambda (k) k)))
134
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))
140              (enqueue
141               (lambda (new-proc)
142                 (enq! next-queue new-proc))))
143         ;; @@: We might support delay-wrapped procedures here
144         (match proc-result
145           ((? procedure? new-proc)
146            (enqueue new-proc))
147           (((? procedure? new-procs) ...)
148            (for-each
149             (lambda (new-proc)
150               (enqueue new-proc))
151             new-procs))
152           ;; do nothing
153           (_ #f))))
154     ;; TODO: Selecting on ports would happen here?
155     ;; Return new agenda, with next queue set
156     (set-field agenda (agenda-queue) next-queue)))