note about possibly replacing time-from-float-or-fraction with srfi-18 stuff
[8sync.git] / eightsync / agenda.scm
1 ;; Copyright (C) 2015 Christopher Allan Webber <cwebber@dustycloud.org>
2
3 ;; This library is free software; you can redistribute it and/or
4 ;; modify it under the terms of the GNU Lesser General Public
5 ;; License as published by the Free Software Foundation; either
6 ;; version 3 of the License, or (at your option) any later version.
7 ;;
8 ;; This library is distributed in the hope that it will be useful,
9 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
10 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 ;; Lesser General Public License for more details.
12 ;;
13 ;; You should have received a copy of the GNU Lesser General Public
14 ;; License along with this library; if not, write to the Free Software
15 ;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 ;; 02110-1301 USA
17
18 (define-module (eightsync agenda)
19   #:use-module (srfi srfi-1)
20   #:use-module (srfi srfi-9)
21   #:use-module (srfi srfi-9 gnu)
22   #:use-module (ice-9 q)
23   #:use-module (ice-9 match)
24   #:use-module (ice-9 receive)
25   #:export (<agenda>
26             make-agenda agenda?
27             agenda-queue agenda-prompt-tag
28             agenda-read-port-map agenda-write-port-map agenda-except-port-map
29             agenda-schedule
30             
31             make-async-prompt-tag
32
33             <time-segment>
34             make-time-segment time-segment?
35             time-segment-time time-segment-queue
36
37             time< time= time<= time-delta+
38             time-minus time-plus
39
40             <time-delta>
41             make-time-delta tdelta time-delta?
42             time-delta-sec time-delta-usec
43
44             <schedule>
45             make-schedule schedule?
46             schedule-add! schedule-empty?
47             schedule-segments
48             schedule-soonest-time
49
50             schedule-segments-split schedule-extract-until!
51             add-segments-contents-to-queue!
52
53             %sync 8sync
54
55             <run-request>
56             make-run-request run-request?
57             run-request-proc run-request-when
58
59             <port-request>
60             make-port-request port-request port-request?
61             port-request-port
62             port-request-read port-request-write port-request-except
63
64             run-it wrap run run-at run-delay
65
66             %port-request %run %run-at %run-delay
67             8port-request 8run 8run-at 8run-delay
68             
69             %current-agenda
70             start-agenda agenda-run-once))
71
72 ;; @@: Using immutable agendas here, so wouldn't it make sense to
73 ;;   replace this queue stuff with using pfds based immutable queues?
74
75 \f
76 ;;; Agenda definition
77 ;;; =================
78
79 ;;; The agenda consists of:
80 ;;;  - a queue of immediate items to handle
81 ;;;  - sheduled future events to be added to a future queue
82 ;;;  - a tag by which running processes can escape for some asynchronous
83 ;;;    operation (from which they can be returned later)
84 ;;;  - a mapping of ports to various handler procedures
85 ;;;
86 ;;; The goal, eventually, is for this all to be immutable and functional.
87 ;;; However, we aren't there yet.  Some tricky things:
88 ;;;  - The schedule needs to be immutable, yet reasonably efficient.
89 ;;;  - Need to use immutable queues (ijp's pfds library?)
90 ;;;  - Modeling reading from ports as something repeatable,
91 ;;;    and with reasonable separation from functional components?
92
93 (define-immutable-record-type <agenda>
94   (make-agenda-intern queue prompt-tag
95                       read-port-map write-port-map except-port-map
96                       schedule time)
97   agenda?
98   (queue agenda-queue)
99   (prompt-tag agenda-prompt-tag)
100   (read-port-map agenda-read-port-map)
101   (write-port-map agenda-write-port-map)
102   (except-port-map agenda-except-port-map)
103   (schedule agenda-schedule)
104   (time agenda-time))
105
106 (define (make-async-prompt-tag)
107   "Make an async prompt tag for an agenda.
108
109 Generally done automatically for the user through (make-agenda)."
110   (make-prompt-tag "prompt"))
111
112 (define* (make-agenda #:key
113                       (queue (make-q))
114                       (prompt (make-prompt-tag))
115                       (read-port-map (make-hash-table))
116                       (write-port-map (make-hash-table))
117                       (except-port-map (make-hash-table))
118                       (schedule (make-schedule))
119                       (time (gettimeofday)))
120   ;; TODO: document arguments
121   "Make a fresh agenda."
122   (make-agenda-intern queue prompt
123                       read-port-map write-port-map except-port-map
124                       schedule time))
125
126 (define (current-agenda-prompt)
127   "Get the prompt for the current agenda; signal an error if there isn't one."
128   (let ((current-agenda (%current-agenda)))
129     (if (not current-agenda)
130         (throw
131          'no-current-agenda
132          "Can't get current agenda prompt if there's no current agenda!")
133         (agenda-prompt-tag current-agenda))))
134
135
136 \f
137 ;;; Schedule
138 ;;; ========
139
140 ;;; This is where we handle timed events for the future
141
142 ;; This section totally borrows from the ideas in SICP
143 ;; <3 <3 <3
144
145 ;; NOTE: time is a cons of (seconds . microseconds)
146
147 (define-record-type <time-segment>
148   (make-time-segment-intern time queue)
149   time-segment?
150   (time time-segment-time)
151   (queue time-segment-queue))
152
153 ;; @@: This seems to be the same as srfi-18's seconds->time procedure?
154 ;;   Maybe double check and switch to that?  (Thanks amz3!)
155
156 (define (time-from-float-or-fraction time)
157   "Produce a (sec . usec) pair from TIME, a float or fraction"
158   (let* ((mixed-whole (floor time))
159          (mixed-rest (- time mixed-whole))  ; float or fraction component
160          (sec mixed-whole)
161          (usec (floor (* 1000000 mixed-rest))))
162     (cons (inexact->exact sec) (inexact->exact usec))))
163
164 (define (time-segment-right-format time)
165   "Ensure TIME is in the right format.
166
167 The right format means (second . microsecond).
168 If an integer, will convert appropriately."
169   ;; TODO: add floating point / rational number support.
170   (match time
171     ;; time is already a cons of second and microsecnd
172     (((? integer? s) . (? integer? u)) time)
173     ;; time was just an integer (just the second)
174     ((? integer? _) (cons time 0))
175     ((or (? rational? _) (? inexact? _))
176      (time-from-float-or-fraction time))
177     (_ (throw 'invalid-time "Invalid time" time))))
178
179 (define* (make-time-segment time #:optional (queue (make-q)))
180   "Make a time segment of TIME and QUEUE
181
182 No automatic conversion is done, so you might have to
183 run (time-segment-right-format) first."
184   (make-time-segment-intern time queue))
185
186 (define (time< time1 time2)
187   "Check if TIME1 is less than TIME2"
188   (cond ((< (car time1)
189             (car time2))
190          #t)
191         ((and (= (car time1)
192                  (car time2))
193               (< (cdr time1)
194                  (cdr time2)))
195          #t)
196         (else #f)))
197
198 (define (time= time1 time2)
199   "Check whether TIME1 and TIME2 are equivalent"
200   (and (= (car time1) (car time2))
201        (= (cdr time1) (cdr time2))))
202
203 (define (time<= time1 time2)
204   "Check if TIME1 is less than or equal to TIME2"
205   (or (time< time1 time2)
206       (time= time1 time2)))
207
208
209 (define-record-type <time-delta>
210   (make-time-delta-intern sec usec)
211   time-delta?
212   (sec time-delta-sec)
213   (usec time-delta-usec))
214
215 (define* (make-time-delta time)
216   "Make a <time-delta> of SEC seconds and USEC microseconds.
217
218 This is used primarily so the agenda can recognize RUN-REQUEST objects
219 which are meant to delay computation"
220   (match (time-segment-right-format time)
221     ((sec . usec)
222      (make-time-delta-intern sec usec))))
223
224 (define tdelta make-time-delta)
225
226 (define (time-carry-correct time)
227   "Corrects/handles time microsecond carry.
228 Will produce (0 . 0) instead of a negative number, if needed."
229   (cond ((>= (cdr time) 1000000)
230          (cons
231           (+ (car time) 1)
232           (- (cdr time) 1000000)))
233         ((< (cdr time) 0)
234          (if (= (car time) 0)
235              '(0 0)
236              (cons
237               (- (car time) 1)
238               (+ (cdr time) 1000000))))
239         (else time)))
240
241 (define (time-delta+ time time-delta)
242   "Increment a TIME by the value of TIME-DELTA"
243   (time-carry-correct
244    (cons (+ (car time) (time-delta-sec time-delta))
245          (+ (cdr time) (time-delta-usec time-delta)))))
246
247 (define (time-minus time1 time2)
248   "Subtract TIME2 from TIME1"
249   (time-carry-correct
250    (cons (- (car time1) (car time2))
251          (- (cdr time2) (cdr time2)))))
252
253 (define (time-plus time1 time2)
254   "Add TIME1 and TIME2"
255   (time-carry-correct
256    (cons (+ (car time1) (car time2))
257          (+ (cdr time2) (cdr time2)))))
258
259
260 (define-record-type <schedule>
261   (make-schedule-intern segments)
262   schedule?
263   (segments schedule-segments set-schedule-segments!))
264
265 (define* (make-schedule #:optional segments)
266   "Make a schedule, optionally pre-composed of SEGMENTS"
267   (make-schedule-intern (or segments '())))
268
269 (define (schedule-soonest-time schedule)
270   "Return a cons of (sec . usec) for next time segement, or #f if none"
271   (let ((segments (schedule-segments schedule)))
272     (if (eq? segments '())
273         #f
274         (time-segment-time (car segments)))))
275
276 ;; TODO: This code is reasonably easy to read but it
277 ;;   mutates AND is worst case of O(n) in both space and time :(
278 ;;   but at least it'll be reasonably easy to refactor to
279 ;;   a more functional setup?
280 (define (schedule-add! schedule time proc)
281   "Mutate SCHEDULE, adding PROC at an appropriate time segment for TIME"
282   (let ((time (time-segment-right-format time)))
283     (define (new-time-segment)
284       (let ((new-segment
285              (make-time-segment time)))
286         (enq! (time-segment-queue new-segment) proc)
287         new-segment))
288     (define (loop segments)
289       (define (segment-equals-time? segment)
290         (time= time (time-segment-time segment)))
291
292       (define (segment-more-than-time? segment)
293         (time< time (time-segment-time segment)))
294
295       ;; We could switch this out to be more mutate'y
296       ;; and avoid the O(n) of space... is that over-optimizing?
297       (match segments
298         ;; If we're at the end of the list, time to make a new
299         ;; segment...
300         ('() (cons (new-time-segment) '()))
301         ;; If the segment's time is exactly our time, good news
302         ;; everyone!  Let's append our stuff to its queue
303         (((? segment-equals-time? first) rest ...)
304          (enq! (time-segment-queue first) proc)
305          segments)
306         ;; If the first segment is more than our time,
307         ;; ours belongs before this one, so add it and
308         ;; start consing our way back
309         (((? segment-more-than-time? first) rest ...)
310          (cons (new-time-segment) segments))
311         ;; Otherwise, build up recursive result
312         ((first rest ... )
313          (cons first (loop rest)))))
314     (set-schedule-segments!
315      schedule
316      (loop (schedule-segments schedule)))))
317
318 (define (schedule-empty? schedule)
319   "Check if the SCHEDULE is currently empty"
320   (eq? (schedule-segments schedule) '()))
321
322 (define (schedule-segments-split schedule time)
323   "Does a multiple value return of time segments before/at and after TIME"
324   (let ((time (time-segment-right-format time)))
325     (define (segment-is-now? segment)
326       (time= (time-segment-time segment) time))
327     (define (segment-is-before-now? segment)
328       (time< (time-segment-time segment) time))
329
330     (let loop ((segments-before '())
331                (segments-left (schedule-segments schedule)))
332       (match segments-left
333         ;; end of the line, return
334         ('()
335          (values (reverse segments-before) '()))
336
337         ;; It's right now, so time to stop, but include this one in before
338         ;; but otherwise return
339         (((? segment-is-now? first) rest ...)
340          (values (reverse (cons first segments-before)) rest))
341
342         ;; This is prior or at now, so add it and keep going
343         (((? segment-is-before-now? first) rest ...)
344          (loop (cons first segments-before) rest))
345
346         ;; Otherwise it's past now, just return what we have
347         (segments-after
348          (values segments-before segments-after))))))
349
350 (define (schedule-extract-until! schedule time)
351   "Extract all segments until TIME from SCHEDULE, and pop old segments off"
352   (receive (segments-before segments-after)
353       (schedule-segments-split schedule time)
354     (set-schedule-segments! schedule segments-after)
355     segments-before))
356
357 (define (add-segments-contents-to-queue! segments queue)
358   (for-each
359    (lambda (segment)
360      (let ((seg-queue (time-segment-queue segment)))
361        (while (not (q-empty? seg-queue))
362          (enq! queue (deq! seg-queue)))))
363    segments))
364
365
366 \f
367 ;;; Request to run stuff
368 ;;; ====================
369
370 (define-record-type <run-request>
371   (make-run-request proc when)
372   run-request?
373   (proc run-request-proc)
374   (when run-request-when))
375
376 (define* (run-it proc #:optional when)
377   "Make a request to run PROC (possibly at WHEN)"
378   (make-run-request proc when))
379
380 (define-syntax-rule (wrap body ...)
381   "Wrap contents in a procedure"
382   (lambda ()
383     body ...))
384
385 ;; @@: Do we really want `body ...' here?
386 ;;   what about just `body'?
387 (define-syntax-rule (run body ...)
388   "Run everything in BODY but wrap in a convenient procedure"
389   (make-run-request (wrap body ...) #f))
390
391 (define-syntax-rule (run-at body ... when)
392   "Run BODY at WHEN"
393   (make-run-request (wrap body ...) when))
394
395 ;; @@: Is it okay to overload the term "delay" like this?
396 ;;   Would `run-in' be better?
397 (define-syntax-rule (run-delay body ... delay-time)
398   "Run BODY at DELAY-TIME time from now"
399   (make-run-request (wrap body ...) (tdelta delay-time)))
400
401
402 ;; A request to set up a port with at least one of read, write, except
403 ;; handling processes
404
405 (define-record-type <port-request>
406   (make-port-request-intern port read write except)
407   port-request?
408   (port port-request-port)
409   (read port-request-read)
410   (write port-request-write)
411   (except port-request-except))
412
413 (define* (make-port-request port #:key read write except)
414   (if (not (or read write except))
415       (throw 'no-port-handler-given "No port handler given.\n"))
416   (make-port-request-intern port read write except))
417
418 (define port-request make-port-request)
419
420
421 \f
422 ;;; Asynchronous escape to run things
423 ;;; =================================
424
425 ;; The future's in futures
426
427 (define (make-future call-first on-success on-fail on-error)
428   ;; TODO: add error stuff here
429   (lambda ()
430     (let ((call-result (call-first)))
431       ;; queue up calling the 
432       (run (on-success call-result)))))
433
434 (define (agenda-on-error agenda)
435   (const #f))
436
437 (define (agenda-on-fail agenda)
438   (const #f))
439
440 (define* (request-future call-first on-success
441                          #:key
442                          (agenda (%current-agenda))
443                          (on-fail (agenda-on-fail agenda))
444                          (on-error (agenda-on-error agenda))  
445                          (when #f))
446   ;; TODO: error handling
447   ;; do we need some distinction between expected, catchable errors,
448   ;; and unexpected, uncatchable ones?  Probably...?
449   (make-run-request
450    (make-future call-first on-success on-fail on-error)
451    when))
452
453 (define-syntax-rule (%sync async-request)
454   "Run BODY asynchronously at a prompt, passing args to make-future.
455
456 Pronounced `eight-sync' despite the spelling.
457
458 %sync was chosen because (async) was already taken and could lead to
459 errors, and this version of asynchronous code uses a prompt, so the `a'
460 character becomes a `%' prompt! :)
461
462 The % and 8 characters kind of look similar... hence this library's
463 name!  (That, and the pun 'eight-synchronous' programming.)
464 There are 8sync aliases if you prefer that name."
465   (abort-to-prompt (current-agenda-prompt)
466                    async-request))
467
468 (define-syntax-rule (8sync args ...)
469   "Alias for %sync"
470   (%sync args ...))
471
472 ;; Async port request and run-request meta-requests
473 (define (make-async-request proc)
474   "Wrap PROC in an async-request
475
476 The purpose of this is to make sure that users don't accidentally
477 return the wrong thing via (8sync) and trip themselves up."
478   (cons '*async-request* proc))
479
480 (define (setup-async-request resume-kont async-request)
481   "Complete an async request for agenda-run-once's continuation handling"
482   (match async-request
483     (('*async-request* . async-setup-proc)
484      (async-setup-proc resume-kont))
485     ;; TODO: deliver more helpful errors depending on what the user
486     ;;   returned
487     (_ (throw 'invalid-async-request
488               "Invalid request passed back via an (%sync) procedure."
489               async-request))))
490
491 (define-syntax-rule (%run body ...)
492   (%run-at body ... #f))
493
494 (define-syntax-rule (%run-at body ... when)
495   (make-async-request
496    (lambda (kont)
497      (make-run-request
498       (wrap
499        (kont
500         (begin body ...)))
501       when))))
502
503 (define-syntax-rule (%run-delay body ... delay-time)
504   (%run-at body ... (tdelta delay-time)))
505
506 (define-syntax-rule (%port-request add-this-port port-request-args ...)
507   (make-async-request
508    (lambda (kont)
509      (list (make-port-request port-request-args ...)
510            (make-run-request kont)))))
511
512 ;; TODO
513 (define-syntax-rule (%run-with-return return body ...)
514   (make-async-request
515    (lambda (kont)
516      (let ((return kont))
517        (lambda ()
518          body ...)))))
519
520 ;; Aliases
521 (define-syntax-rule (8run args ...) (%run args ...))
522 (define-syntax-rule (8run-at args ...) (%run-at args ...))
523 (define-syntax-rule (8run-delay args ...) (%run-delay args ...))
524 (define-syntax-rule (8port-request args ...) (%port-request args ...))
525
526
527 \f
528 ;;; Execution of agenda, and current agenda
529 ;;; =======================================
530
531 (define %current-agenda (make-parameter #f))
532
533 (define (update-agenda-from-select! agenda)
534   "Potentially (select) on ports specified in agenda, adding items to queue"
535   (define (hash-keys hash)
536     (hash-map->list (lambda (k v) k) hash))
537   (define (get-wait-time)
538     ;; TODO: we need to figure this out based on whether there's anything
539     ;;   in the queue, and if not, how long till the next scheduled item
540     (let ((soonest-time (schedule-soonest-time (agenda-schedule agenda))))
541       (cond 
542        ((not (q-empty? (agenda-queue agenda)))
543         (cons 0 0))
544        (soonest-time    ; ie, the agenda is non-empty
545         (let* ((current-time (agenda-time agenda)))
546           (if (time<= soonest-time current-time)
547               ;; Well there's something due so let's select
548               ;; (this avoids a (possible?) race condition chance)
549               (cons 0 0)
550               (time-minus soonest-time current-time))))
551        (else
552         (cons #f #f)))))
553   (define (do-select)
554     ;; TODO: support usecond wait time too
555     (match (get-wait-time)
556       ((sec . usec)
557        (select (hash-keys (agenda-read-port-map agenda))
558                (hash-keys (agenda-write-port-map agenda))
559                (hash-keys (agenda-except-port-map agenda))
560                sec usec))))
561   (define (get-procs-to-run)
562     (define (ports->procs ports port-map)
563       (lambda (initial-procs)
564         (fold
565          (lambda (port prev)
566            (cons (lambda ()
567                    ((hash-ref port-map port) port))
568                  prev))
569          initial-procs
570          ports)))
571     (match (do-select)
572       ((read-ports write-ports except-ports)
573        ;; @@: Come on, we can do better than append ;P
574        ((compose (ports->procs
575                   read-ports
576                   (agenda-read-port-map agenda))
577                  (ports->procs
578                   write-ports
579                   (agenda-write-port-map agenda))
580                  (ports->procs
581                   except-ports
582                   (agenda-except-port-map agenda)))
583         '()))))
584   (define (update-agenda)
585     (let ((procs-to-run (get-procs-to-run))
586           (q (agenda-queue agenda)))
587       (for-each
588        (lambda (proc)
589          (enq! q proc))
590        procs-to-run))
591     agenda)
592   (define (ports-to-select?)
593     (define (has-items? selector)
594       ;; @@: O(n)
595       ;;    ... we could use hash-for-each and a continuation to jump
596       ;;    out with a #t at first indication of an item
597       (not (= (hash-count (const #t)
598                           (selector agenda))
599               0)))
600     (or (has-items? agenda-read-port-map)
601         (has-items? agenda-write-port-map)
602         (has-items? agenda-except-port-map)))
603
604   (if (ports-to-select?)
605       (update-agenda)
606       agenda))
607
608 (define (agenda-handle-port-request! agenda port-request)
609   "Update an agenda for a port-request"
610   (define (handle-selector request-selector port-map-selector)
611     (if (request-selector port-request)
612         (hash-set! (port-map-selector agenda)
613                    (port-request-port port-request)
614                    (request-selector port-request))))
615   (handle-selector port-request-read agenda-read-port-map)
616   (handle-selector port-request-write agenda-write-port-map)
617   (handle-selector port-request-except agenda-except-port-map))
618
619
620 (define* (start-agenda agenda
621                        #:key stop-condition
622                        (get-time gettimeofday)
623                        (handle-ports update-agenda-from-select!))
624   ;; TODO: Document fields
625   "Start up the AGENDA"
626   (let loop ((agenda agenda))
627     (let ((agenda   
628            ;; @@: Hm, maybe here would be a great place to handle
629            ;;   select'ing on ports.
630            ;;   We could compose over agenda-run-once and agenda-read-ports
631            (agenda-run-once agenda)))
632       (if (and stop-condition (stop-condition agenda))
633           'done
634           (let* ((agenda
635                   ;; We have to update the time after ports handled, too
636                   ;; because it may have changed after a select
637                   (set-field
638                    (handle-ports
639                     ;; Adjust the agenda's time just in time
640                     ;; We do this here rather than in agenda-run-once to make
641                     ;; agenda-run-once's behavior fairly predictable
642                     (set-field agenda (agenda-time) (get-time)))
643                    (agenda-time) (get-time))))
644             ;; Update the agenda's current queue based on
645             ;; currently applicable time segments
646             (add-segments-contents-to-queue!
647              (schedule-extract-until! (agenda-schedule agenda) (agenda-time agenda))
648              (agenda-queue agenda))
649             (loop agenda))))))
650
651 (define (agenda-run-once agenda)
652   "Run once through the agenda, and produce a new agenda
653 based on the results"
654   (define (call-proc proc)
655     (call-with-prompt
656      (agenda-prompt-tag agenda)
657      (lambda ()
658        (parameterize ((%current-agenda agenda))
659          (proc)))
660      (lambda (kont async-request)
661        (setup-async-request kont async-request))))
662
663   (let ((queue (agenda-queue agenda))
664         (next-queue (make-q)))
665     (while (not (q-empty? queue))
666       (let* ((proc (q-pop! queue))
667              (proc-result (call-proc proc))
668              (enqueue
669               (lambda (run-request)
670                 (define (schedule-at! time proc)
671                   (schedule-add! (agenda-schedule agenda) time proc))
672                 (let ((request-time (run-request-when run-request)))
673                   (match request-time
674                     ((? time-delta? time-delta)
675                      (let ((time (time-delta+ (agenda-time agenda)
676                                               time-delta)))
677                        (schedule-at! time (run-request-proc run-request))))
678                     ((? integer? sec)
679                      (let ((time (cons sec 0)))
680                        (schedule-at! time (run-request-proc run-request))))
681                     (((? integer? sec) . (? integer? usec))
682                      (schedule-at! request-time (run-request-proc run-request)))
683                     (#f
684                      (enq! next-queue (run-request-proc run-request))))))))
685         (define (handle-individual result)
686           (match result
687             ((? run-request? new-proc)
688              (enqueue new-proc))
689             ((? port-request? port-request)
690              (agenda-handle-port-request! agenda port-request))
691             ;; do nothing
692             (_ #f)))
693         ;; @@: We might support delay-wrapped procedures here
694         (match proc-result
695           ((results ...)
696            (for-each handle-individual results))
697           (one-result (handle-individual one-result)))))
698     ;; TODO: Alternately, we could return the next-queue
699     ;;   along with changes to be added to the schedule here?
700     ;; Return new agenda, with next queue set
701     (set-field agenda (agenda-queue) next-queue)))