diff options
| -rw-r--r-- | Makefile.am | 1 | ||||
| -rw-r--r-- | fibers.scm | 26 | ||||
| -rw-r--r-- | fibers/internal.scm | 82 | ||||
| -rw-r--r-- | fibers/interrupts.scm | 73 |
4 files changed, 121 insertions, 61 deletions
diff --git a/Makefile.am b/Makefile.am index eaad197..968b0b3 100644 --- a/Makefile.am +++ b/Makefile.am @@ -30,6 +30,7 @@ SOURCES = \ fibers/deque.scm \ fibers/epoll.scm \ fibers/internal.scm \ + fibers/interrupts.scm \ fibers/nameset.scm \ fibers/operations.scm \ fibers/posix-clocks.scm \ @@ -21,6 +21,7 @@ #:use-module (fibers internal) #:use-module (fibers repl) #:use-module (fibers timers) + #:use-module (fibers interrupts) #:use-module ((ice-9 ports internal) #:select (port-read-wait-fd port-write-wait-fd)) #:use-module (ice-9 suspendable-ports) @@ -39,7 +40,7 @@ (resume-on-writable-fd (port-read-wait-fd port) fiber)))) (define* (run-fibers #:optional (init #f) - #:key (hz 0) (scheduler (make-scheduler #:hz hz)) + #:key (hz 0) (scheduler (make-scheduler)) (install-suspendable-ports? #t) (keep-scheduler? (eq? scheduler (current-scheduler)))) (when install-suspendable-ports? (install-suspendable-ports!)) @@ -47,16 +48,19 @@ scheduler (parameterize ((current-read-waiter wait-for-readable) (current-write-waiter wait-for-writable)) - (let ((ret #f)) - (spawn-fiber (lambda () - (call-with-values (or init values) - (lambda vals (set! ret vals)))) - scheduler) - (let lp () - (run-scheduler scheduler) - (unless ret (lp))) - (unless keep-scheduler? (destroy-scheduler scheduler)) - (apply values ret))))) + (with-interrupts + hz yield-current-fiber + (lambda () + (let ((ret #f)) + (spawn-fiber (lambda () + (call-with-values (or init values) + (lambda vals (set! ret vals)))) + scheduler) + (let lp () + (run-scheduler scheduler) + (unless ret (lp))) + (unless keep-scheduler? (destroy-scheduler scheduler)) + (apply values ret))))))) (define (require-current-scheduler) (or (current-scheduler) diff --git a/fibers/internal.scm b/fibers/internal.scm index a80e478..033b714 100644 --- a/fibers/internal.scm +++ b/fibers/internal.scm @@ -53,7 +53,8 @@ fiber-by-name suspend-current-fiber - resume-fiber)) + resume-fiber + yield-current-fiber)) (define-once fibers-nameset (make-nameset)) (define-once schedulers-nameset (make-nameset)) @@ -78,7 +79,7 @@ name is known." (define-record-type <scheduler> (%make-scheduler name epfd active-fd-count prompt-tag runqueue - sources timers kernel-thread hz) + sources timers kernel-thread) scheduler? (name scheduler-name set-scheduler-name!) (epfd scheduler-epfd) @@ -91,8 +92,7 @@ name is known." ;; PSQ of thunk -> expiry (timers scheduler-timers set-scheduler-timers!) ;; atomic parameter of thread - (kernel-thread scheduler-kernel-thread) - (hz scheduler-hz)) + (kernel-thread scheduler-kernel-thread)) (define-record-type <fiber> (make-fiber scheduler continuation) @@ -115,7 +115,7 @@ name is known." (unless (eq? prev init) (error "owned by other thread" prev)))))))) -(define* (make-scheduler #:key (hz 0)) +(define* (make-scheduler) "Make a new scheduler in which to run fibers." (let ((epfd (epoll-create)) (active-fd-count 0) @@ -127,8 +127,7 @@ name is known." <)) (kernel-thread (make-atomic-parameter #f))) (let ((sched (%make-scheduler #f epfd active-fd-count prompt-tag - runqueue sources timers kernel-thread - hz))) + runqueue sources timers kernel-thread))) (set-scheduler-name! sched (nameset-add! schedulers-nameset sched)) sched))) @@ -268,53 +267,24 @@ current." (set-fiber-continuation! fiber k) (after-suspend fiber))))) -;; Using SIGPROF for preemption prevents using it for other purposes -;; like profiling. A better solution would be clock_nanosleep on the -;; CLOCK_PROCESS_CPUTIME_ID clock from a separate thread. -(define (maybe-preemptive sched thunk) - (let ((hz (scheduler-hz sched)) - (tag (scheduler-prompt-tag sched)) - (prev #f)) - (define (sigprof-handler _) - (when (suspendable-continuation? tag) - (suspend-current-fiber - (lambda (fiber) - (resume-fiber fiber (lambda () (values))))))) - - (define (start-preemption!) - (let ((period-usecs (inexact->exact (round (/ 1e6 hz))))) - (set! prev (car (sigaction SIGPROF sigprof-handler))) - (setitimer ITIMER_PROF 0 period-usecs 0 period-usecs))) - - (define (stop-preemption!) - (setitimer ITIMER_PROF 0 0 0 0) - (sigaction SIGPROF prev)) - - (if (zero? hz) - (thunk) - (dynamic-wind start-preemption! thunk stop-preemption!)))) - (define* (run-scheduler sched) "Run @var{sched} until there are no more fibers ready to run, no file descriptors being waited on, and no more timers pending to run. Return zero values." - (maybe-preemptive - sched - (lambda () - (let lp () - (schedule-runnables-for-next-turn sched) - (match (dequeue-all! (scheduler-runqueue sched)) - (() - ;; Could be the scheduler is stopping, or it could be that we - ;; got a spurious wakeup. In any case, this is the place to - ;; check to see whether the scheduler is really done. - (cond - ((not (zero? (scheduler-active-fd-count sched))) (lp)) - ((not (psq-empty? (scheduler-timers sched))) (lp)) - (else (values)))) - (runnables - (for-each run-fiber runnables) - (lp))))))) + (let lp () + (schedule-runnables-for-next-turn sched) + (match (dequeue-all! (scheduler-runqueue sched)) + (() + ;; Could be the scheduler is stopping, or it could be that we + ;; got a spurious wakeup. In any case, this is the place to + ;; check to see whether the scheduler is really done. + (cond + ((not (zero? (scheduler-active-fd-count sched))) (lp)) + ((not (psq-empty? (scheduler-timers sched))) (lp)) + (else (values)))) + (runnables + (for-each run-fiber runnables) + (lp))))) (define (destroy-scheduler sched) "Release any resources associated with @var{sched}." @@ -360,6 +330,18 @@ even if @var{fiber} is running on a remote scheduler." (unless cont (error "invalid fiber" fiber)) (schedule-fiber! fiber (lambda () (cont thunk))))) +(define* (yield-current-fiber) + "Yield control to the current scheduler. Like +@code{suspend-current-fiber} followed directly by @code{resume-fiber}, +except that it avoids suspending if the current continuation isn't +suspendable. Returns @code{#t} if the yield succeeded, or @code{#f} +otherwise." + (let ((tag (scheduler-prompt-tag (current-scheduler)))) + (and (suspendable-continuation? tag) + (begin + (abort-to-prompt tag (lambda (fiber) (resume-fiber fiber #f))) + #t)))) + (define (finalize-fd sched fd) "Remove data associated with @var{fd} from the scheduler @var{ctx}. Called by Guile just before Guile goes to close a file descriptor, in diff --git a/fibers/interrupts.scm b/fibers/interrupts.scm new file mode 100644 index 0000000..10f64aa --- /dev/null +++ b/fibers/interrupts.scm @@ -0,0 +1,73 @@ +;; Fibers: cooperative, event-driven user-space threads. + +;;;; Copyright (C) 2016 Free Software Foundation, Inc. +;;;; +;;;; This library is free software; you can redistribute it and/or +;;;; modify it under the terms of the GNU Lesser General Public +;;;; License as published by the Free Software Foundation; either +;;;; version 3 of the License, or (at your option) any later version. +;;;; +;;;; This library is distributed in the hope that it will be useful, +;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +;;;; Lesser General Public License for more details. +;;;; +;;;; You should have received a copy of the GNU Lesser General Public +;;;; License along with this library; if not, write to the Free Software +;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +;;;; + +(define-module (fibers interrupts) + #:use-module (ice-9 threads) + #:use-module (fibers posix-clocks) + #:export (with-interrupts)) + +;; Cause periodic interrupts via `setitimer' and SIGPROF. This +;; implementation has the disadvantage that it prevents `setitimer' +;; from being used for other purposes like profiling. +(define (with-interrupts/sigprof hz interrupt thunk) + (let ((prev #f)) + (define (sigprof-handler _) (interrupt)) + + (define (start-preemption!) + (let ((period-usecs (inexact->exact (round (/ 1e6 hz))))) + (set! prev (car (sigaction SIGPROF sigprof-handler))) + (setitimer ITIMER_PROF 0 period-usecs 0 period-usecs))) + + (define (stop-preemption!) + (setitimer ITIMER_PROF 0 0 0 0) + (sigaction SIGPROF prev)) + + (dynamic-wind start-preemption! thunk stop-preemption!))) + +;; Cause periodic interrupts via a separate thread sleeping on a clock +;; driven by the current thread's CPU time. +(define (with-interrupts/thread-cputime hz interrupt thunk) + (let ((interrupt-thread #f) + (target-thread (current-thread)) + (clockid (pthread-getcpuclockid (pthread-self)))) + (define (start-preemption!) + (let ((period-nsecs (inexact->exact (round (/ 1e9 hz))))) + (set! interrupt-thread + (call-with-new-thread + (lambda () + (let lp () + (clock-nanosleep clockid period-nsecs) + (system-async-mark interrupt target-thread) + (lp))))))) + + (define (stop-preemption!) + (cancel-thread interrupt-thread)) + + (dynamic-wind start-preemption! thunk stop-preemption!))) + +(define (with-interrupts hz interrupt thunk) + "Run @var{sched} until there are no more fibers ready to run, no +file descriptors being waited on, and no more timers pending to run. +Return zero values." + (cond + ((zero? hz) (thunk)) + ((provided? 'threads) + (with-interrupts/thread-cputime hz interrupt thunk)) + (else + (with-interrupts/sigprof hz interrupt thunk)))) |
