Changeset 4901


Ignore:
Timestamp:
Jul 23, 2006, 2:03:32 AM (18 years ago)
Author:
Gary Byers
Message:

STREAM-IOBLOCK now takes 2 required args and is inlined.

Some bug fixes; STREAM class no longer has slots.

IOBLOCK-READ-CHAR-FUNCTION used by READ-CHAR. %ioblock-read-byte variants;
fix read-byte botch (should vector according to element-type.)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/ccl/level-1/l1-streams.lisp

    r4895 r4901  
    374374  (report-bad-arg (ioblock-stream ioblock) '(and binary-stream output-stream)))
    375375
    376 (defun ioblock-no-character-input (ioblock)
     376(defun ioblock-no-charr-input (ioblock)
    377377  (report-bad-arg (ioblock-stream ioblock) '(and character-stream input-stream)))
    378378
    379 (defun ioblock-no-character-output (ioblock)
     379(defun ioblock-no-char-output (ioblock)
    380380  (report-bad-arg (ioblock-stream ioblock) '(and character-stream output-stream)))
    381381
     
    397397
    398398
    399 (defmacro with-ioblock-lock-grabbed ((lock)
    400                                        &body body)
    401   `(with-lock-grabbed (,lock)
    402     ,@body))
    403 
    404 (defmacro with-ioblock-lock-grabbed-maybe ((lock)
    405                                            &body body)
    406   `(with-lock-grabbed-maybe (,lock)
    407     ,@body))
    408 
    409399;;; ioblock must really be an ioblock or you will crash
    410400;;; Also: the expression "ioblock" is evaluated multiple times.
     
    417407          (error "Stream ~s is private to ~s" (ioblock-stream ioblock) owner)))))
    418408
    419 (defmacro with-ioblock-input-locked ((ioblock) &body body)
    420   (let* ((lock (gensym)))
    421     `(let* ((,lock (locally (declare (optimize (speed 3) (safety 0)))
    422                                   (ioblock-inbuf-lock ,ioblock))))
    423       (if ,lock
    424         (with-ioblock-lock-grabbed ((locally (declare (optimize (speed 3) (safety 0)))
    425                                   (ioblock-inbuf-lock ,ioblock)))
    426           ,@body)
    427         (progn
    428           (check-ioblock-owner ,ioblock)
    429           ,@body)))))
    430 
    431 
    432 (defmacro with-ioblock-output-locked ((ioblock) &body body)
    433   (let* ((lock (gensym)))
    434     `(let* ((,lock (locally (declare (optimize (speed 3) (safety 0)))
    435                                   (ioblock-inbuf-lock ,ioblock))))
    436       (if ,lock
    437         (with-ioblock-lock-grabbed ((locally (declare (optimize (speed 3) (safety 0)))
    438                                   (ioblock-outbuf-lock ,ioblock)))
    439           ,@body)
    440         (progn
    441           (check-ioblock-owner ,ioblock)
    442           ,@body)))))
    443 
    444 (defmacro with-ioblock-output-locked-maybe ((ioblock) &body body)
    445   (let* ((lock (gensym)))
    446     `(let* ((,lock (locally (declare (optimize (speed 3) (safety 0)))
    447                                   (ioblock-inbuf-lock ,ioblock))))
    448       (if ,lock
    449         (with-ioblock-lock-grabbed-maybe ((locally (declare (optimize (speed 3) (safety 0)))
    450                                             (ioblock-outbuf-lock ,ioblock)))
    451           ,@body)
    452         (progn
    453           (check-ioblock-owner ,ioblock)
    454           ,@body)))))
     409
     410
     411
     412
    455413
    456414(defun %ioblock-advance (ioblock read-p)
     
    459417           ioblock
    460418           read-p))
     419
    461420(declaim (inline %ioblock-read-byte))
    462421
    463 ;;; Should only be called with the ioblock locked
     422;;; Should only be called with the ioblock locked, if that's necessary.
     423;;; The whole "bivalent streams" nonsense makes this more complicated
     424;;; than it should be.  (Yes, I understand the rationale for that,
     425;;; but discovering what type of array we have on each call isn't
     426;;; a good approach to the problem.)  That's actually not entirely
     427;;; the fault of bivalent streams, to be honest.
    464428(defun %ioblock-read-byte (ioblock)
    465429  (declare (optimize (speed 3) (safety 0)))
     
    477441              limit (io-buffer-count buf)))
    478442      (setf (io-buffer-idx buf) (the fixnum (1+ idx)))
    479       (aref (the (simple-array (unsigned-byte 8) (*))
    480               (io-buffer-buffer buf)) idx))))
     443      (let* ((vec (io-buffer-buffer buf)))
     444        (if (typep vec 'simple-string)
     445          (aref (the (simple-array (unsigned-byte 8) (*))
     446                  vec) idx)
     447          (uvref vec idx))))))
     448
     449(declaim (inline %private-ioblock-read-byte))
     450(defun %private-ioblock-read-byte (ioblock)
     451  (declare (optimize (speed 3) (safety 0)))
     452  (check-ioblock-owner ioblock)
     453  (if (ioblock-untyi-char ioblock)
     454    (prog1 (%char-code (ioblock-untyi-char ioblock))
     455      (setf (ioblock-untyi-char ioblock) nil))
     456    (let* ((buf (ioblock-inbuf ioblock))
     457           (idx (io-buffer-idx buf))
     458           (limit (io-buffer-count buf)))
     459      (declare (fixnum idx limit))
     460      (when (= idx limit)
     461        (unless (%ioblock-advance ioblock t)
     462          (return-from %private-ioblock-read-byte :eof))
     463        (setq idx (io-buffer-idx buf)
     464              limit (io-buffer-count buf)))
     465      (setf (io-buffer-idx buf) (the fixnum (1+ idx)))
     466      (let* ((vec (io-buffer-buffer buf)))
     467        (if (typep vec 'simple-string)
     468          (aref (the (simple-array (unsigned-byte 8) (*))
     469                  vec) idx)
     470          (uvref vec idx))))))
     471
     472(defun %locked-ioblock-read-byte (ioblock)
     473  (declare (optimize (speed 3) (safety 0)))
     474  (with-ioblock-lock-grabbed ((locally (declare (optimize (speed 3) (safety 0)))
     475                                (ioblock-inbuf-lock ioblock)))
     476  (if (ioblock-untyi-char ioblock)
     477    (prog1 (%char-code (ioblock-untyi-char ioblock))
     478      (setf (ioblock-untyi-char ioblock) nil))
     479    (let* ((buf (ioblock-inbuf ioblock))
     480           (idx (io-buffer-idx buf))
     481           (limit (io-buffer-count buf)))
     482      (declare (fixnum idx limit))
     483      (when (= idx limit)
     484        (unless (%ioblock-advance ioblock t)
     485          (return-from %locked-ioblock-read-byte :eof))
     486        (setq idx (io-buffer-idx buf)
     487              limit (io-buffer-count buf)))
     488      (setf (io-buffer-idx buf) (the fixnum (1+ idx)))
     489      (let* ((vec (io-buffer-buffer buf)))
     490        (if (typep vec 'simple-string)
     491          (aref (the (simple-array (unsigned-byte 8) (*))
     492                  vec) idx)
     493          (uvref vec idx)))))))
     494
     495
    481496
    482497(declaim (inline %ioblock-tyi))
     
    19221937  (with-slots (ioblock) s
    19231938    (not (null ioblock))))
    1924  
    1925 (defun stream-ioblock (stream &optional (error-if-nil t))
     1939
     1940(declaim (inline stream-ioblock))
     1941
     1942(defun stream-ioblock (stream error-if-nil)
    19261943  (with-slots (ioblock) stream
    19271944    (or ioblock
     
    20432060
    20442061
    2045 (defmacro with-stream-ioblock-input ((ioblock stream &key
    2046                                              speedy)
    2047                                   &body body)
    2048   `(let ((,ioblock (stream-ioblock ,stream)))
    2049      ,@(when speedy `((declare (optimize (speed 3) (safety 0)))))
    2050      (with-ioblock-input-locked (,ioblock) ,@body)))
    2051 
    2052 (defmacro with-stream-ioblock-output ((ioblock stream &key
    2053                                              speedy)
    2054                                   &body body)
    2055   `(let ((,ioblock (stream-ioblock ,stream)))
    2056      ,@(when speedy `((declare (optimize (speed 3) (safety 0)))))
    2057      (with-ioblock-output-locked (,ioblock) ,@body)))
    2058 
    2059 (defmacro with-stream-ioblock-output-maybe ((ioblock stream &key
    2060                                                      speedy)
    2061                                             &body body)
    2062   `(let ((,ioblock (stream-ioblock ,stream)))
    2063     ,@(when speedy `((declare (optimize (speed 3) (safety 0)))))
    2064     (with-ioblock-output-locked-maybe (,ioblock) ,@body)))
    20652062
    20662063(defmethod stream-read-char ((stream buffered-character-input-stream-mixin))
    2067   (with-stream-ioblock-input (ioblock stream :speedy t)
    2068     (%ioblock-tyi ioblock)))
     2064  (let* ((ioblock (stream-ioblock stream t)))
     2065    (funcall (ioblock-read-char-function ioblock) ioblock)))
    20692066
    20702067(defmethod stream-read-char-no-hang ((stream buffered-character-input-stream-mixin))
     
    23232320
    23242321 
    2325 ;;; Use this when it's possible that the fd might be in
    2326 ;;; a non-blocking state.  Body must return a negative of
    2327 ;;; the os error number on failure.
    2328 ;;; The use of READ-FROM-STRING below is certainly ugly, but macros
    2329 ;;; that expand into reader-macros don't generally trigger the reader-macro's
    2330 ;;; side-effects.  (Besides, the reader-macro might return a different
    2331 ;;; value when the macro function is expanded than it did when the macro
    2332 ;;; function was defined; this can happen during cross-compilation.)
    2333 (defmacro with-eagain (fd direction &body body)
    2334   (let* ((res (gensym))
    2335          (eagain (symbol-value (read-from-string "#$EAGAIN"))))
    2336    `(loop
    2337       (let ((,res (progn ,@body)))
    2338         (if (eql ,res (- ,eagain))
    2339           (,(ecase direction
    2340              (:input 'process-input-wait)
    2341              (:output 'process-output-wait))
    2342            ,fd)
    2343           (return ,res))))))
     2322
    23442323
    23452324
Note: See TracChangeset for help on using the changeset viewer.