Skip to content

Commit

Permalink
Delay deletion of the main thread's event driver to "~shared static t…
Browse files Browse the repository at this point in the history
…his()".
  • Loading branch information
s-ludwig committed May 2, 2014
1 parent eea8206 commit 2be4cce
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
11 changes: 9 additions & 2 deletions source/vibe/core/core.d
Expand Up @@ -1149,6 +1149,8 @@ shared static this()

shared static ~this()
{
deleteEventDriver();

bool tasks_left = false;

synchronized (st_threadsMutex) {
Expand Down Expand Up @@ -1179,6 +1181,9 @@ static this()
static ~this()
{
auto thisthr = Thread.getThis();

bool is_main_thread = false;

synchronized (st_threadsMutex) {
auto idx = st_threads.countUntil!(c => c.thread is thisthr);
assert(idx >= 0);
Expand All @@ -1188,15 +1193,17 @@ static ~this()
}

// if we are the main thread, wait for all others before terminating
if (idx == 0) { // we are the main thread, wait for others
is_main_thread = idx == 0;
if (is_main_thread) { // we are the main thread, wait for others
atomicStore(st_term, true);
st_threadsSignal.emit();
while (st_threads.length)
st_threadShutdownCondition.wait();
}
}

deleteEventDriver();
// delay deletion of the main event driver to "~shared static this()"
if (!is_main_thread) deleteEventDriver();

st_threadShutdownCondition.notifyAll();
}
Expand Down
1 change: 1 addition & 0 deletions source/vibe/core/driver.d
Expand Up @@ -38,6 +38,7 @@ package void deleteEventDriver()
{
// TODO: use destroy() instead
delete s_driver;
s_driver = null;
}


Expand Down

0 comments on commit 2be4cce

Please sign in to comment.