xref: /llvm-project/flang/runtime/unit-map.cpp (revision 73b193aec21d5203bde76c76bd0a29c6f77daf36)
1 //===-- runtime/unit-map.cpp ----------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "unit-map.h"
10 
11 namespace Fortran::runtime::io {
12 
13 void UnitMap::Initialize() {
14   if (!isInitialized_) {
15     freeNewUnits_.InitializeState();
16     // Unit number -1 is reserved.
17     // The unit numbers are pushed in reverse order so that the first
18     // ones to be popped will be small and suitable for use as kind=1
19     // integers.
20     for (int j{freeNewUnits_.maxValue}; j > 1; --j) {
21       freeNewUnits_.Add(j);
22     }
23     isInitialized_ = true;
24   }
25 }
26 
27 // See 12.5.6.12 in Fortran 2018.  NEWUNIT= unit numbers are negative,
28 // and not equal to -1 (or ERROR_UNIT, if it were negative, which it isn't.)
29 ExternalFileUnit &UnitMap::NewUnit(const Terminator &terminator) {
30   CriticalSection critical{lock_};
31   Initialize();
32   std::optional<int> n{freeNewUnits_.PopValue()};
33   if (!n) {
34     n = emergencyNewUnit_++;
35   }
36   return Create(-*n, terminator);
37 }
38 
39 ExternalFileUnit *UnitMap::LookUpForClose(int n) {
40   CriticalSection critical{lock_};
41   Chain *previous{nullptr};
42   int hash{Hash(n)};
43   for (Chain *p{bucket_[hash].get()}; p; previous = p, p = p->next.get()) {
44     if (p->unit.unitNumber() == n) {
45       if (previous) {
46         previous->next.swap(p->next);
47       } else {
48         bucket_[hash].swap(p->next);
49       }
50       // p->next.get() == p at this point; the next swap pushes p on closing_
51       closing_.swap(p->next);
52       return &p->unit;
53     }
54   }
55   return nullptr;
56 }
57 
58 void UnitMap::DestroyClosed(ExternalFileUnit &unit) {
59   Chain *p{nullptr};
60   {
61     CriticalSection critical{lock_};
62     Chain *previous{nullptr};
63     for (p = closing_.get(); p; previous = p, p = p->next.get()) {
64       if (&p->unit == &unit) {
65         int n{unit.unitNumber()};
66         if (n <= -2) {
67           freeNewUnits_.Add(-n);
68         }
69         if (previous) {
70           previous->next.swap(p->next);
71         } else {
72           closing_.swap(p->next);
73         }
74         break;
75       }
76     }
77   }
78   if (p) {
79     p->unit.~ExternalFileUnit();
80     FreeMemory(p);
81   }
82 }
83 
84 void UnitMap::CloseAll(IoErrorHandler &handler) {
85   // Extract units from the map so they can be closed
86   // without holding lock_.
87   OwningPtr<Chain> closeList;
88   {
89     CriticalSection critical{lock_};
90     for (int j{0}; j < buckets_; ++j) {
91       while (Chain * p{bucket_[j].get()}) {
92         bucket_[j].swap(p->next); // pops p from head of bucket list
93         closeList.swap(p->next); // pushes p to closeList
94       }
95     }
96   }
97   while (Chain * p{closeList.get()}) {
98     closeList.swap(p->next); // pops p from head of closeList
99     p->unit.CloseUnit(CloseStatus::Keep, handler);
100     p->unit.~ExternalFileUnit();
101     FreeMemory(p);
102   }
103 }
104 
105 void UnitMap::FlushAll(IoErrorHandler &handler) {
106   CriticalSection critical{lock_};
107   for (int j{0}; j < buckets_; ++j) {
108     for (Chain *p{bucket_[j].get()}; p; p = p->next.get()) {
109       p->unit.FlushOutput(handler);
110     }
111   }
112 }
113 
114 ExternalFileUnit *UnitMap::Find(const char *path) {
115   if (path) {
116     // TODO: Faster data structure
117     for (int j{0}; j < buckets_; ++j) {
118       for (Chain *p{bucket_[j].get()}; p; p = p->next.get()) {
119         if (p->unit.path() && std::strcmp(p->unit.path(), path) == 0) {
120           return &p->unit;
121         }
122       }
123     }
124   }
125   return nullptr;
126 }
127 
128 ExternalFileUnit &UnitMap::Create(int n, const Terminator &terminator) {
129   Chain &chain{*New<Chain>{terminator}(n).release()};
130   chain.next.reset(&chain);
131   bucket_[Hash(n)].swap(chain.next); // pushes new node as list head
132   return chain.unit;
133 }
134 
135 } // namespace Fortran::runtime::io
136