agenda: Various %port-foo enhancements
[8sync.git] / 8sync / agenda.scm
index b61cd99cd4388489a830d20bc996be169eebd9cc..da398458db34096d2893d61286456ee9ad156ab1 100644 (file)
             port-request-port
             port-request-read port-request-write port-request-except
 
+            <port-remove-request>
+            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 
-            %8sync-run %8sync-run-at %8sync-run-delay %8sync-port
+            %run %run-at %run-delay %port-request %port-remove-request
+            %8sync-run %8sync-run-at %8sync-run-delay
+            %8sync-port %8sync-port-remove
             
             catch-8sync catch-%8sync
 
@@ -79,6 +84,8 @@
 
             print-error-and-continue
 
+            stop-on-nothing-to-do
+
             %current-agenda
             start-agenda agenda-run-once))
 
@@ -558,13 +565,13 @@ return the wrong thing via (%8sync) and trip themselves up."
   (make-async-request
    (lambda (kont)
      (list (make-port-request port port-request-args ...)
-           (make-run-request kont)))))
+           (make-run-request kont #f)))))
 
 (define-syntax-rule (%port-remove-request port)
   (make-async-request
    (lambda (kont)
      (list (make-port-remove-request port)
-           (make-run-request kont)))))
+           (make-run-request kont #f)))))
 
 
 ;; Sugar
@@ -584,6 +591,10 @@ return the wrong thing via (%8sync) and trip themselves up."
   "Sugar for (%8sync (%port-request ...))"
   (%8sync (%port-request rest ...)))
 
+(define-syntax-rule (%8sync-port-remove rest ...)
+  "Sugar for (%8sync (%port-remove-request ...))"
+  (%8sync (%port-remove-request rest ...)))
+
 
 ;; TODO: Write (%run-immediately)
 
@@ -619,7 +630,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 +709,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 +736,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 +778,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