1 ;; Copyright (C) 2015 Christopher Allan Webber <cwebber@dustycloud.org>
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.
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.
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
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)
27 agenda-queue agenda-prompt-tag
28 agenda-read-port-map agenda-write-port-map agenda-except-port-map
34 make-time-segment time-segment?
35 time-segment-time time-segment-queue
37 time< time= time<= time-delta+
41 make-time-delta tdelta time-delta?
42 time-delta-sec time-delta-usec
45 make-schedule schedule?
46 schedule-add! schedule-empty?
50 schedule-segments-split schedule-extract-until!
51 add-segments-contents-to-queue!
56 make-run-request run-request?
57 run-request-proc run-request-when
60 make-port-request port-request port-request?
62 port-request-read port-request-write port-request-except
64 run-it wrap wrap-apply run run-at run-delay
66 %port-request %run %run-at %run-delay
68 catch-8sync catch-%8sync
70 print-error-and-continue
73 start-agenda agenda-run-once))
75 ;; @@: Using immutable agendas here, so wouldn't it make sense to
76 ;; replace this queue stuff with using pfds based immutable queues?
82 ;;; The agenda consists of:
83 ;;; - a queue of immediate items to handle
84 ;;; - sheduled future events to be added to a future queue
85 ;;; - a tag by which running processes can escape for some asynchronous
86 ;;; operation (from which they can be returned later)
87 ;;; - a mapping of ports to various handler procedures
89 ;;; The goal, eventually, is for this all to be immutable and functional.
90 ;;; However, we aren't there yet. Some tricky things:
91 ;;; - The schedule needs to be immutable, yet reasonably efficient.
92 ;;; - Need to use immutable queues (ijp's pfds library?)
93 ;;; - Modeling reading from ports as something repeatable,
94 ;;; and with reasonable separation from functional components?
96 (define-immutable-record-type <agenda>
97 (make-agenda-intern queue prompt-tag
98 read-port-map write-port-map except-port-map
99 schedule time catch-handler pre-unwind-handler)
102 (prompt-tag agenda-prompt-tag)
103 (read-port-map agenda-read-port-map)
104 (write-port-map agenda-write-port-map)
105 (except-port-map agenda-except-port-map)
106 (schedule agenda-schedule)
108 (catch-handler agenda-catch-handler)
109 (pre-unwind-handler agenda-pre-unwind-handler))
111 (define (make-async-prompt-tag)
112 "Make an async prompt tag for an agenda.
114 Generally done automatically for the user through (make-agenda)."
115 (make-prompt-tag "prompt"))
117 (define* (make-agenda #:key
119 (prompt (make-prompt-tag))
120 (read-port-map (make-hash-table))
121 (write-port-map (make-hash-table))
122 (except-port-map (make-hash-table))
123 (schedule (make-schedule))
124 (time (gettimeofday))
126 (pre-unwind-handler #f))
127 ;; TODO: document arguments
128 "Make a fresh agenda."
129 (make-agenda-intern queue prompt
130 read-port-map write-port-map except-port-map
132 catch-handler pre-unwind-handler))
134 (define (current-agenda-prompt)
135 "Get the prompt for the current agenda; signal an error if there isn't one."
136 (let ((current-agenda (%current-agenda)))
137 (if (not current-agenda)
140 "Can't get current agenda prompt if there's no current agenda!")
141 (agenda-prompt-tag current-agenda))))
148 ;;; This is where we handle timed events for the future
150 ;; This section totally borrows from the ideas in SICP
153 ;; NOTE: time is a cons of (seconds . microseconds)
155 (define-record-type <time-segment>
156 (make-time-segment-intern time queue)
158 (time time-segment-time)
159 (queue time-segment-queue))
161 ;; @@: This seems to be the same as srfi-18's seconds->time procedure?
162 ;; Maybe double check and switch to that? (Thanks amz3!)
164 (define (time-from-float-or-fraction time)
165 "Produce a (sec . usec) pair from TIME, a float or fraction"
166 (let* ((mixed-whole (floor time))
167 (mixed-rest (- time mixed-whole)) ; float or fraction component
169 (usec (floor (* 1000000 mixed-rest))))
170 (cons (inexact->exact sec) (inexact->exact usec))))
172 (define (time-segment-right-format time)
173 "Ensure TIME is in the right format.
175 The right format means (second . microsecond).
176 If an integer, will convert appropriately."
177 ;; TODO: add floating point / rational number support.
179 ;; time is already a cons of second and microsecnd
180 (((? integer? s) . (? integer? u)) time)
181 ;; time was just an integer (just the second)
182 ((? integer? _) (cons time 0))
183 ((or (? rational? _) (? inexact? _))
184 (time-from-float-or-fraction time))
185 (_ (throw 'invalid-time "Invalid time" time))))
187 (define* (make-time-segment time #:optional (queue (make-q)))
188 "Make a time segment of TIME and QUEUE
190 No automatic conversion is done, so you might have to
191 run (time-segment-right-format) first."
192 (make-time-segment-intern time queue))
194 (define (time< time1 time2)
195 "Check if TIME1 is less than TIME2"
196 (cond ((< (car time1)
206 (define (time= time1 time2)
207 "Check whether TIME1 and TIME2 are equivalent"
208 (and (= (car time1) (car time2))
209 (= (cdr time1) (cdr time2))))
211 (define (time<= time1 time2)
212 "Check if TIME1 is less than or equal to TIME2"
213 (or (time< time1 time2)
214 (time= time1 time2)))
217 (define-record-type <time-delta>
218 (make-time-delta-intern sec usec)
221 (usec time-delta-usec))
223 (define* (make-time-delta time)
224 "Make a <time-delta> of SEC seconds and USEC microseconds.
226 This is used primarily so the agenda can recognize RUN-REQUEST objects
227 which are meant to delay computation"
228 (match (time-segment-right-format time)
230 (make-time-delta-intern sec usec))))
232 (define tdelta make-time-delta)
234 (define (time-carry-correct time)
235 "Corrects/handles time microsecond carry.
236 Will produce (0 . 0) instead of a negative number, if needed."
237 (cond ((>= (cdr time) 1000000)
240 (- (cdr time) 1000000)))
246 (+ (cdr time) 1000000))))
249 (define (time-delta+ time time-delta)
250 "Increment a TIME by the value of TIME-DELTA"
252 (cons (+ (car time) (time-delta-sec time-delta))
253 (+ (cdr time) (time-delta-usec time-delta)))))
255 (define (time-minus time1 time2)
256 "Subtract TIME2 from TIME1"
258 (cons (- (car time1) (car time2))
259 (- (cdr time1) (cdr time2)))))
261 (define (time-plus time1 time2)
262 "Add TIME1 and TIME2"
264 (cons (+ (car time1) (car time2))
265 (+ (cdr time1) (cdr time2)))))
268 (define-record-type <schedule>
269 (make-schedule-intern segments)
271 (segments schedule-segments set-schedule-segments!))
273 (define* (make-schedule #:optional segments)
274 "Make a schedule, optionally pre-composed of SEGMENTS"
275 (make-schedule-intern (or segments '())))
277 (define (schedule-soonest-time schedule)
278 "Return a cons of (sec . usec) for next time segement, or #f if none"
279 (let ((segments (schedule-segments schedule)))
280 (if (eq? segments '())
282 (time-segment-time (car segments)))))
284 ;; TODO: This code is reasonably easy to read but it
285 ;; mutates AND is worst case of O(n) in both space and time :(
286 ;; but at least it'll be reasonably easy to refactor to
287 ;; a more functional setup?
288 (define (schedule-add! schedule time proc)
289 "Mutate SCHEDULE, adding PROC at an appropriate time segment for TIME"
290 (let ((time (time-segment-right-format time)))
291 (define (new-time-segment)
293 (make-time-segment time)))
294 (enq! (time-segment-queue new-segment) proc)
296 (define (loop segments)
297 (define (segment-equals-time? segment)
298 (time= time (time-segment-time segment)))
300 (define (segment-more-than-time? segment)
301 (time< time (time-segment-time segment)))
303 ;; We could switch this out to be more mutate'y
304 ;; and avoid the O(n) of space... is that over-optimizing?
306 ;; If we're at the end of the list, time to make a new
308 ('() (cons (new-time-segment) '()))
309 ;; If the segment's time is exactly our time, good news
310 ;; everyone! Let's append our stuff to its queue
311 (((? segment-equals-time? first) rest ...)
312 (enq! (time-segment-queue first) proc)
314 ;; If the first segment is more than our time,
315 ;; ours belongs before this one, so add it and
316 ;; start consing our way back
317 (((? segment-more-than-time? first) rest ...)
318 (cons (new-time-segment) segments))
319 ;; Otherwise, build up recursive result
321 (cons first (loop rest)))))
322 (set-schedule-segments!
324 (loop (schedule-segments schedule)))))
326 (define (schedule-empty? schedule)
327 "Check if the SCHEDULE is currently empty"
328 (eq? (schedule-segments schedule) '()))
330 (define (schedule-segments-split schedule time)
331 "Does a multiple value return of time segments before/at and after TIME"
332 (let ((time (time-segment-right-format time)))
333 (define (segment-is-now? segment)
334 (time= (time-segment-time segment) time))
335 (define (segment-is-before-now? segment)
336 (time< (time-segment-time segment) time))
338 (let loop ((segments-before '())
339 (segments-left (schedule-segments schedule)))
341 ;; end of the line, return
343 (values (reverse segments-before) '()))
345 ;; It's right now, so time to stop, but include this one in before
346 ;; but otherwise return
347 (((? segment-is-now? first) rest ...)
348 (values (reverse (cons first segments-before)) rest))
350 ;; This is prior or at now, so add it and keep going
351 (((? segment-is-before-now? first) rest ...)
352 (loop (cons first segments-before) rest))
354 ;; Otherwise it's past now, just return what we have
356 (values segments-before segments-after))))))
358 (define (schedule-extract-until! schedule time)
359 "Extract all segments until TIME from SCHEDULE, and pop old segments off"
360 (receive (segments-before segments-after)
361 (schedule-segments-split schedule time)
362 (set-schedule-segments! schedule segments-after)
365 (define (add-segments-contents-to-queue! segments queue)
368 (let ((seg-queue (time-segment-queue segment)))
369 (while (not (q-empty? seg-queue))
370 (enq! queue (deq! seg-queue)))))
375 ;;; Request to run stuff
376 ;;; ====================
378 (define-record-type <run-request>
379 (make-run-request proc when)
381 (proc run-request-proc)
382 (when run-request-when))
384 (define* (run-it proc #:optional when)
385 "Make a request to run PROC (possibly at WHEN)"
386 (make-run-request proc when))
388 (define-syntax-rule (wrap body ...)
389 "Wrap contents in a procedure"
393 (define-syntax-rule (wrap-apply body)
394 "Wrap possibly multi-value function in a procedure, applies all arguments"
399 ;; @@: Do we really want `body ...' here?
400 ;; what about just `body'?
401 (define-syntax-rule (run body ...)
402 "Run everything in BODY but wrap in a convenient procedure"
403 (make-run-request (wrap body ...) #f))
405 (define-syntax-rule (run-at body ... when)
407 (make-run-request (wrap body ...) when))
409 ;; @@: Is it okay to overload the term "delay" like this?
410 ;; Would `run-in' be better?
411 (define-syntax-rule (run-delay body ... delay-time)
412 "Run BODY at DELAY-TIME time from now"
413 (make-run-request (wrap body ...) (tdelta delay-time)))
416 ;; A request to set up a port with at least one of read, write, except
417 ;; handling processes
419 (define-record-type <port-request>
420 (make-port-request-intern port read write except)
422 (port port-request-port)
423 (read port-request-read)
424 (write port-request-write)
425 (except port-request-except))
427 (define* (make-port-request port #:key read write except)
428 (if (not (or read write except))
429 (throw 'no-port-handler-given "No port handler given.\n"))
430 (make-port-request-intern port read write except))
432 (define port-request make-port-request)
436 ;;; Asynchronous escape to run things
437 ;;; =================================
439 (define (agenda-on-error agenda)
442 (define (agenda-on-fail agenda)
445 (define-syntax-rule (%8sync async-request)
446 "Run BODY asynchronously at a prompt, passing args to make-future.
448 Runs things asynchronously (8synchronously?)"
449 (propagate-%async-exceptions
450 (abort-to-prompt (current-agenda-prompt)
453 ;; Async port request and run-request meta-requests
454 (define (make-async-request proc)
455 "Wrap PROC in an async-request
457 The purpose of this is to make sure that users don't accidentally
458 return the wrong thing via (%8sync) and trip themselves up."
459 (cons '*async-request* proc))
461 (define (setup-async-request resume-kont async-request)
462 "Complete an async request for agenda-run-once's continuation handling"
464 (('*async-request* . async-setup-proc)
465 (async-setup-proc resume-kont))
466 ;; TODO: deliver more helpful errors depending on what the user
468 (_ (throw 'invalid-async-request
469 "Invalid request passed back via an (%8sync) procedure."
472 (define-record-type <wrapped-exception>
473 (make-wrapped-exception key args stacks)
475 (key wrapped-exception-key)
476 (args wrapped-exception-args)
477 (stacks wrapped-exception-stacks))
479 (define-syntax-rule (propagate-%async-exceptions body)
480 (let ((body-result body))
481 (if (wrapped-exception? body-result)
482 (throw '8sync-caught-error
483 (wrapped-exception-key body-result)
484 (wrapped-exception-args body-result)
485 (wrapped-exception-stacks body-result))
488 (define-syntax-rule (%run body ...)
489 (%run-at body ... #f))
491 (define-syntax-rule (%run-at body ... when)
492 ;; Send an asynchronous request to apply a continuation to the
493 ;; following function, then handle that as a request to the agenda
496 ;; We're making a run request
498 ;; Wrapping the following execution to run...
500 ;; Once we get the result from the inner part, we'll resume
501 ;; this continuation, but first
502 ;; @@: Is this running immediately, or queueing the result
503 ;; after evaluation for the next agenda tick? It looks
504 ;; like evaluating immediately. Is that what we want?
506 ;; Any unhandled errors are caught
507 (let ((exception-stack #f))
509 ;; Run the actual code the user requested
512 ;; If something bad happened and we didn't catch it,
513 ;; we'll wrap it up in such a way that the continuation
515 ;; @@: For this stack to work doesn't it have to be
518 ((eq? key '8sync-caught-error)
520 ((orig-key orig-args orig-stacks)
521 (make-wrapped-exception
523 (cons exception-stack orig-stacks)))))
525 (make-wrapped-exception key args
526 (list exception-stack)))))
528 (set! exception-stack (make-stack #t 1 0)))))))
531 (define-syntax-rule (%run-delay body ... delay-time)
532 (%run-at body ... (tdelta delay-time)))
534 (define-syntax-rule (%port-request add-this-port port-request-args ...)
537 (list (make-port-request port-request-args ...)
538 (make-run-request kont)))))
541 (define-syntax-rule (%run-with-return return body ...)
548 (define-syntax-rule (catch-8sync exp (handler-key handler) ...)
549 (catch '8sync-caught-error
552 (lambda (_ orig-key orig-args orig-stacks)
554 ((or (eq? handler-key #t)
555 (eq? orig-key handler-key))
556 (apply handler orig-stacks orig-args)) ...
557 (else (raise '8sync-caught-error
558 orig-key orig-args orig-stacks))))))
561 (define-syntax-rule (catch-%8sync rest ...)
562 (catch-8sync rest ...))
566 ;;; Execution of agenda, and current agenda
567 ;;; =======================================
569 (define %current-agenda (make-parameter #f))
571 (define (update-agenda-from-select! agenda)
572 "Potentially (select) on ports specified in agenda, adding items to queue"
573 (define (hash-keys hash)
574 (hash-map->list (lambda (k v) k) hash))
575 (define (get-wait-time)
576 ;; TODO: we need to figure this out based on whether there's anything
577 ;; in the queue, and if not, how long till the next scheduled item
578 (let ((soonest-time (schedule-soonest-time (agenda-schedule agenda))))
580 ((not (q-empty? (agenda-queue agenda)))
582 (soonest-time ; ie, the agenda is non-empty
583 (let* ((current-time (agenda-time agenda)))
584 (if (time<= soonest-time current-time)
585 ;; Well there's something due so let's select
586 ;; (this avoids a (possible?) race condition chance)
588 (time-minus soonest-time current-time))))
592 ;; TODO: support usecond wait time too
593 (match (get-wait-time)
597 (select (hash-keys (agenda-read-port-map agenda))
598 (hash-keys (agenda-write-port-map agenda))
599 (hash-keys (agenda-except-port-map agenda))
601 (lambda (key . rest-args)
605 (_ (error "Unhandled error in select!" key rest-args))))))))
606 (define (get-procs-to-run)
607 (define (ports->procs ports port-map)
608 (lambda (initial-procs)
612 ((hash-ref port-map port) port))
617 ((read-ports write-ports except-ports)
618 ;; @@: Come on, we can do better than append ;P
619 ((compose (ports->procs
621 (agenda-read-port-map agenda))
624 (agenda-write-port-map agenda))
627 (agenda-except-port-map agenda)))
629 (define (update-agenda)
630 (let ((procs-to-run (get-procs-to-run))
631 (q (agenda-queue agenda)))
637 (define (ports-to-select?)
638 (define (has-items? selector)
640 ;; ... we could use hash-for-each and a continuation to jump
641 ;; out with a #t at first indication of an item
642 (not (= (hash-count (const #t)
645 (or (has-items? agenda-read-port-map)
646 (has-items? agenda-write-port-map)
647 (has-items? agenda-except-port-map)))
649 (if (ports-to-select?)
653 (define (agenda-handle-port-request! agenda port-request)
654 "Update an agenda for a port-request"
655 (define (handle-selector request-selector port-map-selector)
656 (if (request-selector port-request)
657 (hash-set! (port-map-selector agenda)
658 (port-request-port port-request)
659 (request-selector port-request))))
660 (handle-selector port-request-read agenda-read-port-map)
661 (handle-selector port-request-write agenda-write-port-map)
662 (handle-selector port-request-except agenda-except-port-map))
665 (define* (start-agenda agenda
667 (get-time gettimeofday)
668 (handle-ports update-agenda-from-select!))
669 ;; TODO: Document fields
670 "Start up the AGENDA"
671 (let loop ((agenda agenda))
673 ;; @@: Hm, maybe here would be a great place to handle
674 ;; select'ing on ports.
675 ;; We could compose over agenda-run-once and agenda-read-ports
676 (agenda-run-once agenda)))
677 (if (and stop-condition (stop-condition agenda))
680 ;; We have to update the time after ports handled, too
681 ;; because it may have changed after a select
684 ;; Adjust the agenda's time just in time
685 ;; We do this here rather than in agenda-run-once to make
686 ;; agenda-run-once's behavior fairly predictable
687 (set-field agenda (agenda-time) (get-time)))
688 (agenda-time) (get-time))))
689 ;; Update the agenda's current queue based on
690 ;; currently applicable time segments
691 (add-segments-contents-to-queue!
692 (schedule-extract-until! (agenda-schedule agenda) (agenda-time agenda))
693 (agenda-queue agenda))
696 (define (print-error-and-continue . args)
697 "Frequently used as pre-unwind-handler for agenda"
698 (format (current-error-port) "\n*** Caught exception with arguments: ~s ***\n"
700 (display-backtrace (make-stack #t 1 0)
701 (current-error-port))
702 (newline (current-error-port)))
704 (define-syntax-rule (maybe-catch-all (catch-handler pre-unwind-handler)
706 (if (or catch-handler pre-unwind-handler)
711 (or catch-handler (lambda _ #f))
712 (or pre-unwind-handler (lambda _ #f)))
715 (define (agenda-run-once agenda)
716 "Run once through the agenda, and produce a new agenda
717 based on the results"
718 (define (call-proc proc)
720 (agenda-prompt-tag agenda)
722 (parameterize ((%current-agenda agenda))
724 ((agenda-catch-handler agenda)
725 (agenda-pre-unwind-handler agenda))
727 (lambda (kont async-request)
728 (setup-async-request kont async-request))))
730 (let ((queue (agenda-queue agenda))
731 (next-queue (make-q)))
732 (while (not (q-empty? queue))
733 (let* ((proc (q-pop! queue))
734 (proc-result (call-proc proc))
736 (lambda (run-request)
737 (define (schedule-at! time proc)
738 (schedule-add! (agenda-schedule agenda) time proc))
739 (let ((request-time (run-request-when run-request)))
741 ((? time-delta? time-delta)
742 (let ((time (time-delta+ (agenda-time agenda)
744 (schedule-at! time (run-request-proc run-request))))
746 (let ((time (cons sec 0)))
747 (schedule-at! time (run-request-proc run-request))))
748 (((? integer? sec) . (? integer? usec))
749 (schedule-at! request-time (run-request-proc run-request)))
751 (enq! next-queue (run-request-proc run-request))))))))
752 (define (handle-individual result)
754 ((? run-request? new-proc)
756 ((? port-request? port-request)
757 (agenda-handle-port-request! agenda port-request))
760 ;; @@: We might support delay-wrapped procedures here
763 (for-each handle-individual results))
764 (one-result (handle-individual one-result)))))
765 ;; TODO: Alternately, we could return the next-queue
766 ;; along with changes to be added to the schedule here?
767 ;; Return new agenda, with next queue set
768 (set-field agenda (agenda-queue) next-queue)))