From a6b58d2fdb2ea22a36da571d8a5e3526caf12d2b Mon Sep 17 00:00:00 2001 From: Emanuele Danovaro Date: Wed, 17 Apr 2024 12:14:27 +0100 Subject: [PATCH] fix PooledFile multi threading --- VERSION | 2 +- src/eckit/io/PooledFile.cc | 20 ++++++++++++-------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/VERSION b/VERSION index dd43a143f..c7c3f3333 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.26.1 +1.26.2 diff --git a/src/eckit/io/PooledFile.cc b/src/eckit/io/PooledFile.cc index 2eab33485..c124316bc 100644 --- a/src/eckit/io/PooledFile.cc +++ b/src/eckit/io/PooledFile.cc @@ -26,7 +26,8 @@ namespace eckit { class PoolFileEntry; -static thread_local std::map> pool_; +static std::map> pool_; +static std::mutex poolMutex_; struct PoolFileEntryStatus { @@ -73,10 +74,11 @@ class PoolFileEntry { void remove(const PooledFile* file) { auto s = statuses_.find(file); - if (s != statuses_.end()) { - statuses_.erase(s); - } + ASSERT(s != statuses_.end()); + + statuses_.erase(s); if (statuses_.size() == 0) { + std::lock_guard lock(poolMutex_); doClose(); pool_.erase(name_); // No code after !!! @@ -113,10 +115,10 @@ class PoolFileEntry { void close(const PooledFile* file) { auto s = statuses_.find(file); - if (s != statuses_.end()) { - ASSERT(s->second.opened_); - s->second.opened_ = false; - } + ASSERT(s != statuses_.end()); + + ASSERT(s->second.opened_); + s->second.opened_ = false; } int fileno(const PooledFile* file) const { @@ -194,6 +196,8 @@ class PoolFileEntry { PooledFile::PooledFile(const PathName& name) : name_(name), entry_(nullptr) { + + std::lock_guard lock(poolMutex_); auto j = pool_.find(name); if (j == pool_.end()) { pool_.emplace(std::make_pair(name, std::unique_ptr(new PoolFileEntry(name))));