tango.io.device.ThreadPipe

License:

BSD style: see license.txt

Version:

Jun 2008: Initial release

Author:

schveiguy
class ThreadPipe : Conduit
Conduit to support a data stream between 2 threads. One creates a ThreadPipe, then uses the OutputStream and the InputStream from it to communicate. All traffic is automatically synchronized, so one just uses the streams like they were normal device streams.
It works by maintaining a circular buffer, where data is written to, and read from, in a FIFO fashion.
1
2
3
4
5
6
7
8
9
10
11
auto tc = new ThreadPipe;
void outFunc()
{
  Stdout.copy(tc.input);
}

auto t = new Thread(&outFunc);
t.start();
tc.write("hello, thread!");
tc.close();
t.join();
this(size_t bufferSize = (1024*16))
Create a new ThreadPipe with the given buffer size.

Parameters:

bufferSizeThe size to allocate the buffer.
size_t bufferSize() [override, const]
Implements IConduit.bufferSize.
Returns the appropriate buffer size that should be used to buffer the ThreadPipe. Note that this is simply the buffer size passed in, and since all the ThreadPipe data is in memory, buffering doesn't make much sense.
string toString() [override]
Implements IConduit.toString
Returns "<thread conduit>"
bool isAlive() [override, const]
Returns true if there is data left to be read, and the write end isn't closed.
size_t remaining()
Return the number of bytes remaining to be read in the circular buffer.
size_t writable()
Return the number of bytes that can be written to the circular buffer.
void stop()
Close the write end of the conduit. Writing to the conduit after it is closed will return Eof.
The read end is not closed until the buffer is empty.
void detach() [override]
This does nothing because we have no clue whether the members have been collected, and detach is run in the destructor. To stop communications, use stop().

TODO:

move stop() functionality to detach when it becomes possible to have fully-owned members
size_t read(void[] dst) [override]
Implements InputStream.read.
Read from the conduit into a target array. The provided dst will be populated with content from the stream.

Returns the number of bytes read, which may be less than requested in dst. Eof is returned whenever an end-of-flow condition arises.
ThreadPipe clear()
Implements InputStream.clear().
Clear any buffered content.
size_t write(const(void)[] src) [override]
Implements OutputStream.write.
Write to stream from a source array. The provided src content will be written to the stream.

Returns the number of bytes written from src, which may be less than the quantity provided. Eof is returned when an end-of-flow condition arises.