summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndy Wingo <wingo@pobox.com>2016-10-11 14:56:06 +0200
committerAndy Wingo <wingo@pobox.com>2016-10-11 14:56:06 +0200
commited4e85000f4c0282a22a0a181056ec37cb89eecc (patch)
tree7d6ee4ac0267703adeb46945ac1dcb8e0c77effe
parentInternal channels refactor (diff)
downloadguile-fibers-ed4e85000f4c0282a22a0a181056ec37cb89eecc.tar.gz
Factor out deque implementation
* fibers/deque.scm: Factor out to helper module. * Makefile.am: Add deque.scm. * fibers/channels.scm: Use (fibers deque).
-rw-r--r--Makefile.am1
-rw-r--r--fibers/channels.scm49
-rw-r--r--fibers/deque.scm76
3 files changed, 78 insertions, 48 deletions
diff --git a/Makefile.am b/Makefile.am
index 6403f44..8fafa15 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -27,6 +27,7 @@ info_TEXINFOS=fibers.texi
SOURCES = \
fibers.scm \
fibers/channels.scm \
+ fibers/deque.scm \
fibers/epoll.scm \
fibers/internal.scm \
fibers/nameset.scm \
diff --git a/fibers/channels.scm b/fibers/channels.scm
index 8a54f61..c37cac9 100644
--- a/fibers/channels.scm
+++ b/fibers/channels.scm
@@ -31,6 +31,7 @@
#:use-module (srfi srfi-9 gnu)
#:use-module (ice-9 atomic)
#:use-module (ice-9 match)
+ #:use-module (fibers deque)
#:use-module (fibers internal)
#:use-module (fibers operations)
#:export (make-channel
@@ -39,54 +40,6 @@
put-message
get-message))
-
-;; A functional double-ended queue ("deque") has a head and a tail,
-;; which are both lists. The head is in FIFO order and the tail is in
-;; LIFO order.
-(define-inlinable (make-deque head tail)
- (cons head tail))
-
-(define (make-empty-deque)
- (make-deque '() '()))
-
-(define (enqueue dq item)
- (match dq
- ((head . tail)
- (make-deque head (cons item tail)))))
-
-(define (undequeue dq item)
- (match dq
- ((head . tail)
- (make-deque (cons item head) tail))))
-
-;; -> new deque, val | #f, #f
-(define (dequeue dq)
- (match dq
- ((() . ()) (values #f #f))
- ((() . tail)
- (dequeue (make-deque (reverse tail) '())))
- (((item . head) . tail)
- (values (make-deque head tail) item))))
-
-(define (dequeue-match dq pred)
- (match dq
- ((() . ()) (values #f #f))
- ((() . tail)
- (dequeue (make-deque (reverse tail) '())))
- (((item . head) . tail)
- (if (pred item)
- (values (make-deque head tail) item)
- (call-with-values (dequeue-match (make-deque head tail) pred)
- (lambda (dq item*)
- (values (undequeue dq item) item*)))))))
-
-(define (enqueue! qbox item)
- (let spin ((q (atomic-box-ref qbox)))
- (let* ((q* (enqueue q item))
- (q** (atomic-box-compare-and-swap! qbox q q*)))
- (unless (eq? q q**)
- (spin q**)))))
-
(define-record-type <channel>
(%make-channel getq putq)
channel?
diff --git a/fibers/deque.scm b/fibers/deque.scm
new file mode 100644
index 0000000..309072b
--- /dev/null
+++ b/fibers/deque.scm
@@ -0,0 +1,76 @@
+;; Double-ended queue
+
+;;;; Copyright (C) 2016 Andy Wingo <wingo@pobox.com>
+;;;;
+;;;; 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 deque)
+ #:use-module (srfi srfi-9)
+ #:use-module (ice-9 atomic)
+ #:use-module (ice-9 match)
+ #:export (make-deque
+ make-empty-deque
+ enqueue
+ dequeue
+ dequeue-match
+ undequeue
+ enqueue!))
+
+;; A functional double-ended queue ("deque") has a head and a tail,
+;; which are both lists. The head is in FIFO order and the tail is in
+;; LIFO order.
+(define-inlinable (make-deque head tail)
+ (cons head tail))
+
+(define (make-empty-deque)
+ (make-deque '() '()))
+
+(define (enqueue dq item)
+ (match dq
+ ((head . tail)
+ (make-deque head (cons item tail)))))
+
+;; -> new deque, val | #f, #f
+(define (dequeue dq)
+ (match dq
+ ((() . ()) (values #f #f))
+ ((() . tail)
+ (dequeue (make-deque (reverse tail) '())))
+ (((item . head) . tail)
+ (values (make-deque head tail) item))))
+
+(define (dequeue-match dq pred)
+ (match dq
+ ((() . ()) (values #f #f))
+ ((() . tail)
+ (dequeue (make-deque (reverse tail) '())))
+ (((item . head) . tail)
+ (if (pred item)
+ (values (make-deque head tail) item)
+ (call-with-values (dequeue-match (make-deque head tail) pred)
+ (lambda (dq item*)
+ (values (undequeue dq item) item*)))))))
+
+(define (undequeue dq item)
+ (match dq
+ ((head . tail)
+ (make-deque (cons item head) tail))))
+
+(define (enqueue! qbox item)
+ (let spin ((q (atomic-box-ref qbox)))
+ (let* ((q* (enqueue q item))
+ (q** (atomic-box-compare-and-swap! qbox q q*)))
+ (unless (eq? q q**)
+ (spin q**)))))