From 48b214dd094ad746f5bb4ecd1cc6a84dc7deb036 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6nke=20Ludwig?= Date: Fri, 4 May 2018 14:57:20 +0200 Subject: [PATCH] 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. --- source/eventcore/core.d | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/source/eventcore/core.d b/source/eventcore/core.d index 9889513..d469d66 100644 --- a/source/eventcore/core.d +++ b/source/eventcore/core.d @@ -27,24 +27,35 @@ 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) - s_driver.dispose(); + if (!s_isMainThread) { + if (!--s_initCount) + s_driver.dispose(); + } } shared static this() { - s_driver = new NativeEventDriver; - s_isMainThread = true; + if (!s_initCount++) { + s_driver = new NativeEventDriver; + s_isMainThread = true; + } } shared static ~this() { - s_driver.dispose(); + 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; }