From: Christopher Allan Webber Date: Tue, 1 Dec 2015 15:31:49 +0000 (-0600) Subject: Port removal, stop-on-nothing-to-do, and select when sleeping X-Git-Tag: v0.1.0~27 X-Git-Url: https://jxself.org/git/?a=commitdiff_plain;h=0a4fe47d9b6fe7c13296562b5408f68d0083da71;p=8sync.git Port removal, stop-on-nothing-to-do, and select when sleeping The aligned goal here is to allow for removing ports, not busy-looping when there's no ports but something to wait on, and allow for stopping when nothing is remaining. Accomplishing the latter requires all of the former. * 8sync/agenda.scm (, make-port-remove-request) (port-remove-request port-remove-request?, stop-on-nothing-to-do): New variables. (update-agenda-from-select!): Sleep (via select, currently) when there's no ports in the queue but we still have things scheduled. (start-agenda): New comments --- diff --git a/8sync/agenda.scm b/8sync/agenda.scm index b61cd99..eba0139 100644 --- a/8sync/agenda.scm +++ b/8sync/agenda.scm @@ -64,6 +64,10 @@ port-request-port port-request-read port-request-write port-request-except + + make-port-remove-request port-remove-request port-remove-request? + port-remove-request-port + run-it wrap wrap-apply run run-at run-delay %run %run-at %run-delay %port-request @@ -79,6 +83,8 @@ print-error-and-continue + stop-on-nothing-to-do + %current-agenda start-agenda agenda-run-once)) @@ -619,7 +625,9 @@ return the wrong thing via (%8sync) and trip themselves up." (define %current-agenda (make-parameter #f)) (define (update-agenda-from-select! agenda) - "Potentially (select) on ports specified in agenda, adding items to queue" + "Potentially (select) on ports specified in agenda, adding items to queue. + +Also handles sleeping when all we have to do is wait on the schedule." (define (hash-keys hash) (hash-map->list (lambda (k v) k) hash)) (define (get-wait-time) @@ -696,7 +704,9 @@ return the wrong thing via (%8sync) and trip themselves up." (has-items? agenda-write-port-map) (has-items? agenda-except-port-map))) - (if (ports-to-select?) + (if (or (ports-to-select?) + ;; select doubles as sleep... + (not (schedule-empty? (agenda-schedule agenda)))) (update-agenda) agenda)) @@ -721,8 +731,19 @@ return the wrong thing via (%8sync) and trip themselves up." (hash-remove! (agenda-except-port-map agenda) port))) +(define (stop-on-nothing-to-do agenda) + (and (q-empty? (agenda-queue agenda)) + (schedule-empty? (agenda-schedule agenda)) + (= 0 (hash-count (const #t) (agenda-read-port-map agenda))) + (= 0 (hash-count (const #t) (agenda-write-port-map agenda))) + (= 0 (hash-count (const #t) (agenda-except-port-map agenda))))) + + (define* (start-agenda agenda - #:key stop-condition + #:key + ;; @@: Should we make stop-on-nothing-to-do + ;; the default stop-condition? + stop-condition (get-time gettimeofday) (handle-ports update-agenda-from-select!)) ;; TODO: Document fields @@ -752,6 +773,7 @@ return the wrong thing via (%8sync) and trip themselves up." (agenda-queue agenda)) (loop agenda)))))) + (define (print-error-and-continue key . args) "Frequently used as pre-unwind-handler for agenda" (cond