2016-03-01 19:30:42 +00:00
|
|
|
module vibe.internal.async;
|
|
|
|
|
|
|
|
import std.traits : ParameterTypeTuple;
|
|
|
|
import std.typecons : tuple;
|
2016-06-14 06:01:03 +00:00
|
|
|
import vibe.core.core : hibernate, switchToTask;
|
|
|
|
import vibe.core.task : InterruptException, Task;
|
2016-03-01 19:30:42 +00:00
|
|
|
import vibe.core.log;
|
|
|
|
import core.time : Duration, seconds;
|
|
|
|
|
|
|
|
|
2016-06-14 06:01:03 +00:00
|
|
|
auto asyncAwait(Callback, alias action, alias cancel, string func = __FUNCTION__)()
|
|
|
|
if (!is(Object == Duration)) {
|
2017-01-15 19:59:36 +00:00
|
|
|
Waitable!(action, cancel, Callback) waitable;
|
2016-06-14 07:26:12 +00:00
|
|
|
asyncAwaitAny!(true, func)(waitable);
|
|
|
|
return tuple(waitable.results);
|
2016-06-14 06:01:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
auto asyncAwait(Callback, alias action, alias cancel, string func = __FUNCTION__)(Duration timeout)
|
2016-03-01 19:30:42 +00:00
|
|
|
{
|
2017-01-15 19:59:36 +00:00
|
|
|
Waitable!(action, cancel, Callback) waitable;
|
2016-06-14 07:26:12 +00:00
|
|
|
asyncAwaitAny!(true, func)(timeout, waitable);
|
2016-10-24 22:27:51 +00:00
|
|
|
static struct R {
|
|
|
|
bool completed;
|
|
|
|
typeof(waitable.results) results;
|
|
|
|
}
|
|
|
|
return R(!waitable.cancelled, waitable.results);
|
2016-06-14 06:01:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
auto asyncAwaitUninterruptible(Callback, alias action, string func = __FUNCTION__)()
|
|
|
|
nothrow {
|
2016-10-24 22:27:51 +00:00
|
|
|
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))) { assert(false, "Action cannot be cancelled."); }
|
2017-01-15 19:59:36 +00:00
|
|
|
Waitable!(action, cancel, Callback) waitable;
|
2016-06-14 07:26:12 +00:00
|
|
|
asyncAwaitAny!(false, func)(waitable);
|
|
|
|
return tuple(waitable.results);
|
2016-06-14 06:01:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
auto asyncAwaitUninterruptible(Callback, alias action, alias cancel, string func = __FUNCTION__)(Duration timeout)
|
|
|
|
nothrow {
|
2017-01-15 19:59:36 +00:00
|
|
|
Waitable!(action, cancel, Callback) waitable;
|
2016-06-14 07:26:12 +00:00
|
|
|
asyncAwaitAny!(false, func)(timeout, waitable);
|
|
|
|
return tuple(waitable.results);
|
2016-06-14 06:01:03 +00:00
|
|
|
}
|
|
|
|
|
2017-01-15 19:59:36 +00:00
|
|
|
struct Waitable(alias wait, alias cancel, CB) {
|
2016-10-24 22:27:51 +00:00
|
|
|
import std.traits : ReturnType;
|
|
|
|
|
2017-01-15 19:59:36 +00:00
|
|
|
alias Callback = CB;
|
|
|
|
|
|
|
|
ParameterTypeTuple!Callback results;
|
2016-06-14 07:26:12 +00:00
|
|
|
bool cancelled;
|
2016-10-24 22:27:51 +00:00
|
|
|
auto waitCallback(Callback cb) nothrow { return wait(cb); }
|
|
|
|
|
|
|
|
static if (is(ReturnType!waitCallback == void))
|
|
|
|
void cancelCallback(Callback cb) nothrow { cancel(cb); }
|
|
|
|
else
|
|
|
|
void cancelCallback(Callback cb, ReturnType!waitCallback r) nothrow { cancel(cb, r); }
|
2016-06-14 07:26:12 +00:00
|
|
|
}
|
|
|
|
|
2016-06-15 16:21:04 +00:00
|
|
|
void asyncAwaitAny(bool interruptible, string func = __FUNCTION__, Waitables...)(Duration timeout, ref Waitables waitables)
|
2016-06-14 07:26:12 +00:00
|
|
|
{
|
|
|
|
if (timeout == Duration.max) asyncAwaitAny!(interruptible, func)(waitables);
|
|
|
|
else {
|
|
|
|
import eventcore.core;
|
|
|
|
|
2016-10-05 12:40:29 +00:00
|
|
|
auto tm = eventDriver.timers.create();
|
2016-10-24 06:22:37 +00:00
|
|
|
eventDriver.timers.set(tm, timeout, 0.seconds);
|
2016-10-05 12:40:29 +00:00
|
|
|
scope (exit) eventDriver.timers.releaseRef(tm);
|
2016-06-14 07:26:12 +00:00
|
|
|
Waitable!(
|
2016-10-05 12:40:29 +00:00
|
|
|
cb => eventDriver.timers.wait(tm, cb),
|
2016-10-24 06:22:37 +00:00
|
|
|
cb => eventDriver.timers.cancelWait(tm),
|
2017-01-15 19:59:36 +00:00
|
|
|
TimerCallback
|
2016-06-14 07:26:12 +00:00
|
|
|
) timerwaitable;
|
|
|
|
asyncAwaitAny!(interruptible, func)(timerwaitable, waitables);
|
|
|
|
}
|
|
|
|
}
|
2016-06-14 06:01:03 +00:00
|
|
|
|
2016-06-15 16:21:04 +00:00
|
|
|
void asyncAwaitAny(bool interruptible, string func = __FUNCTION__, Waitables...)(ref Waitables waitables)
|
2016-06-14 07:26:12 +00:00
|
|
|
if (Waitables.length >= 1 && !is(Waitables[0] == Duration))
|
|
|
|
{
|
|
|
|
import std.meta : staticMap;
|
|
|
|
import std.algorithm.searching : any;
|
2017-01-15 19:59:36 +00:00
|
|
|
import std.format : format;
|
|
|
|
import std.meta : AliasSeq;
|
2016-10-24 22:27:51 +00:00
|
|
|
import std.traits : ReturnType;
|
2016-06-14 07:26:12 +00:00
|
|
|
|
|
|
|
/*scope*/ staticMap!(CBDel, Waitables) callbacks; // FIXME: avoid heap delegates
|
2016-03-01 19:30:42 +00:00
|
|
|
|
2016-06-14 07:26:12 +00:00
|
|
|
bool[Waitables.length] fired;
|
2016-06-15 16:21:04 +00:00
|
|
|
ScopeGuard[Waitables.length] scope_guards;
|
|
|
|
bool any_fired = false;
|
2016-03-01 19:30:42 +00:00
|
|
|
Task t;
|
|
|
|
|
2016-06-17 20:33:04 +00:00
|
|
|
bool still_inside = true;
|
|
|
|
scope (exit) still_inside = false;
|
|
|
|
|
2016-10-04 15:51:24 +00:00
|
|
|
debug(VibeAsyncLog) logDebugV("Performing %s async operations in %s", waitables.length, func);
|
2016-06-15 16:21:04 +00:00
|
|
|
|
2016-10-24 22:27:51 +00:00
|
|
|
() @trusted { logDebugV("si %x", &still_inside); } ();
|
|
|
|
|
2016-06-14 07:26:12 +00:00
|
|
|
foreach (i, W; Waitables) {
|
2017-01-15 19:59:36 +00:00
|
|
|
alias PTypes = ParameterTypeTuple!(CBDel!W);
|
|
|
|
/*scope*/auto cb = mixin(q{(%s) @safe nothrow {
|
|
|
|
() @trusted { logDebugV("siw %%x", &still_inside); } ();
|
|
|
|
debug(VibeAsyncLog) logDebugV("Waitable %%s in %%s fired (istask=%%s).", i, func, t != Task.init);
|
2016-06-17 20:33:04 +00:00
|
|
|
assert(still_inside, "Notification fired after asyncAwait had already returned!");
|
2016-06-14 07:26:12 +00:00
|
|
|
fired[i] = true;
|
2016-06-15 16:21:04 +00:00
|
|
|
any_fired = true;
|
2017-01-15 19:59:36 +00:00
|
|
|
static if (PTypes.length)
|
|
|
|
waitables[i].results = AliasSeq!(%s);
|
2016-06-14 07:26:12 +00:00
|
|
|
if (t != Task.init) switchToTask(t);
|
2017-01-15 19:59:36 +00:00
|
|
|
}}.format(generateParamDecls!(CBDel!W), generateParamNames!(CBDel!W)));
|
2016-06-17 20:33:04 +00:00
|
|
|
callbacks[i] = cb;
|
2016-03-01 19:30:42 +00:00
|
|
|
|
2016-10-04 15:51:24 +00:00
|
|
|
debug(VibeAsyncLog) logDebugV("Starting operation %s", i);
|
2016-10-24 22:27:51 +00:00
|
|
|
static if (is(ReturnType!(W.waitCallback) == void))
|
|
|
|
waitables[i].waitCallback(callbacks[i]);
|
|
|
|
else
|
|
|
|
auto wr = waitables[i].waitCallback(callbacks[i]);
|
2016-06-17 20:33:04 +00:00
|
|
|
|
2016-10-04 15:51:24 +00:00
|
|
|
scope ccb = () @safe nothrow {
|
2016-06-14 07:26:12 +00:00
|
|
|
if (!fired[i]) {
|
2016-10-04 15:51:24 +00:00
|
|
|
debug(VibeAsyncLog) logDebugV("Cancelling operation %s", i);
|
2016-10-24 22:27:51 +00:00
|
|
|
static if (is(ReturnType!(W.waitCallback) == void))
|
|
|
|
waitables[i].cancelCallback(callbacks[i]);
|
|
|
|
else
|
|
|
|
waitables[i].cancelCallback(callbacks[i], wr);
|
2016-06-17 20:33:04 +00:00
|
|
|
waitables[i].cancelled = true;
|
2016-06-15 16:21:04 +00:00
|
|
|
any_fired = true;
|
|
|
|
fired[i] = true;
|
2016-06-14 07:26:12 +00:00
|
|
|
}
|
2016-06-17 20:33:04 +00:00
|
|
|
};
|
|
|
|
scope_guards[i] = ScopeGuard(ccb);
|
|
|
|
|
2016-06-15 16:21:04 +00:00
|
|
|
if (any_fired) {
|
2016-10-04 15:51:24 +00:00
|
|
|
debug(VibeAsyncLog) logDebugV("Returning to %s without waiting.", func);
|
2016-06-15 16:21:04 +00:00
|
|
|
return;
|
2016-06-14 07:26:12 +00:00
|
|
|
}
|
2016-03-01 19:30:42 +00:00
|
|
|
}
|
2016-06-14 07:26:12 +00:00
|
|
|
|
2016-10-04 15:51:24 +00:00
|
|
|
debug(VibeAsyncLog) logDebugV("Need to wait in %s (%s)...", func, interruptible ? "interruptible" : "uninterruptible");
|
2016-06-17 20:33:04 +00:00
|
|
|
|
2016-06-14 07:26:12 +00:00
|
|
|
t = Task.getThis();
|
2016-06-17 20:33:04 +00:00
|
|
|
|
2016-10-24 22:27:51 +00:00
|
|
|
debug (VibeAsyncLog) scope (failure) logDebugV("Aborting wait due to exception");
|
|
|
|
|
2016-06-14 07:26:12 +00:00
|
|
|
do {
|
|
|
|
static if (interruptible) {
|
|
|
|
bool interrupted = false;
|
|
|
|
hibernate(() @safe nothrow {
|
2016-10-04 15:51:24 +00:00
|
|
|
debug(VibeAsyncLog) logDebugV("Got interrupted in %s.", func);
|
2016-06-14 07:26:12 +00:00
|
|
|
interrupted = true;
|
|
|
|
});
|
2016-10-04 15:51:24 +00:00
|
|
|
debug(VibeAsyncLog) logDebugV("Task resumed (fired=%s, interrupted=%s)", fired, interrupted);
|
2016-06-14 07:26:12 +00:00
|
|
|
if (interrupted)
|
|
|
|
throw new InterruptException;
|
2016-06-17 20:33:04 +00:00
|
|
|
} else {
|
|
|
|
hibernate();
|
2016-10-04 15:51:24 +00:00
|
|
|
debug(VibeAsyncLog) logDebugV("Task resumed (fired=%s)", fired);
|
2016-06-17 20:33:04 +00:00
|
|
|
}
|
2016-06-15 16:21:04 +00:00
|
|
|
} while (!any_fired);
|
2016-06-14 07:26:12 +00:00
|
|
|
|
2016-10-04 15:51:24 +00:00
|
|
|
debug(VibeAsyncLog) logDebugV("Return result for %s.", func);
|
2016-03-01 19:30:42 +00:00
|
|
|
}
|
2016-06-14 07:26:12 +00:00
|
|
|
|
2017-01-15 19:59:36 +00:00
|
|
|
private alias CBDel(Waitable) = Waitable.Callback;
|
2016-06-15 16:21:04 +00:00
|
|
|
|
|
|
|
private struct ScopeGuard { @safe nothrow: void delegate() op; ~this() { if (op !is null) op(); } }
|
|
|
|
|
|
|
|
@safe nothrow /*@nogc*/ unittest {
|
|
|
|
int cnt = 0;
|
2017-01-15 19:59:36 +00:00
|
|
|
auto ret = asyncAwaitUninterruptible!(void delegate(int) @safe nothrow, (cb) { cnt++; cb(42); });
|
2016-06-15 16:21:04 +00:00
|
|
|
assert(ret[0] == 42);
|
|
|
|
assert(cnt == 1);
|
2016-06-17 20:33:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@safe nothrow /*@nogc*/ unittest {
|
|
|
|
int a, b, c;
|
|
|
|
Waitable!(
|
|
|
|
(cb) { a++; cb(42); },
|
|
|
|
(cb) { assert(false); },
|
2017-01-15 19:59:36 +00:00
|
|
|
void delegate(int) @safe nothrow
|
2016-06-17 20:33:04 +00:00
|
|
|
) w1;
|
|
|
|
Waitable!(
|
|
|
|
(cb) { b++; },
|
|
|
|
(cb) { c++; },
|
2017-01-15 19:59:36 +00:00
|
|
|
void delegate(int) @safe nothrow
|
2016-06-17 20:33:04 +00:00
|
|
|
) w2;
|
|
|
|
|
|
|
|
asyncAwaitAny!false(w1, w2);
|
|
|
|
assert(w1.results[0] == 42 && w2.results[0] == 0);
|
|
|
|
assert(a == 1 && b == 0 && c == 0);
|
|
|
|
|
|
|
|
asyncAwaitAny!false(w2, w1);
|
|
|
|
assert(w1.results[0] == 42 && w2.results[0] == 0);
|
|
|
|
assert(a == 2 && b == 1 && c == 1);
|
|
|
|
}
|
2017-01-15 19:59:36 +00:00
|
|
|
|
|
|
|
private string generateParamDecls(Fun)()
|
|
|
|
{
|
|
|
|
import std.format : format;
|
|
|
|
import std.traits : ParameterTypeTuple, ParameterStorageClass, ParameterStorageClassTuple;
|
|
|
|
|
|
|
|
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("PTypes[%s] param_%s", i, i);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
private string generateParamNames(Fun)()
|
|
|
|
{
|
|
|
|
import std.format : format;
|
|
|
|
|
|
|
|
string ret;
|
|
|
|
foreach (i, T; ParameterTypeTuple!Fun) {
|
|
|
|
static if (i > 0) ret ~= ", ";
|
|
|
|
ret ~= format("param_%s", i);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|