Initialize the driver instance lazily.

This commit is contained in:
Sönke Ludwig 2018-10-21 20:16:57 +02:00
parent 284d4f43c3
commit 73abd867b4

View file

@ -7,6 +7,7 @@ import eventcore.drivers.posix.epoll;
import eventcore.drivers.posix.kqueue; import eventcore.drivers.posix.kqueue;
import eventcore.drivers.libasync; import eventcore.drivers.libasync;
import eventcore.drivers.winapi.driver; import eventcore.drivers.winapi.driver;
import eventcore.internal.utils : mallocT, freeT;
version (EventcoreEpollDriver) alias NativeEventDriver = EpollEventDriver; version (EventcoreEpollDriver) alias NativeEventDriver = EpollEventDriver;
else version (EventcoreKqueueDriver) alias NativeEventDriver = KqueueEventDriver; else version (EventcoreKqueueDriver) alias NativeEventDriver = KqueueEventDriver;
@ -19,8 +20,11 @@ else alias NativeEventDriver = EventDriver;
@safe @nogc nothrow { @safe @nogc nothrow {
static if (is(NativeEventDriver == EventDriver)) static if (is(NativeEventDriver == EventDriver))
assert(s_driver !is null, "setupEventDriver() was not called for this thread."); assert(s_driver !is null, "setupEventDriver() was not called for this thread.");
else else {
assert(s_driver !is null, "eventcore.core static constructor didn't run!?"); if (!s_driver) {
s_driver = mallocT!NativeEventDriver();
}
}
return s_driver; return s_driver;
} }
@ -30,7 +34,6 @@ static if (!is(NativeEventDriver == EventDriver)) {
if (!s_isMainThread) { if (!s_isMainThread) {
if (!s_initCount++) { if (!s_initCount++) {
assert(s_driver is null); assert(s_driver is null);
s_driver = new NativeEventDriver;
} }
} }
} }
@ -38,15 +41,18 @@ static if (!is(NativeEventDriver == EventDriver)) {
static ~this() static ~this()
{ {
if (!s_isMainThread) { if (!s_isMainThread) {
if (!--s_initCount) if (!--s_initCount) {
if (s_driver) {
s_driver.dispose(); s_driver.dispose();
freeT(s_driver);
}
}
} }
} }
shared static this() shared static this()
{ {
if (!s_initCount++) { if (!s_initCount++) {
s_driver = new NativeEventDriver;
s_isMainThread = true; s_isMainThread = true;
} }
} }
@ -54,7 +60,10 @@ static if (!is(NativeEventDriver == EventDriver)) {
shared static ~this() shared static ~this()
{ {
if (!--s_initCount) { if (!--s_initCount) {
if (s_driver) {
s_driver.dispose(); s_driver.dispose();
freeT(s_driver);
}
} }
} }
} else { } else {