Fix initialization behavior when being loaded as a shared library.

When loading a shared library where both, the host and the library, use eventcore, the static constructors/destructors will be called multiple times.
This commit is contained in:
Sönke Ludwig 2018-05-04 14:57:20 +02:00
parent 242432c416
commit 48b214dd09
No known key found for this signature in database
GPG key ID: D95E8DB493EE314C

View file

@ -27,24 +27,35 @@ else alias NativeEventDriver = EventDriver;
static if (!is(NativeEventDriver == EventDriver)) { static if (!is(NativeEventDriver == EventDriver)) {
static this() static this()
{ {
if (!s_driver) s_driver = new NativeEventDriver; if (!s_isMainThread) {
if (!s_initCount++) {
assert(s_driver is null);
s_driver = new NativeEventDriver;
}
}
} }
static ~this() static ~this()
{ {
if (!s_isMainThread) if (!s_isMainThread) {
s_driver.dispose(); if (!--s_initCount)
s_driver.dispose();
}
} }
shared static this() shared static this()
{ {
s_driver = new NativeEventDriver; if (!s_initCount++) {
s_isMainThread = true; s_driver = new NativeEventDriver;
s_isMainThread = true;
}
} }
shared static ~this() shared static ~this()
{ {
s_driver.dispose(); if (!--s_initCount) {
s_driver.dispose();
}
} }
} else { } else {
void setupEventDriver(EventDriver driver) void setupEventDriver(EventDriver driver)
@ -58,4 +69,7 @@ static if (!is(NativeEventDriver == EventDriver)) {
private { private {
NativeEventDriver s_driver; NativeEventDriver s_driver;
bool s_isMainThread; bool s_isMainThread;
// keeps track of nested DRuntime initializations that happen when
// (un)loading shared libaries.
int s_initCount = 0;
} }