2 May 15:02
PipeStreams (aka. generators)
From: Michael FIG <michael@...>
Subject: PipeStreams (aka. generators)
Newsgroups: gmane.comp.lang.smalltalk.fonc
Date: 2008-05-02 13:04:48 GMT
Subject: PipeStreams (aka. generators)
Newsgroups: gmane.comp.lang.smalltalk.fonc
Date: 2008-05-02 13:04:48 GMT
Hi all, I created an interesting derivative of function/objects/Stream.st called PipeStream.st. It uses the POSIX <ucontext.h> functions to implement fully deterministic coroutine-based streams whose producer is a BlockClosure. Any attempt to read the stream transfers control to the producer coroutine (running on the same OS thread) where it attempts to provide enough data to fill the request. From the producer's point of view, they just run to completion. It's like other languages' "generators" feature, but without having to do an explicit "yield". The PipeStream machinery just detects when you write enough objects back to it (or write a few then exit), then resumes control in the reader. I'm quite happy with it, and will be using it to separate my C preprocessor into multiple pipelined parsing stages so that each stage is independent and easier to debug. The only downside I can see is that <ucontext.h> is not all that common. I don't know if something like it exists for Windows (actually, NT Fibers could be abused to provide the same thing, I think) or MacOS. GNU Pth provides similar functionality, so it may be possible to learn how to reimplement the context-switching features on other platforms. You also have to assign some stack space to the BlockClosure, and eventually, it will be best to allocate that space from the caller's stack so that it has the correct permissions.(Continue reading)
RSS Feed