[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Guile-commits] 21/23: eports tweak
From: |
Andy Wingo |
Subject: |
[Guile-commits] 21/23: eports tweak |
Date: |
Thu, 24 Mar 2016 14:26:04 +0000 |
wingo pushed a commit to branch wip-ethreads
in repository guile.
commit b96b596dad7fae6f99b2a7926a7ba9f97f22bfc0
Author: Andy Wingo <address@hidden>
Date: Fri Mar 30 19:35:42 2012 +0200
eports tweak
* module/ice-9/eports.scm (flush-buffer, fill-input, ensure-writable):
Instead of making flush-buffer reset cur and end to 0 when the buffer
is empty, make that be the job of the less-frequently-called
fill-input and ensure-writable.
---
module/ice-9/eports.scm | 54 ++++++++++++++++++++++------------------------
1 files changed, 26 insertions(+), 28 deletions(-)
diff --git a/module/ice-9/eports.scm b/module/ice-9/eports.scm
index f098e84..5b8231d 100644
--- a/module/ice-9/eports.scm
+++ b/module/ice-9/eports.scm
@@ -111,21 +111,13 @@
(define (make-fresh-buf n)
(make-buf (make-bytevector n 0) 0 0))
-;; Mark N bytes as having been read or written. This advances CUR by N,
-;; except in the case that CUR would be equal to END, in which case both
-;; are reset to 0.
+;; Mark N bytes as having been read or written. This advances CUR by N.
;;
(define (flush-buffer buf n)
- (let ((new-cur (+ (buf-cur buf) n))
- (end (buf-end buf)))
- (cond
- ((< new-cur end)
- (set-buf-cur! buf new-cur))
- ((= new-cur end)
- (set-buf-cur! buf 0)
- (set-buf-end! buf 0))
- (else
- (error "flushing too many bytes" buf n)))))
+ (let ((cur (buf-cur buf)))
+ (unless (<= n (- (buf-end buf) cur))
+ (error "flushing too many bytes" buf n))
+ (set-buf-cur! buf (+ cur n))))
;; Create an NIO port that wraps FD. The strange default sizes assume
;; that the memory is allocated inline to the bytevector, and thus has a
@@ -196,19 +188,19 @@
(define (fill-input eport)
(let* ((buf (eport-readbuf eport))
(bv (buf-bv buf))
- (cur (buf-cur buf))
- (end (buf-end buf))
(len (bytevector-length bv)))
- (if (zero? (- len end))
- (error "fill-input should only be called when the readbuf is empty"))
- (let ((rv (nio-read (eport-fd eport) bv end (- len end))))
+ (unless (= (buf-cur buf) (buf-end buf))
+ (error "fill-input should only be called when the readbuf is empty"))
+ (set-buf-cur! buf 0)
+ (set-buf-end! buf 0) ; in case nio-read throws an error
+ (let ((rv (nio-read (eport-fd eport) bv 0 len)))
(if (< rv 0)
(begin
(wait-for-readable eport)
(fill-input eport))
- (let ((new-end (+ end rv)))
- (set-buf-end! buf new-end)
- (- new-end cur))))))
+ (begin
+ (set-buf-end! buf rv)
+ rv)))))
;; Write all buffered output: those bytes between CUR and END. Advances
;; CUR to be equal to END.
@@ -240,7 +232,12 @@
(end (buf-end buf))
(bv (buf-bv buf))
(size (bytevector-length bv)))
- (when (= end size)
+ (cond
+ ((zero? end))
+ ((= cur end)
+ (set-buf-cur! buf 0)
+ (set-buf-end! buf 0))
+ ((= end size)
(if (> (* cur 2) size)
;; The buffer is less than half full; shuffle the data to
;; make space.
@@ -250,12 +247,13 @@
(set-buf-end! buf (- end cur)))
;; The buffer is more than half full; write some data and
;; try again.
- (let ((written (nio-write (eport-fd eport)
- bv cur (- end cur))))
- (flush-buffer buf written)
- (when (< written (- end cur))
- (wait-for-writable eport)
- (lp)))))))))
+ (begin
+ (let ((written (nio-write (eport-fd eport)
+ bv cur (- end cur))))
+ (flush-buffer buf written)
+ (when (< written (- end cur))
+ (wait-for-writable eport)
+ (lp)))))))))))
;; Peek at the next octet from EPORT, blocking if necessary.
;;
- [Guile-commits] 11/23: socket: TCP_CORK, TCP_NODELAY, (continued)
- [Guile-commits] 11/23: socket: TCP_CORK, TCP_NODELAY, Andy Wingo, 2016/03/24
- [Guile-commits] 23/23: virtualize read/write/close operations in <eport>, Andy Wingo, 2016/03/24
- [Guile-commits] 15/23: (web server ethreads): more use of latin1 accessors, Andy Wingo, 2016/03/24
- [Guile-commits] 01/23: add (ice-9 nio), Andy Wingo, 2016/03/24
- [Guile-commits] 20/23: eports: nonblocking connect-eport, Andy Wingo, 2016/03/24
- [Guile-commits] 14/23: refactoring to (web server ethreads) read-http-line, Andy Wingo, 2016/03/24
- [Guile-commits] 02/23: add (ice-9 eports), Andy Wingo, 2016/03/24
- [Guile-commits] 17/23: getsockopt: allow raw file descriptors, Andy Wingo, 2016/03/24
- [Guile-commits] 16/23: eports: add put-utf8-char, put-utf8-string, Andy Wingo, 2016/03/24
- [Guile-commits] 03/23: add (ice-9 epoll), Andy Wingo, 2016/03/24
- [Guile-commits] 21/23: eports tweak,
Andy Wingo <=
- [Guile-commits] 07/23: add (web server ethreads), Andy Wingo, 2016/03/24
- [Guile-commits] 22/23: add examples/ethreads/memcached-{client, server}, Andy Wingo, 2016/03/24