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