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