From 0a4fe47d9b6fe7c13296562b5408f68d0083da71 Mon Sep 17 00:00:00 2001 From: Christopher Allan Webber Date: Tue, 1 Dec 2015 09:31:49 -0600 Subject: [PATCH] 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 --- 8sync/agenda.scm | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) 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 -- 2.31.1