[flang-commits] [flang] 62ece9c - [flang][runtime] Add a critical section for LookUpOrCreateAnonymous. (#74468)

via flang-commits flang-commits at lists.llvm.org
Tue Dec 5 19:53:22 PST 2023


Author: SiHuaN
Date: 2023-12-06T11:53:17+08:00
New Revision: 62ece9ce997a3ca077a931eff07bc2cfef978289

URL: https://github.com/llvm/llvm-project/commit/62ece9ce997a3ca077a931eff07bc2cfef978289
DIFF: https://github.com/llvm/llvm-project/commit/62ece9ce997a3ca077a931eff07bc2cfef978289.diff

LOG: [flang][runtime] Add a critical section for LookUpOrCreateAnonymous. (#74468)

In parallel regions `LookUpOrCreateAnonymous` may return an anonymous
unit that has not yet been opened, which may cause a runtime error.
This commit ensures that the returned anonymous unit has been opened.
For details see:
https://github.com/llvm/llvm-project/issues/68856#issuecomment-1788632511

Fixes https://github.com/llvm/llvm-project/issues/68856

Added: 
    

Modified: 
    flang/runtime/unit.cpp

Removed: 
    


################################################################################
diff  --git a/flang/runtime/unit.cpp b/flang/runtime/unit.cpp
index 995656b9480c4..5fa8565c2f61f 100644
--- a/flang/runtime/unit.cpp
+++ b/flang/runtime/unit.cpp
@@ -20,6 +20,7 @@ namespace Fortran::runtime::io {
 // The per-unit data structures are created on demand so that Fortran I/O
 // should work without a Fortran main program.
 static Lock unitMapLock;
+static Lock createOpenLock;
 static UnitMap *unitMap{nullptr};
 static ExternalFileUnit *defaultInput{nullptr}; // unit 5
 static ExternalFileUnit *defaultOutput{nullptr}; // unit 6
@@ -52,6 +53,9 @@ ExternalFileUnit *ExternalFileUnit::LookUpOrCreate(
 ExternalFileUnit *ExternalFileUnit::LookUpOrCreateAnonymous(int unit,
     Direction dir, std::optional<bool> isUnformatted,
     const Terminator &terminator) {
+  // Make sure that the returned anonymous unit has been opened
+  // not just created in the unitMap.
+  CriticalSection critical{createOpenLock};
   bool exists{false};
   ExternalFileUnit *result{
       GetUnitMap().LookUpOrCreate(unit, terminator, exists)};


        


More information about the flang-commits mailing list