diff options
| author | Andy Wingo <wingo@pobox.com> | 2016-10-11 23:21:27 +0200 |
|---|---|---|
| committer | Andy Wingo <wingo@pobox.com> | 2016-10-11 23:21:27 +0200 |
| commit | 8451e99bafc253d31a4719d19823b0567c38f472 (patch) | |
| tree | a361246c18d1a3b17d74a52fdec9501645b52f12 | |
| parent | Lighter fibers (diff) | |
| download | guile-fibers-8451e99bafc253d31a4719d19823b0567c38f472.tar.gz | |
Move wake facility to epoll module
* fibers/epoll.scm: Move wake facility here. Note that due to a
miscompilation in Guile 2.1.4, this requires a patch from Git master.
* fibers/internal.scm: Adapt to change.
| -rw-r--r-- | fibers/epoll.scm | 76 | ||||
| -rw-r--r-- | fibers/internal.scm | 98 |
2 files changed, 86 insertions, 88 deletions
diff --git a/fibers/epoll.scm b/fibers/epoll.scm index 356c778..981a01a 100644 --- a/fibers/epoll.scm +++ b/fibers/epoll.scm @@ -18,6 +18,11 @@ ;;;; (define-module (fibers epoll) + #:use-module ((ice-9 binary-ports) #:select (get-u8 put-u8)) + #:use-module (ice-9 atomic) + #:use-module (ice-9 control) + #:use-module (ice-9 match) + #:use-module (ice-9 suspendable-ports) #:use-module (srfi srfi-9) #:use-module (srfi srfi-9 gnu) #:use-module (rnrs bytevectors) @@ -27,6 +32,7 @@ epoll-add! epoll-modify! epoll-remove! + epoll-wake! epoll EPOLLIN EPOLLOUT EPOLLPRO EPOLLERR EPOLLHUP EPOLLET)) @@ -39,11 +45,26 @@ (when (defined? 'EPOLLONESHOT) (export EPOLLONESHOT)) +(define (make-wake-pipe) + (define (set-nonblocking! port) + (fcntl port F_SETFL (logior O_NONBLOCK (fcntl port F_GETFL)))) + (let ((pair (pipe))) + (match pair + ((read-pipe . write-pipe) + (setvbuf write-pipe 'none) + (set-nonblocking! read-pipe) + (set-nonblocking! write-pipe) + (values read-pipe write-pipe))))) + (define-record-type <epoll> - (make-epoll fd eventsv) + (make-epoll fd eventsv state wake-read-pipe wake-write-pipe) epoll? (fd epoll-fd set-epoll-fd!) - (eventsv epoll-eventsv set-epoll-eventsv!)) + (eventsv epoll-eventsv set-epoll-eventsv!) + ;; atomic box of either 'waiting, 'not-waiting or 'dead + (state epoll-state) + (wake-read-pipe epoll-wake-read-pipe) + (wake-write-pipe epoll-wake-write-pipe)) (define-syntax events-offset (lambda (x) @@ -67,12 +88,21 @@ (add-hook! after-gc-hook pump-epoll-guardian) (define* (epoll-create #:key (close-on-exec? #t)) - (let ((epoll (make-epoll (primitive-epoll-create close-on-exec?) #f))) - (epoll-guardian epoll) - epoll)) + (call-with-values (lambda () (make-wake-pipe)) + (lambda (read-pipe write-pipe) + (let* ((state (make-atomic-box 'not-waiting)) + (epoll (make-epoll (primitive-epoll-create close-on-exec?) #f + state read-pipe write-pipe))) + (epoll-guardian epoll) + (epoll-add! epoll (fileno read-pipe) EPOLLIN) + epoll)))) (define (epoll-destroy epoll) + (atomic-box-set! (epoll-state epoll) 'dead) (when (epoll-fd epoll) + (close-port (epoll-wake-read-pipe epoll)) + ;; FIXME: ignore errors flushing output + (close-port (epoll-wake-write-pipe epoll)) (close-fdes (epoll-fd epoll)) (set-epoll-fd! epoll #f))) @@ -85,6 +115,21 @@ (define (epoll-remove! epoll fd) (primitive-epoll-ctl (epoll-fd epoll) EPOLL_CTL_DEL fd)) +(define (epoll-wake! epoll) + "Run after modifying the shared state used by a thread that might be +waiting on this epoll descriptor, to break that thread out of the +epoll wait (if appropriate)." + (match (atomic-box-ref (epoll-state epoll)) + ;; It is always correct to wake an epoll via the pipe. However we + ;; can avoid it if the epoll is guaranteed to see that the + ;; runqueue is not empty before it goes to poll next time. + ('waiting + (let/ec cancel + (parameterize ((current-write-waiter cancel)) + (put-u8 (epoll-wake-write-pipe epoll) #x00)))) + ('not-waiting #t) + ('dead (error "epoll instance is dead")))) + (define (epoll-default-folder fd events seed) (acons fd events seed)) @@ -98,14 +143,25 @@ (set-epoll-eventsv! epoll v) v)))) -(define* (epoll epoll #:optional maxevents (timeout -1) +(define* (epoll epoll #:optional maxevents (timeout-fn (lambda () -1)) #:key (folder epoll-default-folder) (seed '())) + (atomic-box-set! (epoll-state epoll) 'waiting) (let* ((eventsv (ensure-epoll-eventsv epoll maxevents)) - (n (primitive-epoll-wait (epoll-fd epoll) eventsv timeout))) + (n (primitive-epoll-wait (epoll-fd epoll) eventsv (timeout-fn))) + (read-pipe (epoll-wake-read-pipe epoll)) + (read-pipe-fd (fileno read-pipe))) + (atomic-box-set! (epoll-state epoll) 'not-waiting) (let lp ((seed seed) (i 0)) (if (< i n) - (lp (folder (bytevector-s32-native-ref eventsv (fd-offset i)) - (bytevector-u32-native-ref eventsv (events-offset i)) + (let ((fd (bytevector-s32-native-ref eventsv (fd-offset i))) + (events (bytevector-u32-native-ref eventsv (events-offset i)))) + (lp (if (eqv? fd read-pipe-fd) + (begin + (let/ec cancel + ;; Slurp off any wake bytes from the fd. + (parameterize ((current-read-waiter cancel)) + (let lp () (get-u8 read-pipe) (lp)))) seed) - (1+ i)) + (folder fd events seed)) + (1+ i))) seed)))) diff --git a/fibers/internal.scm b/fibers/internal.scm index 68855b1..360c0a6 100644 --- a/fibers/internal.scm +++ b/fibers/internal.scm @@ -24,11 +24,8 @@ #:use-module (fibers psq) #:use-module (fibers nameset) #:use-module (ice-9 atomic) - #:use-module ((ice-9 control) #:select (let/ec)) - #:use-module ((ice-9 binary-ports) #:select (get-u8 put-u8)) - #:use-module (ice-9 fdes-finalizers) #:use-module (ice-9 match) - #:use-module (ice-9 suspendable-ports) + #:use-module (ice-9 fdes-finalizers) #:export (;; Low-level interface: schedulers and threads. make-scheduler with-scheduler @@ -69,8 +66,7 @@ (define-record-type <scheduler> (%make-scheduler name epfd active-fd-count prompt-tag runqueue - sources timers inbox-state wake-pipe - kernel-thread) + sources timers kernel-thread) scheduler? (name scheduler-name set-scheduler-name!) (epfd scheduler-epfd) @@ -82,10 +78,6 @@ (sources scheduler-sources) ;; PSQ of thunk -> expiry (timers scheduler-timers set-scheduler-timers!) - ;; atomic box of either 'will-check, 'needs-wake or 'dead - (inbox-state scheduler-inbox-state) - ;; (read-pipe . write-pipe) - (wake-pipe scheduler-wake-pipe) ;; atomic parameter of thread (kernel-thread scheduler-kernel-thread)) @@ -100,17 +92,6 @@ ;; for suspended, a continuation; for finished, a list of values. (data fiber-data set-fiber-data!)) -(define (make-wake-pipe) - (define (set-nonblocking! port) - (fcntl port F_SETFL (logior O_NONBLOCK (fcntl port F_GETFL)))) - (let ((pair (pipe))) - (match pair - ((read-pipe . write-pipe) - (setvbuf write-pipe 'none) - (set-nonblocking! read-pipe) - (set-nonblocking! write-pipe) - pair)))) - (define (make-atomic-parameter init) (let ((box (make-atomic-box init))) (case-lambda @@ -122,7 +103,6 @@ (unless (eq? prev init) (error "owned by other thread" prev)))))))) -;; FIXME: Perhaps epoll should handle the wake pipe business itself (define (make-scheduler) (let ((epfd (epoll-create)) (active-fd-count 0) @@ -132,15 +112,9 @@ (timers (make-psq (match-lambda* (((t1 . c1) (t2 . c2)) (< t1 t2))) <)) - (inbox-state (make-atomic-box 'will-check)) - (wake-pipe (make-wake-pipe)) (kernel-thread (make-atomic-parameter #f))) - (match wake-pipe - ((read-pipe . _) - (epoll-add! epfd (fileno read-pipe) EPOLLIN))) (let ((sched (%make-scheduler #f epfd active-fd-count prompt-tag - runqueue sources timers - inbox-state wake-pipe kernel-thread))) + runqueue sources timers kernel-thread))) (set-scheduler-name! sched (nameset-add! schedulers-nameset sched)) sched))) @@ -172,31 +146,17 @@ (unless (eq? tail tail*) (lp tail*))))) -(define (wake-remote-scheduler! sched) - (match (scheduler-wake-pipe sched) - ((_ . write-pipe) - (let/ec cancel - (parameterize ((current-write-waiter cancel)) - (put-u8 write-pipe #x00)))))) - (define (schedule-fiber! fiber thunk) - ;; The fiber will be woken at most once, and we are the ones that - ;; will wake it, so we can set the thunk directly. Adding the fiber - ;; to the runqueue is an atomic operation with SEQ_CST ordering, so - ;; that will make sure this operation is visible even for a fiber - ;; scheduled on a remote thread. + ;; The fiber will be resumed at most once, and we are the ones that + ;; will resume it, so we can set the thunk directly. Adding the + ;; fiber to the runqueue is an atomic operation with SEQ_CST + ;; ordering, so that will make sure this operation is visible even + ;; for a fiber scheduled on a remote thread. (set-fiber-data! fiber thunk) (let ((sched (fiber-scheduler fiber))) (enqueue! (scheduler-runqueue sched) fiber) (unless (eq? sched (current-scheduler)) - (match (atomic-box-ref (scheduler-inbox-state sched)) - ;; It is always correct to wake the scheduler via the pipe. - ;; However we can avoid it if the scheduler is guaranteed to - ;; see that the runqueue is not empty before it goes to poll - ;; next time. - ('will-check #t) - ('needs-wake (wake-remote-scheduler! sched)) - ('dead (error "Scheduler is dead")))) + (epoll-wake! (scheduler-epfd sched))) (values))) (define internal-time-units-per-millisecond @@ -204,17 +164,7 @@ (define (schedule-fibers-for-fd fd revents sched) (match (hashv-ref (scheduler-sources sched) fd) - (#f - (match (scheduler-wake-pipe sched) - ((read-pipe . _) - (cond - ((eqv? fd (fileno read-pipe)) - ;; Slurp off any wake bytes from the fd. - (let/ec cancel - (parameterize ((current-write-waiter cancel)) - (let lp () (get-u8 read-pipe) (lp))))) - (else - (warn "scheduler for unknown fd" fd)))))) + (#f (warn "scheduler for unknown fd" fd)) (sources (set-scheduler-active-fd-count! sched (1- (scheduler-active-fd-count sched))) @@ -242,7 +192,10 @@ ;; Don't sleep if there are fibers in the runqueue already. 0) ((psq-empty? (scheduler-timers sched)) - -1) + ;; If there are no timers, only sleep if there are active fd's. (?) + (cond + ((zero? (scheduler-active-fd-count sched)) 0) + (else -1))) (else (match (psq-min (scheduler-timers sched)) ((expiry . thunk) @@ -276,17 +229,12 @@ ;; In any case, check the kernel to see if any of the fd's that we ;; are interested in are active, and in that case schedule their ;; corresponding fibers. Also run any timers that have timed out. - (let ((timeout (scheduler-poll-timeout sched))) - (unless (and (not (zero? timeout)) - (zero? (scheduler-active-fd-count sched))) - (atomic-box-set! (scheduler-inbox-state sched) 'needs-wake) - (epoll (scheduler-epfd sched) - 32 ; maxevents - timeout - #:folder (lambda (fd revents seed) - (schedule-fibers-for-fd fd revents sched) - seed)) - (atomic-box-set! (scheduler-inbox-state sched) 'will-check))) + (epoll (scheduler-epfd sched) + 32 ; maxevents + (lambda () (scheduler-poll-timeout sched)) + #:folder (lambda (fd revents seed) + (schedule-fibers-for-fd fd revents sched) + seed)) (run-timers sched)) (define* (run-fiber fiber) @@ -320,12 +268,6 @@ (define (destroy-scheduler sched) #; (for-each kill-fiber (list-copy (scheduler-fibers sched))) - (atomic-box-set! (scheduler-inbox-state sched) 'dead) - (match (scheduler-wake-pipe sched) - ((read-pipe . write-pipe) - (close-port read-pipe) - ;; FIXME: ignore errors flushing output - (close-port write-pipe))) (epoll-destroy (scheduler-epfd sched))) (define (create-fiber sched thunk) |
