vibe.core.taskpool 0/81(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
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
2910
2920
2930
2940
2950
2960
2970
2980
2990
3000
3010
3020
3030
3040
3050
3060
3070
3080
3090
3100
3110
3120
3130
3140
3150
3160
3170
3180
3190
3200
3210
3220
3230
3240
3250
3260
3270
3280
3290
3300
3310
3320
3330
3340
3350
3360
3370
3380
3390
3400
3410
3420
3430
3440
3450
3460
3470
3480
3490
3500
/** Multi-threaded task pool implementation. Copyright: © 2012-2017 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.core.taskpool; import vibe.core.concurrency : isWeaklyIsolated; import vibe.core.core : exitEventLoop, logicalProcessorCount, runEventLoop, runTask, runTask_internal; import vibe.core.log; import vibe.core.sync : ManualEvent, Monitor, SpinLock, createSharedManualEvent, createMonitor; import vibe.core.task : Task, TaskFuncInfo, callWithMove; import core.sync.mutex : Mutex; import core.thread : Thread; import std.concurrency : prioritySend, receiveOnly; import std.traits : isFunctionPointer; /** Implements a shared, multi-threaded task pool. */ shared final class TaskPool { private { struct State { WorkerThread[] threads; TaskQueue queue; bool term; } vibe.core.sync.Monitor!(State, shared(SpinLock)) m_state; shared(ManualEvent) m_signal; immutable size_t m_threadCount; } /** Creates a new task pool with the specified number of threads. Params: thread_count: The number of worker threads to create */ this(size_t thread_count = logicalProcessorCount()) @safe { import std.format : format; m_threadCount = thread_count; m_signal = createSharedManualEvent(); with (m_state.lock) { queue.setup(); threads.length = thread_count; foreach (i; 0 .. thread_count) { WorkerThread thr; () @trusted { thr = new WorkerThread(this); thr.name = format("vibe-%s", i); thr.start(); } (); threads[i] = thr; } } } /** Returns the number of worker threads. */ @property size_t threadCount() const shared { return m_threadCount; } /** Instructs all worker threads to terminate and waits until all have finished. */ void terminate() @safe nothrow { m_state.lock.term = true; m_signal.emit(); auto ec = m_signal.emitCount; while (true) { WorkerThread th; with (m_state.lock) if (threads.length) { th = threads[0]; threads = threads[1 .. $]; } if (!th) break; () @trusted { try th.join(); catch (Exception e) { logWarn("Failed to wait for worker thread exit: %s", e.msg); } } (); } size_t cnt = m_state.lock.queue.length; if (cnt > 0) logWarn("There were still %d worker tasks pending at exit.", cnt); } /** Instructs all worker threads to terminate as soon as all tasks have been processed and waits for them to finish. */ void join() @safe nothrow { assert(false, "TODO!"); } /** Runs a new asynchronous task in a worker thread. Only function pointers with weakly isolated arguments are allowed to be able to guarantee thread-safety. */ void runTask(FT, ARGS...)(FT func, auto ref ARGS args) if (isFunctionPointer!FT) { foreach (T; ARGS) static assert(isWeaklyIsolated!T, "Argument type "~T.stringof~" is not safe to pass between threads."); runTask_unsafe(func, args); } /// ditto void runTask(alias method, T, ARGS...)(shared(T) object, auto ref ARGS args) if (is(typeof(__traits(getMember, object, __traits(identifier, method))))) { foreach (T; ARGS) static assert(isWeaklyIsolated!T, "Argument type "~T.stringof~" is not safe to pass between threads."); auto func = &__traits(getMember, object, __traits(identifier, method)); runTask_unsafe(func, args); } /** Runs a new asynchronous task in a worker thread, returning the task handle. This function will yield and wait for the new task to be created and started in the worker thread, then resume and return it. Only function pointers with weakly isolated arguments are allowed to be able to guarantee thread-safety. */ Task runTaskH(FT, ARGS...)(FT func, auto ref ARGS args) if (isFunctionPointer!FT) { import std.typecons : Typedef; foreach (T; ARGS) static assert(isWeaklyIsolated!T, "Argument type "~T.stringof~" is not safe to pass between threads."); alias PrivateTask = Typedef!(Task, Task.init, __PRETTY_FUNCTION__); Task caller = Task.getThis(); // workaround for runWorkerTaskH to work when called outside of a task if (caller == Task.init) { Task ret; .runTask(&runTaskHWrapper!(FT, ARGS), () @trusted { return &ret; } (), func, args).join(); return ret; } assert(caller != Task.init, "runWorkderTaskH can currently only be called from within a task."); static void taskFun(Task caller, FT func, ARGS args) { PrivateTask callee = Task.getThis(); caller.tid.prioritySend(callee); mixin(callWithMove!ARGS("func", "args")); } runTask_unsafe(&taskFun, caller, func, args); return cast(Task)receiveOnly!PrivateTask(); } /// ditto Task runTaskH(alias method, T, ARGS...)(shared(T) object, auto ref ARGS args) if (is(typeof(__traits(getMember, object, __traits(identifier, method))))) { static void wrapper()(shared(T) object, ref ARGS args) { __traits(getMember, object, __traits(identifier, method))(args); } return runTaskH(&wrapper!(), object, args); } /** Runs a new asynchronous task in all worker threads concurrently. This function is mainly useful for long-living tasks that distribute their work across all CPU cores. Only function pointers with weakly isolated arguments are allowed to be able to guarantee thread-safety. The number of tasks started is guaranteed to be equal to `workerThreadCount`. */ void runTaskDist(FT, ARGS...)(FT func, auto ref ARGS args) if (is(typeof(*func) == function)) { foreach (T; ARGS) static assert(isWeaklyIsolated!T, "Argument type "~T.stringof~" is not safe to pass between threads."); runTaskDist_unsafe(func, args); } /// ditto void runTaskDist(alias method, T, ARGS...)(shared(T) object, auto ref ARGS args) { auto func = &__traits(getMember, object, __traits(identifier, method)); foreach (T; ARGS) static assert(isWeaklyIsolated!T, "Argument type "~T.stringof~" is not safe to pass between threads."); runTaskDist_unsafe(func, args); } private void runTaskHWrapper(FT, ARGS...)(Task* ret, FT func, ARGS args) { *ret = runTaskH!(FT, ARGS)(func, args); } private void runTask_unsafe(CALLABLE, ARGS...)(CALLABLE callable, ref ARGS args) { import std.traits : ParameterTypeTuple; import vibe.internal.traits : areConvertibleTo; import vibe.internal.typetuple; alias FARGS = ParameterTypeTuple!CALLABLE; static assert(areConvertibleTo!(Group!ARGS, Group!FARGS), "Cannot convert arguments '"~ARGS.stringof~"' to function arguments '"~FARGS.stringof~"'."); m_state.lock.queue.put(callable, args); m_signal.emitSingle(); } private void runTaskDist_unsafe(CALLABLE, ARGS...)(ref CALLABLE callable, ARGS args) // NOTE: no ref for args, to disallow non-copyable types! { import std.traits : ParameterTypeTuple; import vibe.internal.traits : areConvertibleTo; import vibe.internal.typetuple; alias FARGS = ParameterTypeTuple!CALLABLE; static assert(areConvertibleTo!(Group!ARGS, Group!FARGS), "Cannot convert arguments '"~ARGS.stringof~"' to function arguments '"~FARGS.stringof~"'."); { auto st = m_state.lock; foreach (thr; st.threads) { // create one TFI per thread to properly account for elaborate assignment operators/postblit thr.m_queue.put(callable, args); } } m_signal.emit(); } } private final class WorkerThread : Thread { private { shared(TaskPool) m_pool; TaskQueue m_queue; } this(shared(TaskPool) pool) { m_pool = pool; m_queue.setup(); super(&main); } private void main() nothrow { import core.stdc.stdlib : abort; import core.exception : InvalidMemoryOperationError; import std.encoding : sanitize; try { if (m_pool.m_state.lock.term) return; logDebug("entering worker thread"); runTask(&handleWorkerTasks); logDebug("running event loop"); if (!m_pool.m_state.lock.term) runEventLoop(); logDebug("Worker thread exit."); } catch (Exception e) { scope (failure) abort(); logFatal("Worker thread terminated due to uncaught exception: %s", e.msg); logDebug("Full error: %s", e.toString().sanitize()); } catch (Throwable th) { logFatal("Worker thread terminated due to uncaught error: %s", th.msg); logDebug("Full error: %s", th.toString().sanitize()); abort(); } } private void handleWorkerTasks() nothrow @safe { import std.algorithm.iteration : filter; import std.algorithm.mutation : swap; import std.algorithm.searching : count; import std.array : array; logDebug("worker thread enter"); TaskFuncInfo taskfunc; while(true){ auto emit_count = m_pool.m_signal.emitCount; with (m_pool.m_state.lock) { logDebug("worker thread check"); if (term) break; if (m_queue.consume(taskfunc)) { logDebug("worker thread got specific task"); } else if (queue.consume(taskfunc)) { logDebug("worker thread got specific task"); } } if (taskfunc.func !is null) .runTask_internal!((ref tfi) { swap(tfi, taskfunc); }); else emit_count = m_pool.m_signal.waitUninterruptible(emit_count); } logDebug("worker thread exit"); if (!m_queue.empty) logWarn("Worker thread shuts down with specific worker tasks left in its queue."); with (m_pool.m_state.lock) { threads = threads.filter!(t => t !is this).array; if (threads.length > 0 && !queue.empty) logWarn("Worker threads shut down with worker tasks still left in the queue."); } exitEventLoop(); } } private struct TaskQueue { nothrow @safe: // TODO: avoid use of GC import vibe.internal.array : FixedRingBuffer; FixedRingBuffer!TaskFuncInfo* m_queue; void setup() { m_queue = new FixedRingBuffer!TaskFuncInfo; } @property bool empty() const { return m_queue.empty; } @property size_t length() const { return m_queue.length; } void put(CALLABLE, ARGS...)(ref CALLABLE c, ref ARGS args) { import std.algorithm.comparison : max; if (m_queue.full) m_queue.capacity = max(16, m_queue.capacity * 3 / 2); assert(!m_queue.full); m_queue.peekDst[0].set(c, args); m_queue.putN(1); } bool consume(ref TaskFuncInfo tfi) { import std.algorithm.mutation : swap; if (m_queue.empty) return false; swap(tfi, m_queue.front); m_queue.popFront(); return true; } }