vibe.stream.taskpipe 0/100(0%) line coverage

      
10
20
30
40
50
60
70
80
90
100
110
120
130
140
150
160
170
180
190
200
210
220
230
240
250
260
270
280
290
300
310
320
330
340
350
360
370
380
390
400
410
420
430
440
450
460
470
480
490
500
510
520
530
540
550
560
570
580
590
600
610
620
630
640
650
660
670
680
690
700
710
720
730
740
750
760
770
780
790
800
810
820
830
840
850
860
870
880
890
900
910
920
930
940
950
960
970
980
990
1000
1010
1020
1030
1040
1050
1060
1070
1080
1090
1100
1110
1120
1130
1140
1150
1160
1170
1180
1190
1200
1210
1220
1230
1240
1250
1260
1270
1280
1290
1300
1310
1320
1330
1340
1350
1360
1370
1380
1390
1400
1410
1420
1430
1440
1450
1460
1470
1480
1490
1500
1510
1520
1530
1540
1550
1560
1570
1580
1590
1600
1610
1620
1630
1640
1650
1660
1670
1680
1690
1700
1710
1720
1730
1740
1750
1760
1770
1780
1790
1800
1810
1820
1830
1840
1850
1860
1870
1880
1890
1900
1910
1920
1930
1940
1950
1960
1970
1980
1990
2000
2010
2020
2030
2040
2050
2060
2070
2080
2090
2100
2110
2120
2130
2140
2150
2160
2170
2180
2190
2200
2210
2220
2230
2240
2250
2260
2270
2280
2290
2300
2310
2320
2330
2340
2350
2360
2370
2380
2390
2400
2410
2420
2430
2440
2450
2460
/** Stream interface for passing data between different tasks. Copyright: © 2013 RejectedSoftware e.K. License: Subject to the terms of the MIT license, as written in the included LICENSE.txt file. Authors: Sönke Ludwig */ module vibe.stream.taskpipe; public import vibe.core.stream; import core.sync.mutex; import core.time; import std.algorithm : min; import std.exception; import vibe.core.core; import vibe.core.sync; import vibe.utils.array; /** Implements a unidirectional data pipe between two tasks. */ final class TaskPipe : ConnectionStream { private { TaskPipeImpl m_pipe; } /** Constructs a new pipe ready for use. */ this(bool grow_when_full = false) { m_pipe = new TaskPipeImpl(grow_when_full); } /// Size of the (fixed) FIFO buffer used to transfer data between tasks @property size_t bufferSize() const { return m_pipe.bufferSize; } /// ditto @property void bufferSize(size_t nbytes) { m_pipe.bufferSize = nbytes; } @property bool empty() { return leastSize() == 0; } @property ulong leastSize() { m_pipe.waitForData(); return m_pipe.fill; } @property bool dataAvailableForRead() { return m_pipe.fill > 0; } @property bool connected() const { return m_pipe.open; } void close() { m_pipe.close(); } bool waitForData(Duration timeout) { if (dataAvailableForRead) return true; m_pipe.waitForData(timeout); return dataAvailableForRead; } const(ubyte)[] peek() { return m_pipe.peek; } size_t read(scope ubyte[] dst, IOMode mode) { return m_pipe.read(dst, mode); } alias read = ConnectionStream.read; size_t write(in ubyte[] bytes, IOMode mode) { return m_pipe.write(bytes, mode); } alias write = ConnectionStream.write; void flush() {} void finalize() { m_pipe.close(); } } /** Underyling pipe implementation for TaskPipe with no Stream interface. */ private final class TaskPipeImpl { @safe: private { Mutex m_mutex; InterruptibleTaskCondition m_condition; vibe.utils.array.FixedRingBuffer!ubyte m_buffer; bool m_closed = false; bool m_growWhenFull; } /** Constructs a new pipe ready for use. */ this(bool grow_when_full = false) { m_mutex = new Mutex; () @trusted { m_condition = new InterruptibleTaskCondition(m_mutex); } (); m_buffer.capacity = 2048; m_growWhenFull = grow_when_full; } /// Size of the (fixed) buffer used to transfer data between tasks @property size_t bufferSize() const { return m_buffer.capacity; } /// ditto @property void bufferSize(size_t nbytes) { m_buffer.capacity = nbytes; } /// Number of bytes currently in the transfer buffer @property size_t fill() const { synchronized (m_mutex) { return m_buffer.length; } } @property bool open() const { return !m_closed; } /** Closes the pipe. */ void close() { synchronized (m_mutex) m_closed = true; () @trusted { m_condition.notifyAll(); } (); } /** Blocks until at least one byte of data has been written to the pipe. */ void waitForData(Duration timeout = Duration.max) { import std.datetime : Clock, SysTime, UTC; bool have_timeout = timeout > 0.seconds && timeout != Duration.max; SysTime now = Clock.currTime(UTC()); SysTime timeout_target; if (have_timeout) timeout_target = now + timeout; synchronized (m_mutex) { while (m_buffer.empty && !m_closed && (!have_timeout || now < timeout_target)) { if (have_timeout) () @trusted { m_condition.wait(timeout_target - now); } (); else () @trusted { m_condition.wait(); } (); now = Clock.currTime(UTC()); } } } /** Writes the given byte array to the pipe. */ size_t write(const(ubyte)[] data, IOMode mode) { size_t ret = 0; enforce(!m_closed, "Writing to closed task pipe."); while (data.length > 0){ bool need_signal; synchronized (m_mutex) { if (m_growWhenFull && m_buffer.full) { size_t new_sz = m_buffer.capacity; while (new_sz - m_buffer.capacity < data.length) new_sz += 2; m_buffer.capacity = new_sz; } else while (m_buffer.full) { if (mode == IOMode.immediate || mode == IOMode.once && ret > 0) return ret; () @trusted { m_condition.wait(); } (); } need_signal = m_buffer.empty; auto len = min(m_buffer.freeSpace, data.length); m_buffer.put(data[0 .. len]); data = data[len .. $]; ret += len; } if (need_signal) () @trusted { m_condition.notifyAll(); } (); } if (!m_growWhenFull) vibe.core.core.yield(); return ret; } /** Returns a temporary view of the beginning of the transfer buffer. Note that a call to read invalidates this array slice. Blocks in case of a filled up transfer buffer. */ const(ubyte[]) peek() { synchronized (m_mutex) { return m_buffer.peek(); } } /** Reads data into the supplied buffer. Blocks until a sufficient amount of data has been written to the pipe. */ size_t read(scope ubyte[] dst, IOMode mode) { size_t ret = 0; while (dst.length > 0) { bool need_signal; size_t len; synchronized (m_mutex) { while (m_buffer.empty && !m_closed) { if (mode == IOMode.immediate || mode == IOMode.once && ret > 0) return ret; () @trusted { m_condition.wait(); } (); } need_signal = m_buffer.full; enforce(!m_buffer.empty, "Reading past end of closed pipe."); len = min(dst.length, m_buffer.length); m_buffer.read(dst[0 .. len]); ret += len; } if (need_signal) () @trusted { m_condition.notifyAll(); } (); dst = dst[len .. $]; } vibe.core.core.yield(); return ret; } } unittest { // issue #1501 - deadlock in TaskPipe import std.datetime : Clock, UTC; import core.time : msecs; // test read after write and write after read foreach (i; 0 .. 2) { auto p = new TaskPipe; p.bufferSize = 2048; Task a, b; a = runTask({ ubyte[2100] buf; if (i == 0) p.read(buf, IOMode.all); else p.write(buf, IOMode.all); }); b = runTask({ ubyte[2100] buf; if (i == 0) p.write(buf, IOMode.all); else p.read(buf, IOMode.all); }); auto joiner = runTask({ auto starttime = Clock.currTime(UTC()); while (a.running || b.running) { if (Clock.currTime(UTC()) - starttime > 500.msecs) assert(false, "TaskPipe is dead locked."); yield(); } }); joiner.join(); } } unittest { // issue # auto t = runTask({ auto tp = new TaskPipeImpl; tp.waitForData(10.msecs); exitEventLoop(); }); runTask({ sleep(500.msecs); assert(!t.running, "TaskPipeImpl.waitForData didn't timeout."); }); runEventLoop(); }