Skip to content

Commit

Permalink
addressed PR comment
Browse files Browse the repository at this point in the history
  • Loading branch information
danovaro committed Apr 17, 2024
1 parent a6b58d2 commit c13bef4
Showing 1 changed file with 43 additions and 16 deletions.
59 changes: 43 additions & 16 deletions src/eckit/io/PooledFile.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,31 @@
#include "eckit/io/PooledFile.h"
#include "eckit/log/Bytes.h"


namespace eckit {

class PoolFileEntry;
}

namespace {

class Pool {

private:
Pool() {}
std::map<eckit::PathName, std::unique_ptr<eckit::PoolFileEntry>> filePool_;
std::mutex filePoolMutex_;

public:
static Pool& instance() {
static Pool pool;
return pool;
}
eckit::PoolFileEntry* get(const eckit::PathName& name);
void erase(const eckit::PathName& name);
};

}

static std::map<PathName, std::unique_ptr<PoolFileEntry>> pool_;
static std::mutex poolMutex_;
namespace eckit {

struct PoolFileEntryStatus {

Expand Down Expand Up @@ -78,9 +96,8 @@ class PoolFileEntry {

statuses_.erase(s);
if (statuses_.size() == 0) {
std::lock_guard<std::mutex> lock(poolMutex_);
doClose();
pool_.erase(name_);
Pool::instance().erase(name_);
// No code after !!!
}
}
Expand Down Expand Up @@ -116,7 +133,7 @@ class PoolFileEntry {
void close(const PooledFile* file) {
auto s = statuses_.find(file);
ASSERT(s != statuses_.end());

ASSERT(s->second.opened_);
s->second.opened_ = false;
}
Expand Down Expand Up @@ -195,16 +212,8 @@ class PoolFileEntry {


PooledFile::PooledFile(const PathName& name) :
name_(name), entry_(nullptr) {
name_(name), entry_(Pool::instance().get(name)) {

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))));
j = pool_.find(name);
}

entry_ = (*j).second.get();
entry_->add(this);
}

Expand Down Expand Up @@ -266,3 +275,21 @@ PooledFileError::PooledFileError(const std::string& file, const std::string& msg
FileError(msg + " : error on pooled file " + file, loc) {}

} // namespace eckit

namespace {

eckit::PoolFileEntry* Pool::get(const eckit::PathName& name) {
std::lock_guard<std::mutex> lock(filePoolMutex_);
auto j = filePool_.find(name);
if (j == filePool_.end()) {
filePool_.emplace(std::make_pair(name, std::unique_ptr<eckit::PoolFileEntry>(new eckit::PoolFileEntry(name))));
j = filePool_.find(name);
}
return (*j).second.get();
}
void Pool::erase(const eckit::PathName& name) {
std::lock_guard<std::mutex> lock(filePoolMutex_);
filePool_.erase(name);
}

}

0 comments on commit c13bef4

Please sign in to comment.