vibe.core.task 4/92(4%) 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
4654
4754
480
490
500
510
520
530
540
550
560
570
580
590
600
610
620
630
640
650
660
670
680
696
700
710
720
730
740
750
760
770
780
790
800
810
820
830
840
850
860
8724
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
2470
2480
2490
2500
2510
2520
2530
2540
2550
2560
2570
2580
2590
2600
2610
2620
2630
2640
2650
2660
2670
2680
2690
2700
2710
2720
2730
2740
2750
2760
2770
2780
2790
2800
2810
2820
2830
2840
2850
2860
2870
2880
2890
2900
/** Contains interfaces and enums for evented I/O drivers. Copyright: © 2012-2014 RejectedSoftware e.K. Authors: Sönke Ludwig License: Subject to the terms of the MIT license, as written in the included LICENSE.txt file. */ module vibe.core.task; import vibe.core.sync; import vibe.utils.array; import core.thread; import std.exception; import std.traits; import std.typecons; import std.variant; /** Represents a single task as started using vibe.core.runTask. Note that the Task type is considered weakly isolated and thus can be passed between threads using vibe.core.concurrency.send or by passing it as a parameter to vibe.core.core.runWorkerTask. */ struct Task { private { shared(TaskFiber) m_fiber; size_t m_taskCounter; import std.concurrency : ThreadInfo, Tid; static ThreadInfo s_tidInfo; } private this(TaskFiber fiber, size_t task_counter) @safe nothrow { () @trusted { m_fiber = cast(shared)fiber; } (); m_taskCounter = task_counter; } this(in Task other) nothrow { m_fiber = cast(shared(TaskFiber))other.m_fiber; m_taskCounter = other.m_taskCounter; } /** Returns the Task instance belonging to the calling task. */ static Task getThis() nothrow @safe { auto fiber = () @trusted { return Fiber.getThis(); } (); if (!fiber) return Task.init; auto tfiber = cast(TaskFiber)fiber; if (!tfiber) return Task.init; if (!tfiber.m_running) return Task.init; return () @trusted { return Task(tfiber, tfiber.m_taskCounter); } (); } nothrow { @property inout(TaskFiber) fiber() inout @trusted { return cast(inout(TaskFiber))m_fiber; } @property size_t taskCounter() const @safe { return m_taskCounter; } @property inout(Thread) thread() inout @safe { if (m_fiber) return this.fiber.thread; return null; } /** Determines if the task is still running. */ @property bool running() const @trusted { assert(m_fiber !is null, "Invalid task handle"); try if (this.fiber.state == Fiber.State.TERM) return false; catch (Throwable) {} return this.fiber.m_running && this.fiber.m_taskCounter == m_taskCounter; } // FIXME: this is not thread safe! @property ref ThreadInfo tidInfo() { return m_fiber ? fiber.tidInfo : s_tidInfo; } @property ref const(ThreadInfo) tidInfo() const { return m_fiber ? fiber.tidInfo : s_tidInfo; } @property Tid tid() { return tidInfo.ident; } @property const(Tid) tid() const { return tidInfo.ident; } } /// Reserved for internal use! @property inout(MessageQueue) messageQueue() inout { assert(running, "Task is not running"); return fiber.messageQueue; } T opCast(T)() const nothrow if (is(T == bool)) { return m_fiber !is null; } void join() @safe { if (running) fiber.join(); } void interrupt() { if (running) fiber.interrupt(); } void terminate() { if (running) fiber.terminate(); } string toString() const @safe { import std.string; return format("%s:%s", () @trusted { return cast(void*)m_fiber; } (), m_taskCounter); } bool opEquals(in ref Task other) const nothrow @safe { return m_fiber is other.m_fiber && m_taskCounter == other.m_taskCounter; } bool opEquals(in Task other) const nothrow @safe { return m_fiber is other.m_fiber && m_taskCounter == other.m_taskCounter; } } /** The base class for a task aka Fiber. This class represents a single task that is executed concurrently with other tasks. Each task is owned by a single thread. */ class TaskFiber : Fiber { private { import std.concurrency : ThreadInfo; Thread m_thread; ThreadInfo m_tidInfo; MessageQueue m_messageQueue; } protected { shared size_t m_taskCounter; shared bool m_running; } protected this(void delegate() fun, size_t stack_size) nothrow { super(fun, stack_size); m_thread = Thread.getThis(); scope (failure) assert(false); m_messageQueue = new MessageQueue; } /** Returns the thread that owns this task. */ @property inout(Thread) thread() inout @safe nothrow { return m_thread; } /** Returns the handle of the current Task running on this fiber. */ @property Task task() @safe nothrow { return Task(this, m_taskCounter); } /// Reserved for internal use! @property inout(MessageQueue) messageQueue() inout { return m_messageQueue; } @property ref inout(ThreadInfo) tidInfo() inout nothrow { return m_tidInfo; } /** Blocks until the task has ended. */ abstract void join() @safe; /** Throws an InterruptExeption within the task as soon as it calls a blocking function. */ abstract void interrupt(); /** Terminates the task without notice as soon as it calls a blocking function. */ abstract void terminate(); void bumpTaskCounter() @safe nothrow { import core.atomic : atomicOp; () @trusted { atomicOp!"+="(this.m_taskCounter, 1); } (); } } /** Exception that is thrown by Task.interrupt. */ class InterruptException : Exception { this() { super("Task interrupted."); } } class MessageQueue { private { InterruptibleTaskMutex m_mutex; InterruptibleTaskCondition m_condition; FixedRingBuffer!Variant m_queue; FixedRingBuffer!Variant m_priorityQueue; size_t m_maxMailboxSize = 0; bool function(Task) m_onCrowding; } this() { m_mutex = new InterruptibleTaskMutex; m_condition = new InterruptibleTaskCondition(m_mutex); m_queue.capacity = 32; m_priorityQueue.capacity = 8; } @property bool full() const { return m_maxMailboxSize > 0 && m_queue.length + m_priorityQueue.length >= m_maxMailboxSize; } void clear() { m_mutex.performLocked!({ m_queue.clear(); m_priorityQueue.clear(); }); m_condition.notifyAll(); } void setMaxSize(size_t count, bool function(Task tid) action) { m_maxMailboxSize = count; m_onCrowding = action; } void send(Variant msg) { import vibe.core.log; m_mutex.performLocked!({ if( this.full ){ if( !m_onCrowding ){ while(this.full) m_condition.wait(); } else if( !m_onCrowding(Task.getThis()) ){ return; } } assert(!this.full); if( m_queue.full ) m_queue.capacity = (m_queue.capacity * 3) / 2; m_queue.put(msg); }); m_condition.notify(); } void prioritySend(Variant msg) { m_mutex.performLocked!({ if (m_priorityQueue.full) m_priorityQueue.capacity = (m_priorityQueue.capacity * 3) / 2; m_priorityQueue.put(msg); }); m_condition.notify(); } void receive(scope bool delegate(Variant) filter, scope void delegate(Variant) handler) { bool notify; scope (exit) if (notify) m_condition.notify(); Variant args; m_mutex.performLocked!({ notify = this.full; while (true) { import vibe.core.log; logTrace("looking for messages"); if (receiveQueue(m_priorityQueue, args, filter)) break; if (receiveQueue(m_queue, args, filter)) break; logTrace("received no message, waiting.."); m_condition.wait(); notify = this.full; } }); handler(args); } bool receiveTimeout(OPS...)(Duration timeout, scope bool delegate(Variant) filter, scope void delegate(Variant) handler) { import std.datetime; bool notify; scope (exit) if (notify) m_condition.notify(); auto limit_time = Clock.currTime(UTC()) + timeout; Variant args; if (!m_mutex.performLocked!({ notify = this.full; while (true) { if (receiveQueue(m_priorityQueue, args, filter)) break; if (receiveQueue(m_queue, args, filter)) break; auto now = Clock.currTime(UTC()); if (now >= limit_time) return false; m_condition.wait(limit_time - now); notify = this.full; } return true; })) return false; handler(args); return true; } private static bool receiveQueue(OPS...)(ref FixedRingBuffer!Variant queue, ref Variant dst, scope bool delegate(Variant) filter) { auto r = queue[]; while (!r.empty) { scope (failure) queue.removeAt(r); auto msg = r.front; if (filter(msg)) { dst = msg; queue.removeAt(r); return true; } r.popFront(); } return false; } }