1 ;;; 8sync --- Asynchronous programming for Guile
2 ;;; Copyright (C) 2015 Christopher Allan Webber <cwebber@dustycloud.org>
4 ;;; This file is part of 8sync.
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.
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.
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/>.
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)
28 agenda-queue agenda-prompt-tag
29 agenda-read-port-map agenda-write-port-map agenda-except-port-map
37 make-time-segment time-segment?
38 time-segment-time time-segment-queue
40 time< time= time<= time-delta+
44 make-time-delta tdelta time-delta?
45 time-delta-sec time-delta-usec
48 make-schedule schedule?
49 schedule-add! schedule-empty?
53 schedule-segments-split schedule-extract-until!
54 add-segments-contents-to-queue!
59 make-run-request run-request?
60 run-request-proc run-request-when
63 make-port-request port-request port-request?
65 port-request-read port-request-write port-request-except
67 run-it wrap wrap-apply run run-at run-delay
69 %run %run-at %run-delay %port-request
70 %8sync-run %8sync-run-at %8sync-run-delay %8sync-port
72 catch-8sync catch-%8sync
74 ;; used for introspecting the error, but a method for making
77 wrapped-exception-key wrapped-exception-args
78 wrapped-exception-stacks
80 print-error-and-continue
83 start-agenda agenda-run-once))
85 ;; @@: Using immutable agendas here, so wouldn't it make sense to
86 ;; replace this queue stuff with using pfds based immutable queues?
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
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?
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)
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)
118 (catch-handler agenda-catch-handler)
119 (pre-unwind-handler agenda-pre-unwind-handler))
121 (define (make-async-prompt-tag)
122 "Make an async prompt tag for an agenda.
124 Generally done automatically for the user through (make-agenda)."
125 (make-prompt-tag "prompt"))
127 (define* (make-agenda #:key
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))
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
142 catch-handler pre-unwind-handler))
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)
150 "Can't get current agenda prompt if there's no current agenda!")
151 (agenda-prompt-tag current-agenda))))
153 ;; helper for making queues for an agenda
154 (define (list->q lst)
155 "Makes a queue composed of LST items"
163 (define (make-q* . args)
164 "Makes a queue and populates it with this invocation's ARGS"
171 ;;; This is where we handle timed events for the future
173 ;; This section totally borrows from the ideas in SICP
176 ;; NOTE: time is a cons of (seconds . microseconds)
178 (define-record-type <time-segment>
179 (make-time-segment-intern time queue)
181 (time time-segment-time)
182 (queue time-segment-queue))
184 ;; @@: This seems to be the same as srfi-18's seconds->time procedure?
185 ;; Maybe double check and switch to that? (Thanks amz3!)
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
192 (usec (floor (* 1000000 mixed-rest))))
193 (cons (inexact->exact sec) (inexact->exact usec))))
195 (define (time-segment-right-format time)
196 "Ensure TIME is in the right format.
198 The right format means (second . microsecond).
199 If an integer, will convert appropriately."
200 ;; TODO: add floating point / rational number support.
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))))
210 (define* (make-time-segment time #:optional (queue (make-q)))
211 "Make a time segment of TIME and QUEUE
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))
217 (define (time< time1 time2)
218 "Check if TIME1 is less than TIME2"
219 (cond ((< (car time1)
229 (define (time= time1 time2)
230 "Check whether TIME1 and TIME2 are equivalent"
231 (and (= (car time1) (car time2))
232 (= (cdr time1) (cdr time2))))
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)))
240 (define-record-type <time-delta>
241 (make-time-delta-intern sec usec)
244 (usec time-delta-usec))
246 (define* (make-time-delta time)
247 "Make a <time-delta> of SEC seconds and USEC microseconds.
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)
253 (make-time-delta-intern sec usec))))
255 (define tdelta make-time-delta)
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)
263 (- (cdr time) 1000000)))
269 (+ (cdr time) 1000000))))
272 (define (time-delta+ time time-delta)
273 "Increment a TIME by the value of TIME-DELTA"
275 (cons (+ (car time) (time-delta-sec time-delta))
276 (+ (cdr time) (time-delta-usec time-delta)))))
278 (define (time-minus time1 time2)
279 "Subtract TIME2 from TIME1"
281 (cons (- (car time1) (car time2))
282 (- (cdr time1) (cdr time2)))))
284 (define (time-plus time1 time2)
285 "Add TIME1 and TIME2"
287 (cons (+ (car time1) (car time2))
288 (+ (cdr time1) (cdr time2)))))
291 (define-record-type <schedule>
292 (make-schedule-intern segments)
294 (segments schedule-segments set-schedule-segments!))
296 (define* (make-schedule #:optional segments)
297 "Make a schedule, optionally pre-composed of SEGMENTS"
298 (make-schedule-intern (or segments '())))
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 '())
305 (time-segment-time (car segments)))))
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)
316 (make-time-segment time)))
317 (enq! (time-segment-queue new-segment) proc)
319 (define (loop segments)
320 (define (segment-equals-time? segment)
321 (time= time (time-segment-time segment)))
323 (define (segment-more-than-time? segment)
324 (time< time (time-segment-time segment)))
326 ;; We could switch this out to be more mutate'y
327 ;; and avoid the O(n) of space... is that over-optimizing?
329 ;; If we're at the end of the list, time to make a new
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)
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
344 (cons first (loop rest)))))
345 (set-schedule-segments!
347 (loop (schedule-segments schedule)))))
349 (define (schedule-empty? schedule)
350 "Check if the SCHEDULE is currently empty"
351 (eq? (schedule-segments schedule) '()))
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))
361 (let loop ((segments-before '())
362 (segments-left (schedule-segments schedule)))
364 ;; end of the line, return
366 (values (reverse segments-before) '()))
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))
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))
377 ;; Otherwise it's past now, just return what we have
379 (values segments-before segments-after))))))
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)
388 (define (add-segments-contents-to-queue! segments queue)
391 (let ((seg-queue (time-segment-queue segment)))
392 (while (not (q-empty? seg-queue))
393 (enq! queue (deq! seg-queue)))))
398 ;;; Request to run stuff
399 ;;; ====================
401 (define-record-type <run-request>
402 (make-run-request proc when)
404 (proc run-request-proc)
405 (when run-request-when))
407 (define* (run-it proc #:optional when)
408 "Make a request to run PROC (possibly at WHEN)"
409 (make-run-request proc when))
411 (define-syntax-rule (wrap body ...)
412 "Wrap contents in a procedure"
416 (define-syntax-rule (wrap-apply body)
417 "Wrap possibly multi-value function in a procedure, applies all arguments"
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))
428 (define-syntax-rule (run-at body ... when)
430 (make-run-request (wrap body ...) when))
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)))
439 ;; A request to set up a port with at least one of read, write, except
440 ;; handling processes
442 (define-record-type <port-request>
443 (make-port-request-intern port read write except)
445 (port port-request-port)
446 (read port-request-read)
447 (write port-request-write)
448 (except port-request-except))
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))
455 (define port-request make-port-request)
457 (define-record-type <port-remove-request>
458 (make-port-remove-request port)
460 (port port-remove-request-port))
462 (define port-remove-request make-port-remove-request)
466 ;;; Asynchronous escape to run things
467 ;;; =================================
469 (define-syntax-rule (%8sync async-request)
470 "Run BODY asynchronously at a prompt, passing args to make-future.
472 Runs things asynchronously (8synchronously?)"
473 (propagate-%async-exceptions
474 (abort-to-prompt (current-agenda-prompt)
477 ;; Async port request and run-request meta-requests
478 (define (make-async-request proc)
479 "Wrap PROC in an async-request
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))
485 (define (setup-async-request resume-kont async-request)
486 "Complete an async request for agenda-run-once's continuation handling"
488 (('*async-request* . async-setup-proc)
489 (async-setup-proc resume-kont))
490 ;; TODO: deliver more helpful errors depending on what the user
492 (_ (throw 'invalid-async-request
493 "Invalid request passed back via an (%8sync) procedure."
496 (define-record-type <wrapped-exception>
497 (make-wrapped-exception key args stacks)
499 (key wrapped-exception-key)
500 (args wrapped-exception-args)
501 (stacks wrapped-exception-stacks))
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))
512 (define-syntax-rule (%run body ...)
513 (%run-at body ... #f))
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
520 ;; We're making a run request
522 ;; Wrapping the following execution to run...
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?
530 ;; Any unhandled errors are caught
531 (let ((exception-stack #f))
533 ;; Run the actual code the user requested
536 ;; If something bad happened and we didn't catch it,
537 ;; we'll wrap it up in such a way that the continuation
541 ((eq? key '8sync-caught-error)
543 ((orig-key orig-args orig-stacks)
544 (make-wrapped-exception
546 (cons exception-stack orig-stacks)))))
548 (make-wrapped-exception key args
549 (list exception-stack)))))
551 (set! exception-stack (make-stack #t 1 0)))))))
554 (define-syntax-rule (%run-delay body ... delay-time)
555 (%run-at body ... (tdelta delay-time)))
557 (define-syntax-rule (%port-request port port-request-args ...)
560 (list (make-port-request port port-request-args ...)
561 (make-run-request kont)))))
563 (define-syntax-rule (%port-remove-request port)
566 (list (make-port-remove-request port)
567 (make-run-request kont)))))
571 (define-syntax-rule (%8sync-run rest ...)
572 "Sugar for (%8sync (%run ...))"
573 (%8sync (%run rest ...)))
575 (define-syntax-rule (%8sync-run-at rest ...)
576 "Sugar for (%8sync (%run-at ...))"
577 (%8sync (%run-at rest ...)))
579 (define-syntax-rule (%8sync-run-delay rest ...)
580 "Sugar for (%8sync (%run-delay ...))"
581 (%8sync (%run-delay rest ...)))
583 (define-syntax-rule (%8sync-port rest ...)
584 "Sugar for (%8sync (%port-request ...))"
585 (%8sync (%port-request rest ...)))
588 ;; TODO: Write (%run-immediately)
591 (define-syntax-rule (%run-with-return return body ...)
598 (define-syntax-rule (catch-8sync exp (handler-key handler) ...)
599 (catch '8sync-caught-error
602 (lambda (_ orig-key orig-args orig-stacks)
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))))))
611 (define-syntax-rule (catch-%8sync rest ...)
612 (catch-8sync rest ...))
616 ;;; Execution of agenda, and current agenda
617 ;;; =======================================
619 (define %current-agenda (make-parameter #f))
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))))
630 ((not (q-empty? (agenda-queue agenda)))
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)
638 (time-minus soonest-time current-time))))
642 ;; TODO: support usecond wait time too
643 (match (get-wait-time)
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))
651 (lambda (key . rest-args)
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)
662 ((hash-ref port-map port) port))
667 ((read-ports write-ports except-ports)
668 ;; @@: Come on, we can do better than append ;P
669 ((compose (ports->procs
671 (agenda-read-port-map agenda))
674 (agenda-write-port-map agenda))
677 (agenda-except-port-map agenda)))
679 (define (update-agenda)
680 (let ((procs-to-run (get-procs-to-run))
681 (q (agenda-queue agenda)))
687 (define (ports-to-select?)
688 (define (has-items? selector)
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)
695 (or (has-items? agenda-read-port-map)
696 (has-items? agenda-write-port-map)
697 (has-items? agenda-except-port-map)))
699 (if (ports-to-select?)
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))
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)))
724 (define* (start-agenda agenda
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))
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))
739 ;; We have to update the time after ports handled, too
740 ;; because it may have changed after a select
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))
755 (define (print-error-and-continue key . args)
756 "Frequently used as pre-unwind-handler for agenda"
758 ((eq? key '8sync-caught-error)
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"
765 (display "* Caught stacks below (ending with original) *\n\n")
768 (display-backtrace s (current-error-port))
769 (newline (current-error-port)))
772 (format (current-error-port)
773 "\n*** Caught exception with key '~s and arguments: ~s ***\n"
775 (display-backtrace (make-stack #t 1 0)
776 (current-error-port))
777 (newline (current-error-port)))))
779 (define-syntax-rule (maybe-catch-all (catch-handler pre-unwind-handler)
781 (if (or catch-handler pre-unwind-handler)
786 (or catch-handler (lambda _ #f))
787 (or pre-unwind-handler (lambda _ #f)))
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)
795 (agenda-prompt-tag agenda)
797 (parameterize ((%current-agenda agenda))
799 ((agenda-catch-handler agenda)
800 (agenda-pre-unwind-handler agenda))
802 (lambda (kont async-request)
803 (setup-async-request kont async-request))))
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))
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)))
816 ((? time-delta? time-delta)
817 (let ((time (time-delta+ (agenda-time agenda)
819 (schedule-at! time (run-request-proc run-request))))
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)))
826 (enq! next-queue (run-request-proc run-request))))))))
827 (define (handle-individual result)
828 ;; @@: Could maybe optimize by switching to an explicit cond...
830 ((? run-request? 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))
838 ;; @@: We might support delay-wrapped procedures here
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)))