Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ FdCacheInterface::~FdCacheInterface() {

template <typename T>
ze_result_t FsAccessInterface::readValue(const std::string file, T &val) {
auto lock = this->obtainMutex();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Intention of having lock here is not for handling race conditions for sysfs file reads, But to ensure correct operations with FdCacheInterface (Which ensures quicker reads by avoiding redundant file open and file close operations when multiple reads are requested). Adding lock here ensures fd is not closed before read is completed in multi thread environment.

std::unique_lock<std::mutex> lockObj;
if (needLock) {
lockObj = this->obtainMutex();
}

std::string readVal(64, '\0');
int fd = pFdCacheInterface->getFd(file);
Expand All @@ -84,7 +87,7 @@ ze_result_t FsAccessInterface::readValue(const std::string file, T &val) {
}

// Generic Filesystem Access
FsAccessInterface::FsAccessInterface() {
FsAccessInterface::FsAccessInterface(bool needLock) : needLock(needLock) {
pFdCacheInterface = std::make_unique<FdCacheInterface>();
}

Expand Down Expand Up @@ -398,7 +401,7 @@ ::pid_t ProcFsAccessInterface::myProcessId() {
}

// Sysfs Access
SysFsAccessInterface::SysFsAccessInterface() = default;
SysFsAccessInterface::SysFsAccessInterface() : FsAccessInterface(false) {}
SysFsAccessInterface::~SysFsAccessInterface() = default;

const std::string SysFsAccessInterface::drmPath = "/sys/class/drm/";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,9 @@ class FsAccessInterface {
virtual bool directoryExists(const std::string path);

protected:
FsAccessInterface();
FsAccessInterface(bool needLock = true);
MOCKABLE_VIRTUAL std::unique_lock<std::mutex> obtainMutex();
bool needLock;

private:
template <typename T>
Expand Down