diff options
| author | Andy Wingo <wingo@pobox.com> | 2016-10-03 17:48:02 +0200 |
|---|---|---|
| committer | Andy Wingo <wingo@pobox.com> | 2016-10-03 17:53:32 +0200 |
| commit | f7fa72e4f99a52316abaaaf33414cb15b3f99ef2 (patch) | |
| tree | e28ab2ff752d9e09d9c9bf0a3c71f90624d72e5c /fibers | |
| parent | Sleep refactor (diff) | |
| download | guile-fibers-f7fa72e4f99a52316abaaaf33414cb15b3f99ef2.tar.gz | |
Timeout CML operations
* fibers/timers.scm: New file.
* fibers/internal.scm (add-timer!): Take expiry as absolute time.
* fibers.scm (fibers): Re-export sleep from (fibers timers).
* Makefile.am: Adapt.
Diffstat (limited to 'fibers')
| -rw-r--r-- | fibers/internal.scm | 12 | ||||
| -rw-r--r-- | fibers/timers.scm | 51 |
2 files changed, 56 insertions, 7 deletions
diff --git a/fibers/internal.scm b/fibers/internal.scm index f03ebb1..ce83d58 100644 --- a/fibers/internal.scm +++ b/fibers/internal.scm @@ -434,10 +434,8 @@ from a finalizer thread." (add-fdes-finalizer! fd (lambda (fd) (finalize-fd sched fd))) (epoll-add! (scheduler-epfd sched) fd (logior events EPOLLONESHOT)))))) -(define (add-timer! sched thunk seconds) - (let ((waketime (+ (get-internal-real-time) - (inexact->exact - (round (* seconds internal-time-units-per-second)))))) - (set-scheduler-timers! - sched - (psq-set (scheduler-timers sched) (cons waketime thunk) waketime)))) +(define (add-timer! sched thunk expiry) + (set-scheduler-timers! sched + (psq-set (scheduler-timers sched) + (cons expiry thunk) + expiry))) diff --git a/fibers/timers.scm b/fibers/timers.scm new file mode 100644 index 0000000..14c9787 --- /dev/null +++ b/fibers/timers.scm @@ -0,0 +1,51 @@ +;; 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 timers) + #:use-module (fibers internal) + #:use-module (fibers operations) + #:use-module (ice-9 atomic) + #:use-module (ice-9 match) + #:export (wait-operation + timer-operation) + #:replace (sleep)) + +(define (timer-operation expiry) + (make-base-operation #f + (lambda () + (and (< expiry (get-internal-real-time)) + values)) + (lambda (flag fiber wrap-fn) + (define (maybe-resume-fiber) + (match (atomic-box-compare-and-swap! flag 'W 'S) + ('W (resume-fiber fiber (or wrap-fn values))) + ('C (maybe-resume-fiber)) + ('S (values)))) + (add-timer! (fiber-scheduler fiber) + maybe-resume-fiber + expiry)))) + +(define (wait-operation seconds) + (timer-operation + (+ (get-internal-real-time) + (inexact->exact + (round (* seconds internal-time-units-per-second)))))) + +(define (sleep seconds) + (perform-operation (wait-operation seconds))) |
