vibe.core.task 4/292(1%) 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
56120
57120
580
590
600
610
620
630
640
650
660
670
680
690
700
710
720
730
740
750
760
770
780
7972
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
12524
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
3510
3520
3530
3540
3550
3560
3570
3580
3590
3600
3610
3620
3630
3640
3650
3660
3670
3680
3690
3700
3710
3720
3730
3740
3750
3760
3770
3780
3790
3800
3810
3820
3830
3840
3850
3860
3870
3880
3890
3900
3910
3920
3930
3940
3950
3960
3970
3980
3990
4000
4010
4020
4030
4040
4050
4060
4070
4080
4090
4100
4110
4120
4130
4140
4150
4160
4170
4180
4190
4200
4210
4220
4230
4240
4250
4260
4270
4280
4290
4300
4310
4320
4330
4340
4350
4360
4370
4380
4390
4400
4410
4420
4430
4440
4450
4460
4470
4480
4490
4500
4510
4520
4530
4540
4550
4560
4570
4580
4590
4600
4610
4620
4630
4640
4650
4660
4670
4680
4690
4700
4710
4720
4730
4740
4750
4760
4770
4780
4790
4800
4810
4820
4830
4840
4850
4860
4870
4880
4890
4900
4910
4920
4930
4940
4950
4960
4970
4980
4990
5000
5010
5020
5030
5040
5050
5060
5070
5080
5090
5100
5110
5120
5130
5140
5150
5160
5170
5180
5190
5200
5210
5220
5230
5240
5250
5260
5270
5280
5290
5300
5310
5320
5330
5340
5350
5360
5370
5380
5390
5400
5410
5420
5430
5440
5450
5460
5470
5480
5490
5500
5510
5520
5530
5540
5550
5560
5570
5580
5590
5600
5610
5620
5630
5640
5650
5660
5670
5680
5690
5700
5710
5720
5730
5740
5750
5760
5770
5780
5790
5800
5810
5820
5830
5840
5850
5860
5870
5880
5890
5900
5910
5920
5930
5940
5950
5960
5970
5980
5990
6000
6010
6020
6030
6040
6050
6060
6070
6080
6090
6100
6110
6120
6130
6140
6150
6160
6170
6180
6190
6200
6210
6220
6230
6240
6250
6260
6270
6280
6290
6300
6310
6320
6330
6340
6350
6360
6370
6380
6390
6400
6410
6420
6430
6440
6450
6460
6470
6480
6490
6500
6510
6520
6530
6540
6550
6560
6570
6580
6590
6600
6610
6620
6630
6640
6650
6660
6670
6680
6690
6700
6710
6720
6730
6740
6750
6760
6770
6780
6790
6800
6810
6820
6830
6840
6850
6860
6870
6880
6890
6900
6910
6920
6930
6940
6950
6960
6970
6980
6990
7000
7010
7020
7030
7040
7050
7060
7070
7080
7090
7100
7110
7120
7130
7140
7150
7160
7170
7180
7190
7200
7210
7220
7230
7240
7250
7260
7270
7280
7290
7300
7310
7320
7330
7340
7350
7360
7370
7380
7390
7400
7410
7420
7430
7440
7450
7460
7470
7480
7490
7500
7510
7520
7530
7540
7550
7560
7570
7580
7590
7600
7610
7620
7630
7640
7650
7660
7670
7680
7690
7700
7710
7720
7730
7740
7750
7760
7770
7780
7790
7800
7810
7820
7830
7840
7850
7860
7870
7880
7890
7900
7910
7920
7930
7940
7950
7960
7970
7980
7990
8000
8010
8020
8030
8040
8050
8060
8070
8080
8090
8100
8110
8120
8130
8140
8150
8160
8170
8180
8190
8200
8210
8220
8230
8240
8250
8260
8270
8280
8290
8300
8310
8320
8330
8340
8350
8360
8370
8380
8390
8400
8410
8420
8430
8440
8450
8460
8470
8480
8490
8500
8510
8520
8530
8540
8550
8560
8570
8580
8590
8600
8610
8620
8630
8640
8650
8660
8670
8680
8690
8700
8710
8720
8730
8740
8750
8760
8770
8780
8790
8800
8810
8820
8830
8840
8850
8860
8870
8880
8890
8900
8910
8920
8930
8940
8950
8960
8970
8980
8990
9000
9010
9020
9030
9040
9050
9060
9070
9080
9090
9100
9110
9120
9130
9140
9150
9160
9170
9180
9190
9200
9210
9220
9230
9240
9250
9260
9270
9280
9290
9300
9310
9320
9330
9340
9350
9360
9370
9380
9390
9400
9410
9420
9430
9440
9450
9460
9470
9480
9490
9500
9510
9520
9530
9540
9550
9560
9570
9580
9590
9600
9610
9620
9630
9640
9650
9660
9670
9680
9690
9700
9710
9720
9730
9740
9750
9760
9770
9780
9790
9800
9810
9820
9830
9840
9850
9860
9870
9880
9890
9900
9910
9920
9930
9940
9950
9960
9970
9980
9990
10000
10010
10020
10030
10040
10050
10060
10070
10080
10090
10100
10110
10120
10130
10140
10150
10160
10170
10180
10190
10200
10210
10220
10230
10240
10250
10260
10270
10280
10290
/** Contains interfaces and enums for evented I/O drivers. Copyright: © 2012-2016 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.log; import vibe.core.sync; 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) @safe nothrow { m_fiber = () @trusted { return cast(shared(TaskFiber))other.m_fiber; } (); m_taskCounter = other.m_taskCounter; } /** Returns the Task instance belonging to the calling task. */ static Task getThis() @safe nothrow { // In 2067, synchronized statements where annotated nothrow. // DMD#4115, Druntime#1013, Druntime#1021, Phobos#2704 // However, they were "logically" nothrow before. static if (__VERSION__ <= 2066) scope (failure) assert(0, "Internal error: function should be nothrow"); auto fiber = () @trusted { return Fiber.getThis(); } (); if (!fiber) return Task.init; auto tfiber = cast(TaskFiber)fiber; if (!tfiber) return Task.init; // FIXME: returning a non-.init handle for a finished task might break some layered logic return () @trusted { return Task(tfiber, tfiber.m_taskCounter); } (); } nothrow { package @property inout(TaskFiber) taskFiber() inout @system { return cast(inout(TaskFiber))m_fiber; } @property inout(Fiber) fiber() inout @system { return this.taskFiber; } @property size_t taskCounter() const @safe { return m_taskCounter; } @property inout(Thread) thread() inout @trusted { if (m_fiber) return this.taskFiber.thread; return null; } /** Determines if the task is still running. */ @property bool running() // FIXME: this is NOT thread safe const @trusted { assert(m_fiber !is null, "Invalid task handle"); try if (this.taskFiber.state == Fiber.State.TERM) return false; catch (Throwable) {} return this.taskFiber.m_running && this.taskFiber.m_taskCounter == m_taskCounter; } package @property ref ThreadInfo tidInfo() @system { return m_fiber ? taskFiber.tidInfo : s_tidInfo; } // FIXME: this is not thread safe! package @property ref const(ThreadInfo) tidInfo() const @system { return m_fiber ? taskFiber.tidInfo : s_tidInfo; } // FIXME: this is not thread safe! /** Gets the `Tid` associated with this task for use with `std.concurrency`. */ @property Tid tid() @trusted { return tidInfo.ident; } /// ditto @property const(Tid) tid() const @trusted { return tidInfo.ident; } } T opCast(T)() const @safe nothrow if (is(T == bool)) { return m_fiber !is null; } void join() @trusted { if (running) taskFiber.join!true(m_taskCounter); } // FIXME: this is NOT thread safe void joinUninterruptible() @trusted nothrow { if (running) taskFiber.join!false(m_taskCounter); } // FIXME: this is NOT thread safe void interrupt() @trusted nothrow { if (running) taskFiber.interrupt(m_taskCounter); } // FIXME: this is NOT thread safe string toString() const @safe { import std.string; return format("%s:%s", () @trusted { return cast(void*)m_fiber; } (), m_taskCounter); } void getDebugID(R)(ref R dst) { import std.digest.md : MD5; import std.bitmanip : nativeToLittleEndian; import std.base64 : Base64; if (!m_fiber) { dst.put("----"); return; } MD5 md; md.start(); md.put(nativeToLittleEndian(() @trusted { return cast(size_t)cast(void*)m_fiber; } ())); md.put(nativeToLittleEndian(m_taskCounter)); Base64.encode(md.finish()[0 .. 3], dst); if (!this.running) dst.put("-fin"); } string getDebugID() @trusted { import std.array : appender; auto app = appender!string; getDebugID(app); return app.data; } bool opEquals(in ref Task other) const @safe nothrow { return m_fiber is other.m_fiber && m_taskCounter == other.m_taskCounter; } bool opEquals(in Task other) const @safe nothrow { return m_fiber is other.m_fiber && m_taskCounter == other.m_taskCounter; } } /** Implements a task local storage variable. Task local variables, similar to thread local variables, exist separately in each task. Consequently, they do not need any form of synchronization when accessing them. Note, however, that each TaskLocal variable will increase the memory footprint of any task that uses task local storage. There is also an overhead to access TaskLocal variables, higher than for thread local variables, but generelly still O(1) (since actual storage acquisition is done lazily the first access can require a memory allocation with unknown computational costs). Notice: FiberLocal instances MUST be declared as static/global thread-local variables. Defining them as a temporary/stack variable will cause crashes or data corruption! Examples: --- TaskLocal!string s_myString = "world"; void taskFunc() { assert(s_myString == "world"); s_myString = "hello"; assert(s_myString == "hello"); } shared static this() { // both tasks will get independent storage for s_myString runTask(&taskFunc); runTask(&taskFunc); } --- */ struct TaskLocal(T) { private { size_t m_offset = size_t.max; size_t m_id; T m_initValue; bool m_hasInitValue = false; } this(T init_val) { m_initValue = init_val; m_hasInitValue = true; } @disable this(this); void opAssign(T value) { this.storage = value; } @property ref T storage() @safe { import std.conv : emplace; auto fiber = TaskFiber.getThis(); // lazily register in FLS storage if (m_offset == size_t.max) { static assert(T.alignof <= 8, "Unsupported alignment for type "~T.stringof); assert(TaskFiber.ms_flsFill % 8 == 0, "Misaligned fiber local storage pool."); m_offset = TaskFiber.ms_flsFill; m_id = TaskFiber.ms_flsCounter++; TaskFiber.ms_flsFill += T.sizeof; while (TaskFiber.ms_flsFill % 8 != 0) TaskFiber.ms_flsFill++; } // make sure the current fiber has enough FLS storage if (fiber.m_fls.length < TaskFiber.ms_flsFill) { fiber.m_fls.length = TaskFiber.ms_flsFill + 128; () @trusted { fiber.m_flsInit.length = TaskFiber.ms_flsCounter + 64; } (); } // return (possibly default initialized) value auto data = () @trusted { return fiber.m_fls.ptr[m_offset .. m_offset+T.sizeof]; } (); if (!() @trusted { return fiber.m_flsInit[m_id]; } ()) { () @trusted { fiber.m_flsInit[m_id] = true; } (); import std.traits : hasElaborateDestructor, hasAliasing; static if (hasElaborateDestructor!T || hasAliasing!T) { void function(void[], size_t) destructor = (void[] fls, size_t offset){ static if (hasElaborateDestructor!T) { auto obj = cast(T*)&fls[offset]; // call the destructor on the object if a custom one is known declared obj.destroy(); } else static if (hasAliasing!T) { // zero the memory to avoid false pointers foreach (size_t i; offset .. offset + T.sizeof) { ubyte* u = cast(ubyte*)&fls[i]; *u = 0; } } }; FLSInfo fls_info; fls_info.fct = destructor; fls_info.offset = m_offset; // make sure flsInfo has enough space if (TaskFiber.ms_flsInfo.length <= m_id) TaskFiber.ms_flsInfo.length = m_id + 64; TaskFiber.ms_flsInfo[m_id] = fls_info; } if (m_hasInitValue) { static if (__traits(compiles, () @trusted { emplace!T(data, m_initValue); } ())) () @trusted { emplace!T(data, m_initValue); } (); else assert(false, "Cannot emplace initialization value for type "~T.stringof); } else () @trusted { emplace!T(data); } (); } return *() @trusted { return cast(T*)data.ptr; } (); } alias storage this; } /** Exception that is thrown by Task.interrupt. */ class InterruptException : Exception { this() @safe nothrow { super("Task interrupted."); } } /** High level state change events for a Task */ enum TaskEvent { preStart, /// Just about to invoke the fiber which starts execution postStart, /// After the fiber has returned for the first time (by yield or exit) start, /// Just about to start execution yield, /// Temporarily paused resume, /// Resumed from a prior yield end, /// Ended normally fail /// Ended with an exception } struct TaskCreationInfo { Task handle; const(void)* functionPointer; } alias TaskEventCallback = void function(TaskEvent, Task) nothrow; alias TaskCreationCallback = void function(ref TaskCreationInfo) nothrow @safe; /** The maximum combined size of all parameters passed to a task delegate See_Also: runTask */ enum maxTaskParameterSize = 128; /** 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. */ final package class TaskFiber : Fiber { static if ((void*).sizeof >= 8) enum defaultTaskStackSize = 16*1024*1024; else enum defaultTaskStackSize = 512*1024; private { import std.concurrency : ThreadInfo; import std.bitmanip : BitArray; // task queue management (TaskScheduler.m_taskQueue) TaskFiber m_prev, m_next; TaskFiberQueue* m_queue; Thread m_thread; ThreadInfo m_tidInfo; shared size_t m_taskCounter; shared bool m_running; bool m_shutdown = false; shared(ManualEvent) m_onExit; // task local storage BitArray m_flsInit; void[] m_fls; bool m_interrupt; // Task.interrupt() is progress package int m_yieldLockCount; static TaskFiber ms_globalDummyFiber; static FLSInfo[] ms_flsInfo; static size_t ms_flsFill = 0; // thread-local static size_t ms_flsCounter = 0; } package TaskFuncInfo m_taskFunc; package __gshared size_t ms_taskStackSize = defaultTaskStackSize; package __gshared debug TaskEventCallback ms_taskEventCallback; package __gshared debug TaskCreationCallback ms_taskCreationCallback; this() @trusted nothrow { super(&run, ms_taskStackSize); m_thread = Thread.getThis(); } static TaskFiber getThis() @safe nothrow { auto f = () @trusted nothrow { return Fiber.getThis(); } (); if (auto tf = cast(TaskFiber)f) return tf; if (!ms_globalDummyFiber) ms_globalDummyFiber = new TaskFiber; return ms_globalDummyFiber; } // expose Fiber.state as @safe on older DMD versions static if (!__traits(compiles, () @safe { return Fiber.init.state; } ())) @property State state() @trusted const nothrow { return super.state; } private void run() nothrow { import std.algorithm.mutation : swap; import std.concurrency : Tid, thisTid; import std.encoding : sanitize; import vibe.core.core : isEventLoopRunning, recycleFiber, taskScheduler, yield; version (VibeDebugCatchAll) alias UncaughtException = Throwable; else alias UncaughtException = Exception; try { while (true) { while (!m_taskFunc.func) { try { debug (VibeTaskLog) logTrace("putting fiber to sleep waiting for new task..."); Fiber.yield(); } catch (Exception e) { logWarn("CoreTaskFiber was resumed with exception but without active task!"); logDiagnostic("Full error: %s", e.toString().sanitize()); } if (m_shutdown) return; } TaskFuncInfo task; swap(task, m_taskFunc); Task handle = this.task; try { m_running = true; scope(exit) m_running = false; thisTid; // force creation of a message box debug if (ms_taskEventCallback) ms_taskEventCallback(TaskEvent.start, handle); if (!isEventLoopRunning) { debug (VibeTaskLog) logTrace("Event loop not running at task start - yielding."); taskScheduler.yieldUninterruptible(); debug (VibeTaskLog) logTrace("Initial resume of task."); } task.call(); debug if (ms_taskEventCallback) ms_taskEventCallback(TaskEvent.end, handle); } catch (Exception e) { debug if (ms_taskEventCallback) ms_taskEventCallback(TaskEvent.fail, handle); import std.encoding; logCritical("Task terminated with uncaught exception: %s", e.msg); logDebug("Full error: %s", e.toString().sanitize()); } if (m_interrupt) { logDebug("Task exited while an interrupt was in flight."); m_interrupt = false; } this.tidInfo.ident = Tid.init; // clear message box debug (VibeTaskLog) logTrace("Notifying joining tasks."); m_onExit.emit(); // make sure that the task does not get left behind in the yielder queue if terminated during yield() if (m_queue) m_queue.remove(this); // zero the fls initialization ByteArray for memory safety foreach (size_t i, ref bool b; m_flsInit) { if (b) { if (ms_flsInfo !is null && ms_flsInfo.length >= i && ms_flsInfo[i] != FLSInfo.init) ms_flsInfo[i].destroy(m_fls); b = false; } } assert(!m_queue, "Fiber done but still scheduled to be resumed!?"); // make the fiber available for the next task recycleFiber(this); } } catch(UncaughtException th) { logCritical("CoreTaskFiber was terminated unexpectedly: %s", th.msg); logDiagnostic("Full error: %s", th.toString().sanitize()); } catch (Throwable th) { import std.stdio : stderr, writeln; import core.stdc.stdlib : abort; try stderr.writeln(th); catch (Exception e) { try stderr.writeln(th.msg); catch (Exception e) {} } abort(); } } /** 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); } @property ref inout(ThreadInfo) tidInfo() inout @safe nothrow { return m_tidInfo; } @property size_t taskCounter() const @safe nothrow { return m_taskCounter; } /** Shuts down the task handler loop. */ void shutdown() @safe nothrow { assert(!m_running); m_shutdown = true; while (state != Fiber.State.TERM) () @trusted { try call(Fiber.Rethrow.no); catch (Exception e) assert(false, e.msg); } (); } /** Blocks until the task has ended. */ void join(bool interruptiple)(size_t task_counter) @trusted { auto cnt = m_onExit.emitCount; while (m_running && m_taskCounter == task_counter) { static if (interruptiple) cnt = m_onExit.wait(cnt); else cnt = m_onExit.waitUninterruptible(cnt); } } /** Throws an InterruptExeption within the task as soon as it calls an interruptible function. */ void interrupt(size_t task_counter) @safe nothrow { import vibe.core.core : taskScheduler; if (m_taskCounter != task_counter) return; auto caller = Task.getThis(); if (caller != Task.init) { assert(caller != this.task, "A task cannot interrupt itself."); assert(caller.thread is this.thread, "Interrupting tasks in different threads is not yet supported."); } else assert(() @trusted { return Thread.getThis(); } () is this.thread, "Interrupting tasks in different threads is not yet supported."); debug (VibeTaskLog) logTrace("Resuming task with interrupt flag."); m_interrupt = true; taskScheduler.switchTo(this.task); } void bumpTaskCounter() @safe nothrow { import core.atomic : atomicOp; () @trusted { atomicOp!"+="(this.m_taskCounter, 1); } (); } package void handleInterrupt(scope void delegate() @safe nothrow on_interrupt) @safe nothrow { assert(() @trusted { return Task.getThis().fiber; } () is this, "Handling interrupt outside of the corresponding fiber."); if (m_interrupt && on_interrupt) { debug (VibeTaskLog) logTrace("Handling interrupt flag."); m_interrupt = false; on_interrupt(); } } package void handleInterrupt() @safe { if (m_interrupt) { m_interrupt = false; throw new InterruptException; } } } package struct TaskFuncInfo { void function(ref TaskFuncInfo) func; void[2*size_t.sizeof] callable; void[maxTaskParameterSize] args; debug ulong functionPointer; void set(CALLABLE, ARGS...)(ref CALLABLE callable, ref ARGS args) { assert(!func, "Setting TaskFuncInfo that is already set."); import std.algorithm : move; import std.traits : hasElaborateAssign; import std.conv : to; static struct TARGS { ARGS expand; } static assert(CALLABLE.sizeof <= TaskFuncInfo.callable.length, "Storage required for task callable is too large ("~CALLABLE.sizeof~" vs max "~callable.length~"): "~CALLABLE.stringof); static assert(TARGS.sizeof <= maxTaskParameterSize, "The arguments passed to run(Worker)Task must not exceed "~ maxTaskParameterSize.to!string~" bytes in total size: "~TARGS.sizeof.stringof~" bytes"); debug functionPointer = callPointer(callable); static void callDelegate(ref TaskFuncInfo tfi) { assert(tfi.func is &callDelegate, "Wrong callDelegate called!?"); // copy original call data to stack CALLABLE c; TARGS args; move(*(cast(CALLABLE*)tfi.callable.ptr), c); move(*(cast(TARGS*)tfi.args.ptr), args); // reset the info tfi.func = null; // make the call mixin(callWithMove!ARGS("c", "args.expand")); } func = &callDelegate; () @trusted { static if (hasElaborateAssign!CALLABLE) initCallable!CALLABLE(); static if (hasElaborateAssign!TARGS) initArgs!TARGS(); typedCallable!CALLABLE = callable; foreach (i, A; ARGS) { static if (needsMove!A) args[i].move(typedArgs!TARGS.expand[i]); else typedArgs!TARGS.expand[i] = args[i]; } } (); } void call() { this.func(this); } @property ref C typedCallable(C)() { static assert(C.sizeof <= callable.sizeof); return *cast(C*)callable.ptr; } @property ref A typedArgs(A)() { static assert(A.sizeof <= args.sizeof); return *cast(A*)args.ptr; } void initCallable(C)() nothrow { static const C cinit; this.callable[0 .. C.sizeof] = cast(void[])(&cinit)[0 .. 1]; } void initArgs(A)() nothrow { static const A ainit; this.args[0 .. A.sizeof] = cast(void[])(&ainit)[0 .. 1]; } } private ulong callPointer(C)(ref C callable) @trusted nothrow @nogc { alias IP = ulong; static if (is(C == function)) return cast(IP)cast(void*)callable; else static if (is(C == delegate)) return cast(IP)callable.funcptr; else static if (is(typeof(&callable.opCall) == function)) return cast(IP)cast(void*)&callable.opCall; else static if (is(typeof(&callable.opCall) == delegate)) return cast(IP)(&callable.opCall).funcptr; else return cast(IP)&callable; } package struct TaskScheduler { import eventcore.driver : ExitReason; import eventcore.core : eventDriver; private { TaskFiberQueue m_taskQueue; TaskFiber m_markerTask; } @safe: @disable this(this); @property size_t scheduledTaskCount() const nothrow { return m_taskQueue.length; } /** Lets other pending tasks execute before continuing execution. This will give other tasks or events a chance to be processed. If multiple tasks call this function, they will be processed in a fírst-in-first-out manner. */ void yield() { auto t = Task.getThis(); if (t == Task.init) return; // not really a task -> no-op auto tf = () @trusted { return t.taskFiber; } (); debug (VibeTaskLog) logTrace("Yielding (interrupt=%s)", tf.m_interrupt); tf.handleInterrupt(); if (tf.m_queue !is null) return; // already scheduled to be resumed m_taskQueue.insertBack(tf); doYield(t); tf.handleInterrupt(); } nothrow: /** Performs a single round of scheduling without blocking. This will execute scheduled tasks and process events from the event queue, as long as possible without having to wait. Returns: A reason is returned: $(UL $(LI `ExitReason.exit`: The event loop was exited due to a manual request) $(LI `ExitReason.outOfWaiters`: There are no more scheduled tasks or events, so the application would do nothing from now on) $(LI `ExitReason.idle`: All scheduled tasks and pending events have been processed normally) $(LI `ExitReason.timeout`: Scheduled tasks have been processed, but there were no pending events present.) ) */ ExitReason process() { assert(TaskFiber.getThis().m_yieldLockCount == 0, "May not process events within an active yieldLock()!"); bool any_events = false; while (true) { // process pending tasks bool any_tasks_processed = schedule() != ScheduleStatus.idle; debug (VibeTaskLog) logTrace("Processing pending events..."); ExitReason er = eventDriver.core.processEvents(0.seconds); debug (VibeTaskLog) logTrace("Done."); final switch (er) { case ExitReason.exited: return ExitReason.exited; case ExitReason.outOfWaiters: if (!scheduledTaskCount) return ExitReason.outOfWaiters; break; case ExitReason.timeout: if (!scheduledTaskCount) return any_events || any_tasks_processed ? ExitReason.idle : ExitReason.timeout; break; case ExitReason.idle: any_events = true; if (!scheduledTaskCount) return ExitReason.idle; break; } } } /** Performs a single round of scheduling, blocking if necessary. Returns: A reason is returned: $(UL $(LI `ExitReason.exit`: The event loop was exited due to a manual request) $(LI `ExitReason.outOfWaiters`: There are no more scheduled tasks or events, so the application would do nothing from now on) $(LI `ExitReason.idle`: All scheduled tasks and pending events have been processed normally) ) */ ExitReason waitAndProcess() { // first, process tasks without blocking auto er = process(); final switch (er) { case ExitReason.exited, ExitReason.outOfWaiters: return er; case ExitReason.idle: return ExitReason.idle; case ExitReason.timeout: break; } // if the first run didn't process any events, block and // process one chunk debug (VibeTaskLog) logTrace("Wait for new events to process..."); er = eventDriver.core.processEvents(Duration.max); debug (VibeTaskLog) logTrace("Done."); final switch (er) { case ExitReason.exited: return ExitReason.exited; case ExitReason.outOfWaiters: if (!scheduledTaskCount) return ExitReason.outOfWaiters; break; case ExitReason.timeout: assert(false, "Unexpected return code"); case ExitReason.idle: break; } // finally, make sure that all scheduled tasks are run er = process(); if (er == ExitReason.timeout) return ExitReason.idle; else return er; } void yieldUninterruptible() { auto t = Task.getThis(); if (t == Task.init) return; // not really a task -> no-op auto tf = () @trusted { return t.taskFiber; } (); if (tf.m_queue !is null) return; // already scheduled to be resumed m_taskQueue.insertBack(tf); doYield(t); } /** Holds execution until the task gets explicitly resumed. */ void hibernate() { import vibe.core.core : isEventLoopRunning; auto thist = Task.getThis(); if (thist == Task.init) { assert(!isEventLoopRunning, "Event processing outside of a fiber should only happen before the event loop is running!?"); static import vibe.core.core; vibe.core.core.runEventLoopOnce(); } else { doYield(thist); } } /** Immediately switches execution to the specified task without giving up execution privilege. This forces immediate execution of the specified task. After the tasks finishes or yields, the calling task will continue execution. */ void switchTo(Task t, Flag!"defer" defer = No.defer) { auto thist = Task.getThis(); if (t == thist) return; auto thisthr = thist ? thist.thread : () @trusted { return Thread.getThis(); } (); assert(t.thread is thisthr, "Cannot switch to a task that lives in a different thread."); auto tf = () @trusted { return t.taskFiber; } (); if (tf.m_queue) { debug (VibeTaskLog) logTrace("Task to switch to is already scheduled. Moving to front of queue."); assert(tf.m_queue is &m_taskQueue, "Task is already enqueued, but not in the main task queue."); m_taskQueue.remove(tf); assert(!tf.m_queue, "Task removed from queue, but still has one set!?"); } if (thist == Task.init && defer == No.defer) { assert(TaskFiber.getThis().m_yieldLockCount == 0, "Cannot yield within an active yieldLock()!"); debug (VibeTaskLog) logTrace("switch to task from global context"); resumeTask(t); debug (VibeTaskLog) logTrace("task yielded control back to global context"); } else { auto thistf = () @trusted { return thist.taskFiber; } (); assert(!thistf || !thistf.m_queue, "Calling task is running, but scheduled to be resumed!?"); debug (VibeTaskLog) logDebugV("Switching tasks (%s already in queue)", m_taskQueue.length); if (defer) { m_taskQueue.insertFront(tf); } else { m_taskQueue.insertFront(thistf); m_taskQueue.insertFront(tf); doYield(thist); } } } /** Runs any pending tasks. A pending tasks is a task that is scheduled to be resumed by either `yield` or `switchTo`. Returns: Returns `true` $(I iff) there are more tasks left to process. */ ScheduleStatus schedule() nothrow { if (m_taskQueue.empty) return ScheduleStatus.idle; if (!m_markerTask) m_markerTask = new TaskFiber; // TODO: avoid allocating an actual task here! scope (exit) assert(!m_markerTask.m_queue, "Marker task still in queue!?"); assert(Task.getThis() == Task.init, "TaskScheduler.schedule() may not be called from a task!"); assert(!m_markerTask.m_queue, "TaskScheduler.schedule() was called recursively!"); // keep track of the end of the queue, so that we don't process tasks // infinitely m_taskQueue.insertBack(m_markerTask); while (m_taskQueue.front !is m_markerTask) { auto t = m_taskQueue.front; m_taskQueue.popFront(); debug (VibeTaskLog) logTrace("resuming task"); resumeTask(t.task); debug (VibeTaskLog) logTrace("task out"); assert(!m_taskQueue.empty, "Marker task got removed from tasks queue!?"); if (m_taskQueue.empty) return ScheduleStatus.idle; // handle gracefully in release mode } // remove marker task m_taskQueue.popFront(); debug (VibeTaskLog) logDebugV("schedule finished - %s tasks left in queue", m_taskQueue.length); return m_taskQueue.empty ? ScheduleStatus.allProcessed : ScheduleStatus.busy; } /// Resumes execution of a yielded task. private void resumeTask(Task t) nothrow { import std.encoding : sanitize; debug (VibeTaskLog) logTrace("task fiber resume"); auto uncaught_exception = () @trusted nothrow { return t.fiber.call!(Fiber.Rethrow.no)(); } (); debug (VibeTaskLog) logTrace("task fiber yielded"); if (uncaught_exception) { auto th = cast(Throwable)uncaught_exception; assert(th, "Fiber returned exception object that is not a Throwable!?"); assert(() @trusted nothrow { return t.fiber.state; } () == Fiber.State.TERM); logError("Task terminated with unhandled exception: %s", th.msg); logDebug("Full error: %s", () @trusted { return th.toString().sanitize; } ()); // always pass Errors on if (auto err = cast(Error)th) throw err; } } private void doYield(Task task) { assert(() @trusted { return task.taskFiber; } ().m_yieldLockCount == 0, "May not yield while in an active yieldLock()!"); debug if (TaskFiber.ms_taskEventCallback) () @trusted { TaskFiber.ms_taskEventCallback(TaskEvent.yield, task); } (); () @trusted { Fiber.yield(); } (); debug if (TaskFiber.ms_taskEventCallback) () @trusted { TaskFiber.ms_taskEventCallback(TaskEvent.resume, task); } (); assert(!task.m_fiber.m_queue, "Task is still scheduled after resumption."); } } package enum ScheduleStatus { idle, allProcessed, busy } private struct TaskFiberQueue { @safe nothrow: TaskFiber first, last; size_t length; @disable this(this); @property bool empty() const { return first is null; } @property TaskFiber front() { return first; } void insertFront(TaskFiber task) { assert(task.m_queue is null, "Task is already scheduled to be resumed!"); assert(task.m_prev is null, "Task has m_prev set without being in a queue!?"); assert(task.m_next is null, "Task has m_next set without being in a queue!?"); task.m_queue = &this; if (empty) { first = task; last = task; } else { first.m_prev = task; task.m_next = first; first = task; } length++; } void insertBack(TaskFiber task) { assert(task.m_queue is null, "Task is already scheduled to be resumed!"); assert(task.m_prev is null, "Task has m_prev set without being in a queue!?"); assert(task.m_next is null, "Task has m_next set without being in a queue!?"); task.m_queue = &this; if (empty) { first = task; last = task; } else { last.m_next = task; task.m_prev = last; last = task; } length++; } void popFront() { if (first is last) last = null; assert(first && first.m_queue == &this, "Popping from empty or mismatching queue"); auto next = first.m_next; if (next) next.m_prev = null; first.m_next = null; first.m_queue = null; first = next; length--; } void remove(TaskFiber task) { assert(task.m_queue is &this, "Task is not contained in task queue."); if (task.m_prev) task.m_prev.m_next = task.m_next; else first = task.m_next; if (task.m_next) task.m_next.m_prev = task.m_prev; else last = task.m_prev; task.m_queue = null; task.m_prev = null; task.m_next = null; } } private struct FLSInfo { void function(void[], size_t) fct; size_t offset; void destroy(void[] fls) { fct(fls, offset); } } // mixin string helper to call a function with arguments that potentially have // to be moved package string callWithMove(ARGS...)(string func, string args) { import std.string; string ret = func ~ "("; foreach (i, T; ARGS) { if (i > 0) ret ~= ", "; ret ~= format("%s[%s]", args, i); static if (needsMove!T) ret ~= ".move"; } return ret ~ ");"; } private template needsMove(T) { template isCopyable(T) { enum isCopyable = __traits(compiles, (T a) { return a; }); } template isMoveable(T) { enum isMoveable = __traits(compiles, (T a) { return a.move; }); } enum needsMove = !isCopyable!T; static assert(isCopyable!T || isMoveable!T, "Non-copyable type "~T.stringof~" must be movable with a .move property."); } unittest { enum E { a, move } static struct S { @disable this(this); @property S move() { return S.init; } } static struct T { @property T move() { return T.init; } } static struct U { } static struct V { @disable this(); @disable this(this); @property V move() { return V.init; } } static struct W { @disable this(); } static assert(needsMove!S); static assert(!needsMove!int); static assert(!needsMove!string); static assert(!needsMove!E); static assert(!needsMove!T); static assert(!needsMove!U); static assert(needsMove!V); static assert(!needsMove!W); }