From 2998b46bc4f5b71268135df1f6f3d0915221485f Mon Sep 17 00:00:00 2001 From: Nimish Mishra Date: Sat, 2 Dec 2023 06:32:15 +0000 Subject: [PATCH] Move deallocation of I/O resources under STOP to a critical section --- flang/runtime/stop.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/flang/runtime/stop.cpp b/flang/runtime/stop.cpp index 98324da1d91e1..f6e5f0c593024 100644 --- a/flang/runtime/stop.cpp +++ b/flang/runtime/stop.cpp @@ -16,6 +16,7 @@ #include #include +static Fortran::runtime::Lock lock; extern "C" { static void DescribeIEEESignaledExceptions() { @@ -52,7 +53,8 @@ static void CloseAllExternalUnits(const char *why) { [[noreturn]] void RTNAME(StopStatement)( int code, bool isErrorStop, bool quiet) { - CloseAllExternalUnits("STOP statement"); + + Fortran::runtime::CriticalSection critical{lock}; if (Fortran::runtime::executionEnvironment.noStopMessage && code == 0) { quiet = true; } @@ -64,12 +66,13 @@ static void CloseAllExternalUnits(const char *why) { std::fputc('\n', stderr); DescribeIEEESignaledExceptions(); } + CloseAllExternalUnits("STOP statement"); std::exit(code); } [[noreturn]] void RTNAME(StopStatementText)( const char *code, std::size_t length, bool isErrorStop, bool quiet) { - CloseAllExternalUnits("STOP statement"); + Fortran::runtime::CriticalSection critical{lock}; if (!quiet) { if (Fortran::runtime::executionEnvironment.noStopMessage && !isErrorStop) { std::fprintf(stderr, "%.*s\n", static_cast(length), code); @@ -79,6 +82,7 @@ static void CloseAllExternalUnits(const char *why) { } DescribeIEEESignaledExceptions(); } + CloseAllExternalUnits("STOP statement"); if (isErrorStop) { std::exit(EXIT_FAILURE); } else {