2021-09-02 16:14:01 +08:00
|
|
|
//===-- runtime/unit-map.cpp ----------------------------------------------===//
|
2020-02-14 06:41:56 +08:00
|
|
|
//
|
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "unit-map.h"
|
|
|
|
|
|
|
|
namespace Fortran::runtime::io {
|
|
|
|
|
|
|
|
ExternalFileUnit *UnitMap::LookUpForClose(int n) {
|
|
|
|
CriticalSection critical{lock_};
|
|
|
|
Chain *previous{nullptr};
|
|
|
|
int hash{Hash(n)};
|
|
|
|
for (Chain *p{bucket_[hash].get()}; p; previous = p, p = p->next.get()) {
|
|
|
|
if (p->unit.unitNumber() == n) {
|
|
|
|
if (previous) {
|
|
|
|
previous->next.swap(p->next);
|
|
|
|
} else {
|
|
|
|
bucket_[hash].swap(p->next);
|
|
|
|
}
|
|
|
|
// p->next.get() == p at this point; the next swap pushes p on closing_
|
|
|
|
closing_.swap(p->next);
|
|
|
|
return &p->unit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void UnitMap::DestroyClosed(ExternalFileUnit &unit) {
|
|
|
|
Chain *p{nullptr};
|
|
|
|
{
|
|
|
|
CriticalSection critical{lock_};
|
|
|
|
Chain *previous{nullptr};
|
|
|
|
for (p = closing_.get(); p; previous = p, p = p->next.get()) {
|
|
|
|
if (&p->unit == &unit) {
|
|
|
|
if (previous) {
|
|
|
|
previous->next.swap(p->next);
|
|
|
|
} else {
|
|
|
|
closing_.swap(p->next);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (p) {
|
|
|
|
p->unit.~ExternalFileUnit();
|
|
|
|
FreeMemory(p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void UnitMap::CloseAll(IoErrorHandler &handler) {
|
2021-12-05 03:30:01 +08:00
|
|
|
// Extract units from the map so they can be closed
|
|
|
|
// without holding lock_.
|
|
|
|
OwningPtr<Chain> closeList;
|
|
|
|
{
|
|
|
|
CriticalSection critical{lock_};
|
|
|
|
for (int j{0}; j < buckets_; ++j) {
|
|
|
|
while (Chain * p{bucket_[j].get()}) {
|
|
|
|
bucket_[j].swap(p->next); // pops p from head of bucket list
|
|
|
|
closeList.swap(p->next); // pushes p to closeList
|
|
|
|
}
|
2020-02-14 06:41:56 +08:00
|
|
|
}
|
|
|
|
}
|
2021-12-05 03:30:01 +08:00
|
|
|
while (Chain * p{closeList.get()}) {
|
|
|
|
closeList.swap(p->next); // pops p from head of closeList
|
|
|
|
p->unit.CloseUnit(CloseStatus::Keep, handler);
|
|
|
|
p->unit.~ExternalFileUnit();
|
|
|
|
FreeMemory(p);
|
|
|
|
}
|
2020-02-14 06:41:56 +08:00
|
|
|
}
|
|
|
|
|
2020-07-04 03:38:22 +08:00
|
|
|
void UnitMap::FlushAll(IoErrorHandler &handler) {
|
|
|
|
CriticalSection critical{lock_};
|
|
|
|
for (int j{0}; j < buckets_; ++j) {
|
|
|
|
for (Chain *p{bucket_[j].get()}; p; p = p->next.get()) {
|
2021-06-29 02:41:04 +08:00
|
|
|
p->unit.FlushOutput(handler);
|
2020-07-04 03:38:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-04 02:35:29 +08:00
|
|
|
ExternalFileUnit *UnitMap::Find(const char *path) {
|
|
|
|
if (path) {
|
|
|
|
// TODO: Faster data structure
|
|
|
|
for (int j{0}; j < buckets_; ++j) {
|
|
|
|
for (Chain *p{bucket_[j].get()}; p; p = p->next.get()) {
|
|
|
|
if (p->unit.path() && std::strcmp(p->unit.path(), path) == 0) {
|
|
|
|
return &p->unit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-02-14 06:41:56 +08:00
|
|
|
ExternalFileUnit &UnitMap::Create(int n, const Terminator &terminator) {
|
2020-07-03 09:35:20 +08:00
|
|
|
Chain &chain{*New<Chain>{terminator}(n).release()};
|
2020-02-14 06:41:56 +08:00
|
|
|
chain.next.reset(&chain);
|
2020-03-29 12:00:16 +08:00
|
|
|
bucket_[Hash(n)].swap(chain.next); // pushes new node as list head
|
2020-02-14 06:41:56 +08:00
|
|
|
return chain.unit;
|
|
|
|
}
|
2021-06-26 01:40:08 +08:00
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
} // namespace Fortran::runtime::io
|