Don't freak out on interrupts during (select)!
[8sync.git] / eightsync / agenda.scm
index 8ffe6f18a489a94520f0a1058f0d9a8e3ca1da8a..b26d93b6bdebe328981474e88a3d40f1dd901fef 100644 (file)
@@ -150,6 +150,17 @@ Generally done automatically for the user through (make-agenda)."
   (time time-segment-time)
   (queue time-segment-queue))
 
+;; @@: This seems to be the same as srfi-18's seconds->time procedure?
+;;   Maybe double check and switch to that?  (Thanks amz3!)
+
+(define (time-from-float-or-fraction time)
+  "Produce a (sec . usec) pair from TIME, a float or fraction"
+  (let* ((mixed-whole (floor time))
+         (mixed-rest (- time mixed-whole))  ; float or fraction component
+         (sec mixed-whole)
+         (usec (floor (* 1000000 mixed-rest))))
+    (cons (inexact->exact sec) (inexact->exact usec))))
+
 (define (time-segment-right-format time)
   "Ensure TIME is in the right format.
 
@@ -161,6 +172,8 @@ If an integer, will convert appropriately."
     (((? integer? s) . (? integer? u)) time)
     ;; time was just an integer (just the second)
     ((? integer? _) (cons time 0))
+    ((or (? rational? _) (? inexact? _))
+     (time-from-float-or-fraction time))
     (_ (throw 'invalid-time "Invalid time" time))))
 
 (define* (make-time-segment time #:optional (queue (make-q)))
@@ -199,12 +212,14 @@ run (time-segment-right-format) first."
   (sec time-delta-sec)
   (usec time-delta-usec))
 
-(define* (make-time-delta sec #:optional (usec 0))
+(define* (make-time-delta time)
   "Make a <time-delta> of SEC seconds and USEC microseconds.
 
 This is used primarily so the agenda can recognize RUN-REQUEST objects
-which are meant "
-  (make-time-delta-intern sec usec))
+which are meant to delay computation"
+  (match (time-segment-right-format time)
+    ((sec . usec)
+     (make-time-delta-intern sec usec))))
 
 (define tdelta make-time-delta)
 
@@ -233,13 +248,13 @@ Will produce (0 . 0) instead of a negative number, if needed."
   "Subtract TIME2 from TIME1"
   (time-carry-correct
    (cons (- (car time1) (car time2))
-         (- (cdr time2) (cdr time2)))))
+         (- (cdr time1) (cdr time2)))))
 
 (define (time-plus time1 time2)
   "Add TIME1 and TIME2"
   (time-carry-correct
    (cons (+ (car time1) (car time2))
-         (+ (cdr time2) (cdr time2)))))
+         (+ (cdr time1) (cdr time2)))))
 
 
 (define-record-type <schedule>
@@ -539,10 +554,17 @@ return the wrong thing via (8sync) and trip themselves up."
     ;; TODO: support usecond wait time too
     (match (get-wait-time)
       ((sec . usec)
-       (select (hash-keys (agenda-read-port-map agenda))
-               (hash-keys (agenda-write-port-map agenda))
-               (hash-keys (agenda-except-port-map agenda))
-               sec usec))))
+       (catch 'system-error
+         (lambda ()
+           (select (hash-keys (agenda-read-port-map agenda))
+                   (hash-keys (agenda-write-port-map agenda))
+                   (hash-keys (agenda-except-port-map agenda))
+                   sec usec))
+         (lambda (key . rest-args)
+           (match (pk 'rest-args rest-args)
+             ((_ _ _ (EINTR))
+              '(() () ()))
+             (_ (error "Unhandled error in select!" key rest-args))))))))
   (define (get-procs-to-run)
     (define (ports->procs ports port-map)
       (lambda (initial-procs)