- (with-fd-handler (fd direction (lambda (fd)
- (declare (ignore fd))
- (setf usable t)))
- (loop
- (sub-serve-event to-sec to-usec signalp)
- (when usable
- (return-from wait-until-fd-usable t))
- (when to-sec
- (multiple-value-bind (sec usec)
- (decode-internal-time (get-internal-real-time))
- (setf to-sec (- stop-sec sec))
- (cond ((> usec stop-usec)
- (decf to-sec)
- (setf to-usec (- (+ stop-usec 1000000) usec)))
- (t
- (setf to-usec (- stop-usec usec)))))
- (when (or (minusp to-sec) (minusp to-usec))
- (if signalp
- (progn
- (signal-deadline)
- (go :restart))
- (return-from wait-until-fd-usable nil)))))))))
+ (flet ((maybe-update-timeout ()
+ ;; If we return early, recompute the timeouts, possibly
+ ;; signaling the deadline or returning with NIL to caller.
+ (setf (values to-sec to-usec)
+ (relative-decoded-times stop-sec stop-usec))
+ (when (and (zerop to-sec) (not (plusp to-usec)))
+ (cond (signalp
+ (signal-deadline)
+ (go :restart))
+ (t
+ (return-from wait-until-fd-usable nil))))))
+ (if (and serve-events
+ ;; No timeout or non-zero timeout
+ (or (not to-sec)
+ (not (= 0 to-sec to-usec)))
+ ;; Something to do while we wait
+ (or *descriptor-handlers* *periodic-polling-function*))
+ ;; Loop around SUB-SERVE-EVENT till done.
+ (dx-let ((usable (list nil)))
+ (dx-flet ((usable! (fd)
+ (declare (ignore fd))
+ (setf (car usable) t)))
+ (with-fd-handler (fd direction #'usable!)
+ (loop
+ (sub-serve-event to-sec to-usec signalp)
+ (when (car usable)
+ (return-from wait-until-fd-usable t))
+ (when to-sec
+ (maybe-update-timeout))))))
+ ;; If we don't have to serve events, just poll on the single FD instead.
+ (loop for to-msec = (if (and to-sec to-usec)
+ (+ (* 1000 to-sec) (truncate to-usec 1000))
+ -1)
+ when (or #!+win32 (eq direction :output)
+ (sb!unix:unix-simple-poll fd direction to-msec))
+ do (return-from wait-until-fd-usable t)
+ else
+ do (when to-sec (maybe-update-timeout))))))))