CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
95 lines
2.7 KiB
C++
95 lines
2.7 KiB
C++
// Copyright (c) Advanced Micro Devices, Inc., or its affiliates.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
#include <miopen/errors.hpp>
|
|
#include <miopen/lock_file.hpp>
|
|
#include <miopen/logger.hpp>
|
|
#include <miopen/md5.hpp>
|
|
#include <miopen/stringutils.hpp>
|
|
|
|
namespace miopen {
|
|
|
|
inline void LogFsError(const fs::filesystem_error& ex, const std::string_view from)
|
|
{
|
|
// clang-format off
|
|
MIOPEN_LOG_E_FROM(from, "File system operation error in LockFile. "
|
|
"Error code: " << ex.code() << ". "
|
|
"Description: '" << ex.what() << "'");
|
|
// clang-format on
|
|
}
|
|
|
|
fs::path LockFilePath(const fs::path& filename_)
|
|
{
|
|
try
|
|
{
|
|
const auto directory = fs::temp_directory_path() / "miopen-lockfiles";
|
|
|
|
if(!fs::exists(directory))
|
|
{
|
|
fs::create_directories(directory);
|
|
fs::permissions(directory, fs::perms::all);
|
|
}
|
|
const auto hash = md5(filename_.parent_path().string());
|
|
const auto file = directory / (hash + "_" + filename_.filename() + ".lock");
|
|
|
|
return file;
|
|
}
|
|
catch(const fs::filesystem_error& ex)
|
|
{
|
|
LogFsError(ex, MIOPEN_GET_FN_NAME);
|
|
throw;
|
|
}
|
|
}
|
|
|
|
LockFile::LockFile(const fs::path& path_, PassKey) : path(path_)
|
|
{
|
|
try
|
|
{
|
|
if(!fs::exists(path))
|
|
{
|
|
if(!std::ofstream{path})
|
|
MIOPEN_THROW("Error creating file <" + path + "> for locking.");
|
|
fs::permissions(path, fs::perms::all);
|
|
}
|
|
|
|
flock = decltype(flock)(path.string());
|
|
}
|
|
catch(const fs::filesystem_error& ex)
|
|
{
|
|
LogFsError(ex, MIOPEN_GET_FN_NAME);
|
|
throw;
|
|
}
|
|
catch(const std::exception& ex)
|
|
{
|
|
LogFlockError(ex, "lock initialization", MIOPEN_GET_FN_NAME);
|
|
throw;
|
|
}
|
|
}
|
|
|
|
LockFile& LockFile::Get(const fs::path& file)
|
|
{
|
|
#ifdef _WIN32
|
|
// The character ':' is reserved on Windows and cannot be used for constructing
|
|
// a path except when it follows a drive letter.
|
|
fs::path path{ReplaceString(file.string(), ":memory:", "memory_")};
|
|
#else
|
|
#define path file
|
|
#endif
|
|
// NOLINTNEXTLINE (cppcoreguidelines-avoid-non-const-global-variables)
|
|
static std::mutex mutex;
|
|
std::lock_guard<std::mutex> lock(mutex);
|
|
|
|
{ // To guarantee that construction won't be called if not required.
|
|
auto found = LockFiles().find(path);
|
|
|
|
if(found != LockFiles().end())
|
|
return found->second;
|
|
}
|
|
|
|
auto emplaced = LockFiles().emplace(std::piecewise_construct,
|
|
std::forward_as_tuple(path),
|
|
std::forward_as_tuple(path, PassKey{}));
|
|
return emplaced.first->second;
|
|
}
|
|
} // namespace miopen
|