Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

Commit

Permalink
Merge pull request #600 from dawgfoto/fix10976
Browse files Browse the repository at this point in the history
fix Issue 10976 - thread_joinAll after main exit performed too late
  • Loading branch information
Alex Rønne Petersen committed Sep 17, 2013
2 parents 43110e7 + 7d82a57 commit db77296
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
9 changes: 6 additions & 3 deletions src/core/runtime.d
Expand Up @@ -117,9 +117,12 @@ struct Runtime


/**
* Terminates the runtime. This call is to be used in instances where the
* standard program termination process will not be not executed. This is
* most often in shared libraries or in libraries linked to a C program.
* Terminates the runtime. This call is to be used in instances
* where the standard program termination process will not be not
* executed. This is most often in shared libraries or in
* libraries linked to a C program. All non-daemon threads must be
* joined or detached prior to calling this function. See also
* $(CXREF thread, thread_joinAll) and $(CXREF thread, thread_detachThis).
*
* Params:
* dg = A delegate which will receive any exception thrown during the
Expand Down
19 changes: 18 additions & 1 deletion src/rt/dmain2.d
Expand Up @@ -201,8 +201,21 @@ extern (C) bool rt_term(ExceptionHandler dg = null)
{
try
{
/* Check that all other non-daemon threads have finished
* execution before calling the shared module destructors.
* Calling thread_joinAll here would be too late because other
* shared libraries might have already been
* destructed/unloaded.
*/
import core.thread : Thread;
auto tthis = Thread.getThis();
foreach (t; Thread)
{
if (t !is tthis && t.isRunning && !t.isDaemon)
assert(0, "Can only call rt_term when all non-daemon threads have been joined or detached.");
}

rt_moduleTlsDtor();
thread_joinAll();
rt_moduleDtor();
gc_term();
finiSections();
Expand Down Expand Up @@ -481,6 +494,8 @@ extern (C) int _d_run_main(int argc, char **argv, MainFunc mainFunc)
tryExec({ result = mainFunc(args); });
else
result = EXIT_FAILURE;

tryExec({thread_joinAll();});
}

void runMainWithInit()
Expand All @@ -490,6 +505,8 @@ extern (C) int _d_run_main(int argc, char **argv, MainFunc mainFunc)
else
result = EXIT_FAILURE;

tryExec({thread_joinAll();});

if (!rt_term())
result = (result == EXIT_SUCCESS) ? EXIT_FAILURE : result;
}
Expand Down

0 comments on commit db77296

Please sign in to comment.