Merge pull request #79 from vibe-d/nested_initialization

Fix initialization behavior when being loaded as a shared library.
This commit is contained in:
Sönke Ludwig 2018-05-05 12:16:57 +02:00 committed by GitHub
commit 745e4ea2c6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -27,25 +27,36 @@ 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) {
if (!--s_initCount)
s_driver.dispose(); s_driver.dispose();
} }
}
shared static this() shared static this()
{ {
if (!s_initCount++) {
s_driver = new NativeEventDriver; s_driver = new NativeEventDriver;
s_isMainThread = true; s_isMainThread = true;
} }
}
shared static ~this() shared static ~this()
{ {
if (!--s_initCount) {
s_driver.dispose(); 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;
} }