Skip to content

Commit

Permalink
fix PooledFile multi threading
Browse files Browse the repository at this point in the history
  • Loading branch information
danovaro committed Apr 17, 2024
1 parent 7b974be commit a6b58d2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.26.1
1.26.2
20 changes: 12 additions & 8 deletions src/eckit/io/PooledFile.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ namespace eckit {

class PoolFileEntry;

static thread_local std::map<PathName, std::unique_ptr<PoolFileEntry>> pool_;
static std::map<PathName, std::unique_ptr<PoolFileEntry>> pool_;
static std::mutex poolMutex_;

struct PoolFileEntryStatus {

Expand Down Expand Up @@ -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<std::mutex> lock(poolMutex_);
doClose();
pool_.erase(name_);
// No code after !!!
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -194,6 +196,8 @@ class PoolFileEntry {

PooledFile::PooledFile(const PathName& name) :
name_(name), entry_(nullptr) {

std::lock_guard<std::mutex> lock(poolMutex_);
auto j = pool_.find(name);
if (j == pool_.end()) {
pool_.emplace(std::make_pair(name, std::unique_ptr<PoolFileEntry>(new PoolFileEntry(name))));
Expand Down

0 comments on commit a6b58d2

Please sign in to comment.