From 37573afb8aea3688b3bb2fe30919300efe198ac0 Mon Sep 17 00:00:00 2001 From: Roland Schulz Date: Sun, 21 Sep 2014 18:03:17 -0400 Subject: [PATCH] Allow runAsMainSingleModule to be called more than once Call finalizeForCommandLine even if an exception is thrown so that it is OK to call runAsMainSingleModule again. This allows this (and the calling runAsMain as e.g. used in the template) be called from some framework which doesn't reset globals. Change-Id: I9eff770e06feef90f99ec0445bd294816f8b6a0a --- src/gromacs/commandline/cmdlineinit.cpp | 7 +++++++ src/gromacs/commandline/cmdlineinit.h | 14 +++++++++++++- src/gromacs/commandline/cmdlinemodulemanager.cpp | 2 +- src/gromacs/commandline/cmdlinemodulemanager.h | 2 +- src/programs/gmx.cpp | 2 +- src/testutils/testoptions.cpp | 2 +- 6 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/gromacs/commandline/cmdlineinit.cpp b/src/gromacs/commandline/cmdlineinit.cpp index 8fe171ee05..003ad846c9 100644 --- a/src/gromacs/commandline/cmdlineinit.cpp +++ b/src/gromacs/commandline/cmdlineinit.cpp @@ -140,6 +140,13 @@ void finalizeForCommandLine() g_commandLineContext.reset(); } +int processExceptionAtExitForCommandLine(const std::exception &ex) +{ + int rc = processExceptionAtExit(ex); //Currently this aborts for GMX_LIB_MPI + finalizeForCommandLine(); //thus this MPI_Finalize doesn't matter + return rc; +} + int runCommandLineModule(int argc, char *argv[], CommandLineModuleInterface *module) { diff --git a/src/gromacs/commandline/cmdlineinit.h b/src/gromacs/commandline/cmdlineinit.h index ae4089f4ae..0f95039cbc 100644 --- a/src/gromacs/commandline/cmdlineinit.h +++ b/src/gromacs/commandline/cmdlineinit.h @@ -92,7 +92,19 @@ CommandLineProgramContext &initForCommandLine(int *argc, char ***argv); * \ingroup module_commandline */ void finalizeForCommandLine(); - +/*! \brief + * Handles an exception and deinitializes after initForCommandLine. + * + * \param[in] ex Exception that is the cause for terminating the program. + * \returns Return code to return from main(). + * + * This method should be called as the last thing before terminating the + * program because of an exception. See processExceptionAtExit() for details. + * Additionally this method undoes the work done by initForCommandLine. + * + * Does not throw. + */ +int processExceptionAtExitForCommandLine(const std::exception &ex); /*! \brief * Implements a main() method that runs a single module. * diff --git a/src/gromacs/commandline/cmdlinemodulemanager.cpp b/src/gromacs/commandline/cmdlinemodulemanager.cpp index 8fee8aa10e..74113cc531 100644 --- a/src/gromacs/commandline/cmdlinemodulemanager.cpp +++ b/src/gromacs/commandline/cmdlinemodulemanager.cpp @@ -622,7 +622,7 @@ int CommandLineModuleManager::runAsMainSingleModule( catch (const std::exception &ex) { printFatalErrorMessage(stderr, ex); - return processExceptionAtExit(ex); + return processExceptionAtExitForCommandLine(ex); } } diff --git a/src/gromacs/commandline/cmdlinemodulemanager.h b/src/gromacs/commandline/cmdlinemodulemanager.h index 62b39488ca..707d251e5e 100644 --- a/src/gromacs/commandline/cmdlinemodulemanager.h +++ b/src/gromacs/commandline/cmdlinemodulemanager.h @@ -82,7 +82,7 @@ typedef gmx_unique_ptr::type catch (const std::exception &ex) { gmx::printFatalErrorMessage(stderr, ex); - return gmx::processExceptionAtExit(ex); + return gmx::processExceptionAtExitForCommandLine(ex); } } * \endcode diff --git a/src/programs/gmx.cpp b/src/programs/gmx.cpp index 3381f6bc8e..94a668eadb 100644 --- a/src/programs/gmx.cpp +++ b/src/programs/gmx.cpp @@ -64,6 +64,6 @@ main(int argc, char *argv[]) catch (const std::exception &ex) { gmx::printFatalErrorMessage(stderr, ex); - return gmx::processExceptionAtExit(ex); + return gmx::processExceptionAtExitForCommandLine(ex); } } diff --git a/src/testutils/testoptions.cpp b/src/testutils/testoptions.cpp index a46d8875c3..77f49b9dd1 100644 --- a/src/testutils/testoptions.cpp +++ b/src/testutils/testoptions.cpp @@ -189,7 +189,7 @@ void initTestUtils(const char *dataPath, const char *tempPath, int *argc, char * catch (const std::exception &ex) { printFatalErrorMessage(stderr, ex); - std::exit(processExceptionAtExit(ex)); + std::exit(processExceptionAtExitForCommandLine(ex)); } } -- 2.22.0