vibe.internal.async 0/35(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
module vibe.internal.async; import std.traits : ParameterTypeTuple, ReturnType; import std.typecons : tuple; import vibe.core.core : hibernate, switchToTask; import vibe.core.task : InterruptException, Task; import vibe.core.log; import core.time : Duration, seconds; auto asyncAwait(Callback, alias action, alias cancel)(string func = __FUNCTION__) if (!is(Object == Duration)) { ParameterTypeTuple!Callback results; alias waitable = Waitable!(Callback, action, cancel, (ParameterTypeTuple!Callback r) { results = r; }); asyncAwaitAny!(true, waitable)(func); return tuple(results); } auto asyncAwait(Callback, alias action, alias cancel)(Duration timeout, string func = __FUNCTION__) { static struct R { bool completed = true; ParameterTypeTuple!Callback results; } R ret; static if (is(ReturnType!action == void)) { alias waitable = Waitable!(Callback, action, (cb) { ret.completed = false; cancel(cb); }, (ParameterTypeTuple!Callback r) { ret.results = r; } ); } else { alias waitable = Waitable!(Callback, action, (cb, waitres) { ret.completed = false; cancel(cb, waitres); }, (ParameterTypeTuple!Callback r) { ret.results = r; } ); } asyncAwaitAny!(true, waitable)(timeout, func); return ret; } auto asyncAwaitUninterruptible(Callback, alias action)(string func = __FUNCTION__) nothrow { static if (is(typeof(action(Callback.init)) == void)) void cancel(Callback) { assert(false, "Action cannot be cancelled."); } else void cancel(Callback, typeof(action(Callback.init))) @safe @nogc nothrow { assert(false, "Action cannot be cancelled."); } ParameterTypeTuple!Callback results; alias waitable = Waitable!(Callback, action, cancel, (ParameterTypeTuple!Callback r) { results = r; }); asyncAwaitAny!(false, waitable)(func); return tuple(results); } auto asyncAwaitUninterruptible(Callback, alias action, alias cancel)(Duration timeout, string func = __FUNCTION__) nothrow { ParameterTypeTuple!Callback results; alias waitable = Waitable!(Callback, action, cancel, (ParameterTypeTuple!Callback r) { results = r; }); asyncAwaitAny!(false, waitable)(timeout, func); return tuple(results); } template Waitable(CB, alias WAIT, alias CANCEL, alias DONE) { import std.traits : ReturnType; static assert(is(typeof(WAIT(CB.init))), "WAIT must be callable with a parameter of type "~CB.stringof); static if (is(typeof(WAIT(CB.init)) == void)) static assert(is(typeof(CANCEL(CB.init))), "CANCEL must be callable with a parameter of type "~CB.stringof); else static assert(is(typeof(CANCEL(CB.init, typeof(WAIT(CB.init)).init))), "CANCEL must be callable with parameters "~CB.stringof~ " and "~typeof(WAIT(CB.init)).stringof); static assert(is(typeof(DONE(ParameterTypeTuple!CB.init))), "DONE must be callable with types "~ParameterTypeTuple!CB.stringof); alias Callback = CB; alias wait = WAIT; alias cancel = CANCEL; alias done = DONE; } void asyncAwaitAny(bool interruptible, Waitables...)(Duration timeout, string func = __FUNCTION__) { if (timeout == Duration.max) asyncAwaitAny!(interruptible, Waitables)(func); else { import eventcore.core; auto tm = eventDriver.timers.create(); eventDriver.timers.set(tm, timeout, 0.seconds); scope (exit) eventDriver.timers.releaseRef(tm); alias timerwaitable = Waitable!(TimerCallback, cb => eventDriver.timers.wait(tm, cb), cb => eventDriver.timers.cancelWait(tm), (tid) {} ); asyncAwaitAny!(interruptible, timerwaitable, Waitables)(func); } } void asyncAwaitAny(bool interruptible, Waitables...)(string func = __FUNCTION__) if (Waitables.length >= 1) { import std.meta : staticMap; import std.algorithm.searching : any; import std.format : format; import std.meta : AliasSeq; import std.traits : ReturnType; bool[Waitables.length] fired; bool any_fired = false; Task t; bool still_inside = true; scope (exit) still_inside = false; debug(VibeAsyncLog) logDebugV("Performing %s async operations in %s", Waitables.length, func); static string waitableCode() { string ret; foreach (i, W; Waitables) { alias PTypes = ParameterTypeTuple!(CBDel!W); ret ~= q{ alias PT%1$s = ParameterTypeTuple!(Waitables[%1$s].Callback); scope callback_%1$s = (%2$s) @safe nothrow { // NOTE: this triggers DigitalMars/optlink#18 //() @trusted { logDebugV("siw %%x", &still_inside); } (); debug(VibeAsyncLog) logDebugV("Waitable %%s in %%s fired (istask=%%s).", %1$s, func, t != Task.init); assert(still_inside, "Notification fired after asyncAwait had already returned!"); fired[%1$s] = true; any_fired = true; Waitables[%1$s].done(%3$s); if (t != Task.init) switchToTask(t); }; debug(VibeAsyncLog) logDebugV("Starting operation %%s", %1$s); alias WR%1$s = typeof(Waitables[%1$s].wait(callback_%1$s)); static if (is(WR%1$s == void)) Waitables[%1$s].wait(callback_%1$s); else auto wr%1$s = Waitables[%1$s].wait(callback_%1$s); scope (exit) { if (!fired[%1$s]) { debug(VibeAsyncLog) logDebugV("Cancelling operation %%s", %1$s); static if (is(WR%1$s == void)) Waitables[%1$s].cancel(callback_%1$s); else Waitables[%1$s].cancel(callback_%1$s, wr%1$s); any_fired = true; fired[%1$s] = true; } } if (any_fired) { debug(VibeAsyncLog) logDebugV("Returning to %%s without waiting.", func); return; } }.format(i, generateParamDecls!(CBDel!W)(format("PT%s", i)), generateParamNames!(CBDel!W)); } return ret; } mixin(waitableCode()); debug(VibeAsyncLog) logDebugV("Need to wait in %s (%s)...", func, interruptible ? "interruptible" : "uninterruptible"); t = Task.getThis(); debug (VibeAsyncLog) scope (failure) logDebugV("Aborting wait due to exception"); do { static if (interruptible) { bool interrupted = false; hibernate(() @safe nothrow { debug(VibeAsyncLog) logDebugV("Got interrupted in %s.", func); interrupted = true; }); debug(VibeAsyncLog) logDebugV("Task resumed (fired=%s, interrupted=%s)", fired, interrupted); if (interrupted) throw new InterruptException; } else { hibernate(); debug(VibeAsyncLog) logDebugV("Task resumed (fired=%s)", fired); } } while (!any_fired); debug(VibeAsyncLog) logDebugV("Return result for %s.", func); } private alias CBDel(alias Waitable) = Waitable.Callback; @safe nothrow /*@nogc*/ unittest { int cnt = 0; auto ret = asyncAwaitUninterruptible!(void delegate(int) @safe nothrow, (cb) { cnt++; cb(42); }); assert(ret[0] == 42); assert(cnt == 1); } @safe nothrow /*@nogc*/ unittest { int a, b, c; int w1r, w2r; alias w1 = Waitable!( void delegate(int) @safe nothrow, (cb) { a++; cb(42); }, (cb) { assert(false); }, (i) { w1r = i; } ); alias w2 = Waitable!( void delegate(int) @safe nothrow, (cb) { b++; }, (cb) { c++; }, (i) { w2r = i; } ); alias w3 = Waitable!( void delegate(int) @safe nothrow, (cb) { c++; cb(42); }, (cb) { assert(false); }, (int n) { assert(n == 42); } ); asyncAwaitAny!(false, w1, w2); assert(w1r == 42 && w2r == 0); assert(a == 1 && b == 0 && c == 0); asyncAwaitAny!(false, w2, w1); assert(w1r == 42 && w2r == 0); assert(a == 2 && b == 1 && c == 1); asyncAwaitAny!(false, w3); assert(c == 2); } private string generateParamDecls(Fun)(string ptypes_name = "PTypes") { import std.format : format; import std.traits : ParameterTypeTuple, ParameterStorageClass, ParameterStorageClassTuple; if (!__ctfe) assert(false); alias Types = ParameterTypeTuple!Fun; alias SClasses = ParameterStorageClassTuple!Fun; string ret; foreach (i, T; Types) { static if (i > 0) ret ~= ", "; static if (SClasses[i] & ParameterStorageClass.lazy_) ret ~= "lazy "; static if (SClasses[i] & ParameterStorageClass.scope_) ret ~= "scope "; static if (SClasses[i] & ParameterStorageClass.out_) ret ~= "out "; static if (SClasses[i] & ParameterStorageClass.ref_) ret ~= "ref "; ret ~= format("%s[%s] param_%s", ptypes_name, i, i); } return ret; } private string generateParamNames(Fun)() { import std.format : format; if (!__ctfe) assert(false); string ret; foreach (i, T; ParameterTypeTuple!Fun) { static if (i > 0) ret ~= ", "; ret ~= format("param_%s", i); } return ret; } private template hasAnyScopeParameter(Callback) { import std.algorithm.searching : any; import std.traits : ParameterStorageClass, ParameterStorageClassTuple; alias SC = ParameterStorageClassTuple!Callback; static if (SC.length == 0) enum hasAnyScopeParameter = false; else enum hasAnyScopeParameter = any!(c => c & ParameterStorageClass.scope_)([SC]); }