Update to use rocprofiler-sdk (#55)
- Renames the CMake option "ROCPROFSYS_USE_HIP" to "ROCPROFSYS_USE_ROCM"
- Remove the "ROCPROFSYS_USE_ROCM_SMI option. Controlled with the "ROCPROFSYS_USE_ROCM" option, instead.
- Runtime configuration can still toggle ROCPROFSYS_USE_ROCM_SMI to disable the sampling.
- Rename ROCPROFSYS_HIP_VERSION macro to ROCPROFSYS_ROCM_VERSION and remove blocks for `ROCPROFSYS_ROCM_VERSION < 60000`
- Remove ROCPROFSYS_USE_ROCTRACER and ROCPROFSYS_USE_ROCPROFILER
- Update test cases
- Update docker files and workflows to install cmake 3.21, which is required for the rocprofiler-sdk findPackage script.
- Removed rocm-6.2 from workflows due to a rocprofiler-sdk API change.
[ROCm/rocprofiler-systems commit: 88aa2d3cbe]
このコミットが含まれているのは:
@@ -19,7 +19,9 @@ target_sources(
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/environment.hpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/invoke.hpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/join.hpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/setup.hpp)
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/setup.hpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/static_object.hpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/synchronized.hpp)
|
||||
|
||||
get_filename_component(COMMON_SOURCE_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}" DIRECTORY)
|
||||
get_filename_component(COMMON_BINARY_INCLUDE_DIR "${CMAKE_CURRENT_BINARY_DIR}" DIRECTORY)
|
||||
|
||||
@@ -42,10 +42,10 @@
|
||||
#define ROCPROFSYS_COMPILER_STRING ROCPROFSYS_COMPILER_ID " v" ROCPROFSYS_COMPILER_VERSION
|
||||
|
||||
#define ROCPROFSYS_DEFAULT_ROCM_PATH "@ROCmVersion_DIR@"
|
||||
#define ROCPROFSYS_HIP_VERSION_STRING "@ROCPROFSYS_HIP_VERSION@"
|
||||
#define ROCPROFSYS_HIP_VERSION_MAJOR @ROCPROFSYS_HIP_VERSION_MAJOR@
|
||||
#define ROCPROFSYS_HIP_VERSION_MINOR @ROCPROFSYS_HIP_VERSION_MINOR@
|
||||
#define ROCPROFSYS_HIP_VERSION_PATCH @ROCPROFSYS_HIP_VERSION_PATCH@
|
||||
#define ROCPROFSYS_ROCM_VERSION_STRING "@ROCPROFSYS_ROCM_VERSION@"
|
||||
#define ROCPROFSYS_ROCM_VERSION_MAJOR @ROCPROFSYS_ROCM_VERSION_MAJOR@
|
||||
#define ROCPROFSYS_ROCM_VERSION_MINOR @ROCPROFSYS_ROCM_VERSION_MINOR@
|
||||
#define ROCPROFSYS_ROCM_VERSION_PATCH @ROCPROFSYS_ROCM_VERSION_PATCH@
|
||||
|
||||
// these can be set via defining the variable in CMake, e.g.:
|
||||
// cmake -D ROCPROFSYS_CACHELINE_SIZE=N /path/to/source
|
||||
@@ -63,15 +63,15 @@
|
||||
((10000 * ROCPROFSYS_VERSION_MAJOR) + (100 * ROCPROFSYS_VERSION_MINOR) + \
|
||||
ROCPROFSYS_VERSION_PATCH)
|
||||
|
||||
#define ROCPROFSYS_HIP_VERSION \
|
||||
((10000 * ROCPROFSYS_HIP_VERSION_MAJOR) + (100 * ROCPROFSYS_HIP_VERSION_MINOR) + \
|
||||
ROCPROFSYS_HIP_VERSION_PATCH)
|
||||
#define ROCPROFSYS_ROCM_VERSION \
|
||||
((10000 * ROCPROFSYS_ROCM_VERSION_MAJOR) + (100 * ROCPROFSYS_ROCM_VERSION_MINOR) + \
|
||||
ROCPROFSYS_ROCM_VERSION_PATCH)
|
||||
|
||||
#if ROCPROFSYS_HIP_VERSION_MAJOR > 0
|
||||
# define ROCPROFSYS_HIP_VERSION_COMPAT_STRING \
|
||||
"v@ROCPROFSYS_HIP_VERSION_MAJOR@.@ROCPROFSYS_HIP_VERSION_MINOR@.x"
|
||||
#if ROCPROFSYS_ROCM_VERSION_MAJOR > 0
|
||||
# define ROCPROFSYS_ROCM_VERSION_COMPAT_STRING \
|
||||
"v@ROCPROFSYS_ROCM_VERSION_MAJOR@.@ROCPROFSYS_ROCM_VERSION_MINOR@.x"
|
||||
#else
|
||||
# define ROCPROFSYS_HIP_VERSION_COMPAT_STRING ""
|
||||
# define ROCPROFSYS_ROCM_VERSION_COMPAT_STRING ""
|
||||
#endif
|
||||
|
||||
// this should be passed to argparse::argument_parser::enable_version
|
||||
@@ -83,7 +83,7 @@
|
||||
{ \
|
||||
{ "", ROCPROFSYS_LIBRARY_ARCH }, { "compiler", ROCPROFSYS_COMPILER_STRING }, \
|
||||
{ \
|
||||
"rocm", ROCPROFSYS_HIP_VERSION_COMPAT_STRING \
|
||||
"rocm", ROCPROFSYS_ROCM_VERSION_COMPAT_STRING \
|
||||
} \
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -109,148 +109,6 @@ get_environ(int _verbose, std::string _search_paths = {},
|
||||
_omnilib = common::path::find_path(_omnilib, _verbose, _search_paths);
|
||||
_omnilib_dl = common::path::find_path(_omnilib_dl, _verbose, _search_paths);
|
||||
|
||||
#if defined(ROCPROFSYS_USE_ROCTRACER) && ROCPROFSYS_USE_ROCTRACER > 0
|
||||
_data.emplace_back(env_config{ "HSA_TOOLS_LIB", _omnilib.c_str(), 0 });
|
||||
#endif
|
||||
|
||||
#if defined(ROCPROFSYS_USE_ROCPROFILER) && ROCPROFSYS_USE_ROCPROFILER > 0
|
||||
# if ROCPROFSYS_HIP_VERSION >= 50200
|
||||
# define ROCPROFILER_METRICS_DIR "lib/rocprofiler"
|
||||
# else
|
||||
# define ROCPROFILER_METRICS_DIR "rocprofiler/lib"
|
||||
# endif
|
||||
# if ROCPROFSYS_HIP_VERSION <= 50500
|
||||
# define ROCPROFILER_LIBNAME "librocprofiler64.so"
|
||||
# else
|
||||
# define ROCPROFILER_LIBNAME "librocprofiler64.so.1"
|
||||
# endif
|
||||
|
||||
_data.emplace_back(env_config{ "HSA_TOOLS_LIB", _omnilib.c_str(), 0 });
|
||||
_data.emplace_back(env_config{ "ROCP_TOOL_LIB", _omnilib.c_str(), 0 });
|
||||
_data.emplace_back(env_config{ "ROCPROFILER_LOG", "1", 0 });
|
||||
_data.emplace_back(env_config{ "ROCP_HSA_INTERCEPT", "1", 0 });
|
||||
_data.emplace_back(env_config{ "HSA_TOOLS_REPORT_LOAD_FAILURE", "1", 0 });
|
||||
|
||||
auto _possible_rocp_metrics = std::vector<std::string>{};
|
||||
auto _possible_rocprof_libs = std::vector<std::string>{};
|
||||
for(const auto* itr : { "ROCPROFSYS_ROCM_PATH", "ROCM_PATH" })
|
||||
{
|
||||
if(getenv(itr))
|
||||
{
|
||||
_possible_rocp_metrics.emplace_back(
|
||||
common::join('/', getenv(itr), "lib/rocprofiler"));
|
||||
_possible_rocprof_libs.emplace_back(
|
||||
common::join('/', getenv(itr), "lib/rocprofiler", ROCPROFILER_LIBNAME));
|
||||
_possible_rocp_metrics.emplace_back(
|
||||
common::join('/', getenv(itr), "rocprofiler/lib"));
|
||||
_possible_rocprof_libs.emplace_back(
|
||||
common::join('/', getenv(itr), "rocprofiler/lib", ROCPROFILER_LIBNAME));
|
||||
}
|
||||
}
|
||||
|
||||
// default path
|
||||
_possible_rocp_metrics.emplace_back(
|
||||
common::join('/', ROCPROFSYS_DEFAULT_ROCM_PATH, "lib/rocprofiler"));
|
||||
_possible_rocp_metrics.emplace_back(
|
||||
common::join('/', ROCPROFSYS_DEFAULT_ROCM_PATH, "rocprofiler/lib"));
|
||||
|
||||
auto _realpath_and_unique = [](const auto& _inp_v) {
|
||||
auto _out_v = decltype(_inp_v){};
|
||||
for(auto& itr : _inp_v)
|
||||
{
|
||||
if(path::exists(itr)) _out_v.emplace_back(path::realpath(itr));
|
||||
}
|
||||
|
||||
_out_v.erase(std::unique(_out_v.begin(), _out_v.end()), _out_v.end());
|
||||
return _out_v;
|
||||
};
|
||||
|
||||
_possible_rocprof_libs = _realpath_and_unique(_possible_rocprof_libs);
|
||||
|
||||
for(const auto& itr : _possible_rocprof_libs)
|
||||
{
|
||||
if(path::exists(itr))
|
||||
{
|
||||
_data.emplace_back(
|
||||
env_config{ "ROCPROFSYS_ROCPROFILER_LIBRARY", itr.c_str(), 0 });
|
||||
_possible_rocp_metrics.emplace(
|
||||
_possible_rocp_metrics.begin(),
|
||||
common::join('/', path::dirname(itr), "../../lib/rocprofiler"));
|
||||
_possible_rocp_metrics.emplace(_possible_rocp_metrics.begin(),
|
||||
common::join('/', path::dirname(itr)));
|
||||
}
|
||||
}
|
||||
|
||||
_possible_rocp_metrics = _realpath_and_unique(_possible_rocp_metrics);
|
||||
|
||||
auto _env_rocp_metrics = get_env("ROCP_METRICS", "");
|
||||
if(!_env_rocp_metrics.empty())
|
||||
{
|
||||
if(!path::exists(_env_rocp_metrics))
|
||||
throw std::runtime_error(join("", "Error! ROCP_METRICS file \"",
|
||||
_env_rocp_metrics, "\" does not exist"));
|
||||
_possible_rocp_metrics.clear();
|
||||
_possible_rocp_metrics.emplace_back(
|
||||
common::join('/', path::dirname(_env_rocp_metrics)));
|
||||
}
|
||||
|
||||
auto _found_rocp_metrics = (!_env_rocp_metrics.empty())
|
||||
? get_env("ROCPROFSYS_ROCP_METRICS_FORCE_VALID", false)
|
||||
: false;
|
||||
|
||||
if(!_found_rocp_metrics)
|
||||
{
|
||||
for(const auto& itr : _possible_rocp_metrics)
|
||||
{
|
||||
auto _metrics_path = join('/', itr, "metrics.xml");
|
||||
if(path::exists(itr) && path::exists(_metrics_path) &&
|
||||
path::exists(join('/', itr, "gfx_metrics.xml")))
|
||||
{
|
||||
_found_rocp_metrics = true;
|
||||
_data.emplace_back(
|
||||
env_config{ "ROCP_METRICS", _metrics_path.c_str(), 0 });
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// handle error
|
||||
if(!_found_rocp_metrics)
|
||||
{
|
||||
auto _msg = std::stringstream{};
|
||||
_msg << std::boolalpha;
|
||||
if(!_env_rocp_metrics.empty())
|
||||
{
|
||||
auto _env_rocp_metrics_dir = path::dirname(_env_rocp_metrics);
|
||||
auto _rocp_metrics_xml = join('/', _env_rocp_metrics_dir, "metrics.xml");
|
||||
auto _rocp_gfx_metrics_xml =
|
||||
join('/', _env_rocp_metrics_dir, "gfx_metrics.xml");
|
||||
_msg << "Error! ROCP_METRICS=\"" << _env_rocp_metrics
|
||||
<< "\" in the environment but the directory (" << _env_rocp_metrics_dir
|
||||
<< ") does not contain "
|
||||
"metrics.xml (found: "
|
||||
<< path::exists(_rocp_metrics_xml) << ") and/or gfx_metrics.xml (found: "
|
||||
<< path::exists(_rocp_gfx_metrics_xml)
|
||||
<< "). To ignore this error, set "
|
||||
"ROCPROFSYS_ROCP_METRICS_FORCE_VALID=true in the environment";
|
||||
}
|
||||
else
|
||||
{
|
||||
_msg
|
||||
<< "Error! ROCP_METRICS not set in environment and rocprof-sys could not "
|
||||
"find a suitable path. Please set ROCP_METRICS=/path/to/metrics.xml "
|
||||
"in the environment. This file is typically located in the same "
|
||||
"folder as the librocprofiler64.so library.\nAdditional note: "
|
||||
"metrics.xml typically contains:\n\t#include "
|
||||
"\"gfx_metrics.xml\"\nMake sure the provided path also contains this "
|
||||
"file.\nExample:\n\texport ROCP_METRICS="
|
||||
<< ROCPROFSYS_DEFAULT_ROCM_PATH << "/" << ROCPROFILER_METRICS_DIR
|
||||
<< "/metrics.xml\n";
|
||||
}
|
||||
throw std::runtime_error(_msg.str());
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(ROCPROFSYS_USE_OMPT) && ROCPROFSYS_USE_OMPT > 0
|
||||
if(get_env("ROCPROFSYS_USE_OMPT", true))
|
||||
{
|
||||
|
||||
@@ -0,0 +1,207 @@
|
||||
// MIT License
|
||||
//
|
||||
// Copyright (c) 2024 Advanced Micro Devices, Inc. All Rights Reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <array>
|
||||
#include <cstddef>
|
||||
#include <deque>
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
#include <mutex>
|
||||
#include <stack>
|
||||
|
||||
namespace rocprofsys
|
||||
{
|
||||
inline namespace common
|
||||
{
|
||||
using static_dtor_func_t = void (*)();
|
||||
|
||||
void
|
||||
destroy_static_objects();
|
||||
|
||||
void
|
||||
register_static_dtor(static_dtor_func_t&&);
|
||||
|
||||
namespace
|
||||
{
|
||||
struct anonymous
|
||||
{};
|
||||
} // namespace
|
||||
|
||||
struct do_not_destroy
|
||||
{};
|
||||
|
||||
template <typename Tp>
|
||||
constexpr size_t
|
||||
static_buffer_size()
|
||||
{
|
||||
return sizeof(Tp);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This struct is used to create static singleton objects which have the properties
|
||||
* of a heap-allocated static object without a memory leak.
|
||||
*
|
||||
* @tparam Tp Data type of singleton
|
||||
* @tparam ContextT Use to differentiate singletons in different translation units (if
|
||||
* using default parameter) or ensure the singleton can be accessed in different
|
||||
* translation units (not recommended) as long as this type is not in an anonymous
|
||||
* namespace
|
||||
*
|
||||
* This template works by creating a buffer of at least `sizeof(Tp)` bytes in the binary
|
||||
* and does a placement new into that buffer. The object created is NOT heap allocated,
|
||||
* the address of the object is an address in between the library load address and the
|
||||
* load address + size of library.
|
||||
*/
|
||||
template <typename Tp, typename ContextT = anonymous>
|
||||
struct static_object
|
||||
{
|
||||
static_object() = delete;
|
||||
~static_object() = delete;
|
||||
static_object(const static_object&) = delete;
|
||||
static_object(static_object&&) noexcept = delete;
|
||||
static_object& operator=(const static_object&) = delete;
|
||||
static_object& operator=(static_object&&) noexcept = delete;
|
||||
|
||||
template <typename... Args>
|
||||
static Tp*& construct(Args&&... args);
|
||||
|
||||
template <typename... Args>
|
||||
static Tp*& construct(do_not_destroy&&, Args&&... args);
|
||||
|
||||
static Tp* get() { return m_object; }
|
||||
|
||||
static constexpr bool is_trivial_standard_layout();
|
||||
|
||||
private:
|
||||
static Tp* m_object;
|
||||
static std::array<std::byte, static_buffer_size<Tp>()> m_buffer;
|
||||
};
|
||||
|
||||
template <typename Tp, typename ContextT>
|
||||
Tp* static_object<Tp, ContextT>::m_object = nullptr;
|
||||
|
||||
template <typename Tp, typename ContextT>
|
||||
std::array<std::byte, static_buffer_size<Tp>()>
|
||||
static_object<Tp, ContextT>::m_buffer = {};
|
||||
|
||||
template <typename Tp, typename ContextT>
|
||||
constexpr bool
|
||||
static_object<Tp, ContextT>::is_trivial_standard_layout()
|
||||
{
|
||||
return (std::is_standard_layout<Tp>::value && std::is_trivial<Tp>::value);
|
||||
}
|
||||
|
||||
template <typename Tp, typename ContextT>
|
||||
template <typename... Args>
|
||||
Tp*&
|
||||
static_object<Tp, ContextT>::construct(Args&&... args)
|
||||
{
|
||||
if constexpr(!is_trivial_standard_layout())
|
||||
{
|
||||
static auto _once = std::once_flag{};
|
||||
std::call_once(_once, []() {
|
||||
register_static_dtor([]() {
|
||||
if(static_object<Tp, ContextT>::m_object)
|
||||
{
|
||||
static_object<Tp, ContextT>::m_object->~Tp();
|
||||
static_object<Tp, ContextT>::m_object = nullptr;
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
if(m_object)
|
||||
{
|
||||
std::cerr
|
||||
<< "reconstructing static object. Use get() function to retrieve pointer"
|
||||
<< std::endl;
|
||||
abort();
|
||||
}
|
||||
|
||||
m_object = new(m_buffer.data()) Tp{ std::forward<Args>(args)... };
|
||||
return m_object;
|
||||
}
|
||||
|
||||
template <typename Tp, typename ContextT>
|
||||
template <typename... Args>
|
||||
Tp*&
|
||||
static_object<Tp, ContextT>::construct(do_not_destroy&&, Args&&... args)
|
||||
{
|
||||
if(m_object)
|
||||
{
|
||||
std::cerr
|
||||
<< "reconstructing static object. Use get() function to retrieve pointer"
|
||||
<< std::endl;
|
||||
abort();
|
||||
}
|
||||
|
||||
m_object = new(m_buffer.data()) Tp{ std::forward<Args>(args)... };
|
||||
return m_object;
|
||||
}
|
||||
|
||||
namespace
|
||||
{
|
||||
inline auto*&
|
||||
get_static_object_stack()
|
||||
{
|
||||
static auto* _v = new std::stack<static_dtor_func_t>{};
|
||||
return _v;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
inline void
|
||||
destroy_static_objects()
|
||||
{
|
||||
static auto _sync = std::mutex{};
|
||||
auto _lk = std::unique_lock<std::mutex>{ _sync };
|
||||
|
||||
auto*& _stack = get_static_object_stack();
|
||||
if(_stack)
|
||||
{
|
||||
while(!_stack->empty())
|
||||
{
|
||||
auto& itr = _stack->top();
|
||||
if(itr) itr();
|
||||
_stack->pop();
|
||||
}
|
||||
|
||||
delete _stack;
|
||||
_stack = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
inline void
|
||||
register_static_dtor(static_dtor_func_t&& _func)
|
||||
{
|
||||
static auto _sync = std::mutex{};
|
||||
auto _lk = std::unique_lock<std::mutex>{ _sync };
|
||||
|
||||
auto*& _stack = get_static_object_stack();
|
||||
if(_stack)
|
||||
{
|
||||
_stack->push(_func);
|
||||
}
|
||||
}
|
||||
} // namespace common
|
||||
} // namespace rocprofsys
|
||||
@@ -0,0 +1,167 @@
|
||||
// MIT License
|
||||
//
|
||||
// Copyright (c) 2024 Advanced Micro Devices, Inc. All Rights Reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
#include <functional>
|
||||
#include <mutex>
|
||||
#include <shared_mutex>
|
||||
#include <type_traits>
|
||||
|
||||
namespace rocprofsys
|
||||
{
|
||||
inline namespace common
|
||||
{
|
||||
/**
|
||||
* Sychronized is a wrapper that adds lock based write/read
|
||||
* protection around a datatype. The protected data is accessed
|
||||
* only by rlock/wlock. rlock(lambda) gets a reader lock of the
|
||||
* protected value, passing the protected value to the lambda as a
|
||||
* const. wlock(lambda) gets a writer lock on the protective value
|
||||
* and does the same. The reason for this class is to make it less
|
||||
* error prone to access shared data and more obvious when a lock
|
||||
* is being held.
|
||||
*
|
||||
* Example usage:
|
||||
*
|
||||
* synchronized<int> x(9);
|
||||
* x.rlock([](const auto& data){
|
||||
* // data = 9
|
||||
* });
|
||||
*
|
||||
* x.wlock([](auto& data){
|
||||
* // set data to new value
|
||||
* });
|
||||
*/
|
||||
template <typename LockedType, bool IsMappedTypeV = false>
|
||||
class synchronized
|
||||
{
|
||||
public:
|
||||
using value_type = LockedType;
|
||||
using this_type = synchronized<value_type, IsMappedTypeV>;
|
||||
|
||||
synchronized() = default;
|
||||
~synchronized() = default;
|
||||
|
||||
explicit synchronized(value_type&& data)
|
||||
: m_data{ std::move(data) }
|
||||
{}
|
||||
|
||||
synchronized(synchronized&& data) noexcept = default;
|
||||
synchronized& operator=(synchronized&& data) noexcept = default;
|
||||
|
||||
// Do not allow this data structure to be copied, std::move only.
|
||||
synchronized(const synchronized&) = delete;
|
||||
|
||||
template <typename FuncT, typename... Args>
|
||||
decltype(auto) rlock(FuncT&& lambda, Args&&... args) const;
|
||||
|
||||
template <typename FuncT, typename... Args>
|
||||
decltype(auto) wlock(FuncT&& lambda, Args&&... args);
|
||||
|
||||
// This overload to wlock allows a synchronized map whose keys map to synchronized
|
||||
// data to use a read lock on the key data and then a write lock on the mapped data.
|
||||
template <typename FuncT, typename... Args, bool EnableForMappedType = IsMappedTypeV,
|
||||
std::enable_if_t<EnableForMappedType, int> = 0>
|
||||
decltype(auto) wlock(FuncT&& lambda, Args&&... args) const;
|
||||
|
||||
// Upgradable lock. If read returns false, write will be called with a unique_lock.
|
||||
// Essentially a helper function that does .rlock() followed by .wlock().
|
||||
template <typename ReadFuncT, typename WriteFuncT, typename... Args>
|
||||
bool ulock(ReadFuncT&& read, WriteFuncT&& write, Args&&... args);
|
||||
|
||||
private:
|
||||
mutable std::shared_mutex m_mutex = {};
|
||||
value_type m_data = {};
|
||||
};
|
||||
|
||||
//
|
||||
// member definitions
|
||||
//
|
||||
template <typename LockedType, bool IsMappedTypeV>
|
||||
template <typename FuncT, typename... Args>
|
||||
decltype(auto)
|
||||
synchronized<LockedType, IsMappedTypeV>::rlock(FuncT&& lambda, Args&&... args) const
|
||||
{
|
||||
static_assert(std::is_invocable<FuncT, const value_type&, Args...>::value,
|
||||
"function must accept const reference to locked type");
|
||||
|
||||
auto lock = std::shared_lock{ m_mutex };
|
||||
return std::forward<FuncT>(lambda)(m_data, std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
template <typename LockedType, bool IsMappedTypeV>
|
||||
template <typename FuncT, typename... Args>
|
||||
decltype(auto)
|
||||
synchronized<LockedType, IsMappedTypeV>::wlock(FuncT&& lambda, Args&&... args)
|
||||
{
|
||||
static_assert(std::is_invocable<FuncT, value_type&, Args...>::value,
|
||||
"function must accept reference to locked type");
|
||||
|
||||
auto lock = std::unique_lock{ m_mutex };
|
||||
return std::forward<FuncT>(lambda)(m_data, std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
// This overload to wlock allows a synchronized map whose keys map to synchronized data to
|
||||
// use a read lock on the key data and then a write lock on the mapped data.
|
||||
template <typename LockedType, bool IsMappedTypeV>
|
||||
template <typename FuncT, typename... Args, bool EnableForMappedType,
|
||||
std::enable_if_t<EnableForMappedType, int>>
|
||||
decltype(auto)
|
||||
synchronized<LockedType, IsMappedTypeV>::wlock(FuncT&& lambda, Args&&... args) const
|
||||
{
|
||||
return const_cast<this_type*>(this)->wlock(std::forward<FuncT>(lambda),
|
||||
std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
// Upgradable lock. If read returns false, write will be called with a unique_lock.
|
||||
// Essentially a helper function that does .rlock() followed by .wlock().
|
||||
template <typename LockedType, bool IsMappedTypeV>
|
||||
template <typename ReadFuncT, typename WriteFuncT, typename... Args>
|
||||
bool
|
||||
synchronized<LockedType, IsMappedTypeV>::ulock(ReadFuncT&& read, WriteFuncT&& write,
|
||||
Args&&... args)
|
||||
{
|
||||
static_assert(std::is_invocable<ReadFuncT, const value_type&, Args...>::value,
|
||||
"read function must accept const reference to locked type");
|
||||
static_assert(std::is_invocable<WriteFuncT, value_type&, Args...>::value,
|
||||
"write function must accept reference to locked type");
|
||||
|
||||
using read_return_type = std::invoke_result_t<ReadFuncT, const value_type&, Args...>;
|
||||
using write_return_type = std::invoke_result_t<WriteFuncT, value_type&, Args...>;
|
||||
|
||||
static_assert(std::is_same<read_return_type, write_return_type>::value,
|
||||
"read and write functions must return same type");
|
||||
static_assert(std::is_same<read_return_type, bool>::value,
|
||||
"read/write functions must return bool");
|
||||
|
||||
{
|
||||
auto lock = std::shared_lock{ m_mutex };
|
||||
if(read(m_data, std::forward<Args>(args)...)) return true;
|
||||
}
|
||||
|
||||
auto lock = std::unique_lock{ m_mutex };
|
||||
return write(m_data, std::forward<Args>(args)...);
|
||||
}
|
||||
} // namespace common
|
||||
} // namespace rocprofsys
|
||||
新しいイシューから参照
ユーザーをブロックする