| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | #include "fileLock.h" |
| | #include "NvInfer.h" |
| | #include <sstream> |
| | #include <stdexcept> |
| | #include <string> |
| |
|
| | namespace nvinfer1::utils |
| | { |
| |
|
| | FileLock::FileLock(ILogger& logger, std::string const& fileName) |
| | : mLogger(logger) |
| | , mFileName(fileName) |
| | { |
| | std::string lockFileName = mFileName + ".lock"; |
| | #ifdef _MSC_VER |
| | { |
| | std::stringstream ss; |
| | ss << "Trying to set exclusive file lock " << lockFileName << std::endl; |
| | mLogger.log(ILogger::Severity::kVERBOSE, ss.str().c_str()); |
| | } |
| | |
| | mHandle = CreateFileA(lockFileName.c_str(), GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, 0, NULL); |
| | if (mHandle == INVALID_HANDLE_VALUE) |
| | { |
| | throw std::runtime_error("Failed to lock " + lockFileName + "!"); |
| | } |
| | #elif defined(__QNX__) |
| | |
| | #else |
| | mHandle = fopen(lockFileName.c_str(), "wb+"); |
| | if (mHandle == nullptr) |
| | { |
| | throw std::runtime_error("Cannot open " + lockFileName + "!"); |
| | } |
| | { |
| | std::stringstream ss; |
| | ss << "Trying to set exclusive file lock " << lockFileName << std::endl; |
| | mLogger.log(ILogger::Severity::kVERBOSE, ss.str().c_str()); |
| | } |
| | mDescriptor = fileno(mHandle); |
| | auto ret = lockf(mDescriptor, F_LOCK, 0); |
| | if (ret != 0) |
| | { |
| | mDescriptor = -1; |
| | fclose(mHandle); |
| | throw std::runtime_error("Failed to lock " + lockFileName + "!"); |
| | } |
| | #endif |
| | } |
| |
|
| | FileLock::~FileLock() |
| | { |
| | std::string lockFileName = mFileName + ".lock"; |
| | #ifdef _MSC_VER |
| | if (mHandle != INVALID_HANDLE_VALUE) |
| | { |
| | CloseHandle(mHandle); |
| | } |
| | #elif defined(__QNX__) |
| | |
| | #else |
| | if (mDescriptor != -1) |
| | { |
| | auto ret = lockf(mDescriptor, F_ULOCK, 0); |
| | if (mHandle != nullptr) |
| | { |
| | fclose(mHandle); |
| | } |
| | if (ret != 0) |
| | { |
| | std::stringstream ss; |
| | ss << "Failed to unlock " << lockFileName << ", please remove " << lockFileName << ".lock manually!" |
| | << std::endl; |
| | mLogger.log(ILogger::Severity::kVERBOSE, ss.str().c_str()); |
| | } |
| | } |
| | #endif |
| | } |
| |
|
| | } |
| |
|