Last active
December 25, 2019 14:04
-
-
Save death/2709ae6a9124968a86070bbb760b1629 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defpackage #:snippets/pipe-streams | |
(:import-from #:sb-sys #:make-fd-stream) | |
(:import-from #:sb-posix #:pipe) | |
(:import-from #:sb-thread #:make-thread) | |
(:import-from #:log4cl) | |
(:use #:cl) | |
(:export | |
#:make-pipe-streams)) | |
(in-package #:snippets/pipe-streams) | |
;; Wow, the following function demonstrates use of the run-time | |
;; property of keyword arguments! | |
(defun make-pipe-streams (&key (element-type '(unsigned-byte 8))) | |
"Return two streams, one for reading and the other for writing. | |
When elements are written to the write stream, they will be able to be | |
read by the read stream. When the write stream is closed, the read | |
stream will reach the end-of-file." | |
(multiple-value-bind (read write) (pipe) | |
(flet ((stream-of-type (type fd) | |
(make-fd-stream fd | |
:element-type element-type | |
:buffering :none | |
:auto-close t | |
type t))) | |
(values (stream-of-type :input read) | |
(stream-of-type :output write))))) | |
(defun character-stream-p (stream) | |
"Return true if stream is a character stream, and false otherwise." | |
(and (streamp stream) | |
(subtypep (stream-element-type stream) 'character))) | |
(defun write-octet (octet stream) | |
"Write octet to the stream, which may be a character stream or a | |
byte stream with byte size of 8." | |
(if (character-stream-p stream) | |
(format stream "~D " octet) | |
(write-byte octet stream))) | |
(defun read-octet (stream &optional (eof-error-p t) eof-value) | |
"Read an octet from the stream, which may be a character stream or a | |
byte stream with byte size of 8." | |
(if (character-stream-p stream) | |
(read stream eof-error-p eof-value) | |
(read-byte stream eof-error-p eof-value))) | |
(defun producer (stream) | |
"Writes a 100 random octets to the stream, and then closes the | |
stream." | |
(with-open-stream (stream stream) | |
(loop repeat 100 | |
do (write-octet (random 256) stream)))) | |
(defun consumer (stream) | |
"Reads octets and prints them to the debug log." | |
(loop for octet = (read-octet stream nil nil) | |
while octet | |
do (log:debug "octet ~D" octet))) | |
(defmacro in-parallel (&rest forms) | |
"Run all forms in parallel." | |
(cond ((null forms) | |
`(progn)) | |
((null (rest forms)) | |
(car forms)) | |
(t | |
(destructuring-bind (form &rest rest) forms | |
`(progn | |
(make-thread (lambda () ,form)) | |
(in-parallel ,@rest)))))) | |
(defun test (&key octets) | |
"Demonstrate use of pipe streams." | |
(multiple-value-bind (read write) | |
(make-pipe-streams :element-type | |
(if octets | |
'(unsigned-byte 8) | |
'character)) | |
(in-parallel | |
(producer write) | |
(consumer read)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment