Skip to content

Commit 7141d1c

Browse files
committed
address review comments
- Refactored ptrace_runner to hide the class used
1 parent 82e2617 commit 7141d1c

File tree

9 files changed

+273
-193
lines changed

9 files changed

+273
-193
lines changed

projects/rocprofiler-sdk/source/lib/rocprofiler-sdk-rocattach/CMakeLists.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@ endforeach()
3333

3434
target_sources(
3535
rocprofiler-sdk-rocattach-shared-library
36-
PRIVATE rocattach.cpp auxv.cpp ptrace_runner.cpp ptrace_session.cpp symbol_lookup.cpp
37-
common/ptrace.cpp)
36+
PRIVATE rocattach.cpp auxv.cpp ptrace_runner.cpp ptrace_session.cpp symbol_lookup.cpp)
37+
add_subdirectory(common)
38+
3839
target_include_directories(
3940
rocprofiler-sdk-rocattach-shared-library
4041
INTERFACE $<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/source/include>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# MIT License
2+
#
3+
# Copyright (c) 2026 Advanced Micro Devices, Inc. All rights reserved.
4+
#
5+
# Permission is hereby granted, free of charge, to any person obtaining a copy
6+
# of this software and associated documentation files (the "Software"), to deal
7+
# in the Software without restriction, including without limitation the rights
8+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
# copies of the Software, and to permit persons to whom the Software is
10+
# furnished to do so, subject to the following conditions:
11+
#
12+
# The above copyright notice and this permission notice shall be included in all
13+
# copies or substantial portions of the Software.
14+
#
15+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
# SOFTWARE.
22+
23+
target_sources(rocprofiler-sdk-rocattach-shared-library PRIVATE ptrace.cpp)

projects/rocprofiler-sdk/source/lib/rocprofiler-sdk-rocattach/common/wait_for_atomic.hpp

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,52 +24,59 @@
2424

2525
#include <atomic>
2626
#include <chrono>
27+
#include <functional>
2728
#include <thread>
29+
#include <type_traits>
2830

2931
namespace rocprofiler
3032
{
3133
namespace rocattach
3234
{
33-
template <typename T>
35+
// Blocks until predicate(flag) == true or timeout_ms milliseconds have elapsed.
36+
// Returns true if predicate(flag) was true
37+
// Returns false if timeout occurred
38+
template <typename Tp, typename PredicateT>
3439
bool
35-
wait_for(std::atomic<T>& flag, T condition, size_t timeout_ms, bool equal)
40+
wait_for(std::atomic<Tp>& flag, size_t timeout_ms, PredicateT&& predicate)
3641
{
37-
auto cond_check = [&]() {
38-
if(equal) return flag.load() == condition;
39-
return flag.load() != condition;
40-
};
42+
static_assert(std::is_invocable<PredicateT, std::atomic<Tp>&>::value, "Invalid predicate");
43+
using predicate_return_type = typename std::invoke_result<PredicateT, std::atomic<Tp>&>::type;
44+
static_assert(std::is_same<predicate_return_type, bool>::value,
45+
"Predicate must return boolean");
46+
4147
auto start_time = std::chrono::steady_clock::now();
4248
auto timeout_duration = std::chrono::milliseconds(timeout_ms);
4349
auto end_time = start_time + timeout_duration;
4450
while(std::chrono::steady_clock::now() < end_time)
4551
{
46-
if(cond_check())
52+
if(std::invoke(std::forward<PredicateT>(predicate), std::forward<std::atomic<Tp>&>(flag)))
4753
{
4854
return true;
4955
}
5056
std::this_thread::yield();
5157
}
5258
// Last chance check in case we were scheduled after timeout
53-
return cond_check();
59+
return std::invoke(std::forward<PredicateT>(predicate), std::forward<std::atomic<Tp>&>(flag));
5460
}
55-
// Blocks until flag is NOT equal to condition or timeout_ms milliseconds have elapsed.
61+
// Blocks until flag is NOT equal to value or timeout_ms milliseconds have elapsed.
5662
// Returns true if the flag is not equal
5763
// Returns false if timeout occurred
5864
template <typename T>
5965
bool
60-
wait_for_ne(std::atomic<T>& flag, T condition, size_t timeout_ms)
66+
wait_for_ne(std::atomic<T>& flag, T value, size_t timeout_ms)
6167
{
62-
return wait_for(flag, condition, timeout_ms, false);
68+
auto predicate = [value](std::atomic<T>& a) { return a.load() != value; };
69+
return wait_for(flag, timeout_ms, predicate);
6370
}
64-
// Blocks until flag is equal to condition or timeout_ms milliseconds have elapsed.
71+
// Blocks until flag is equal to value or timeout_ms milliseconds have elapsed.
6572
// Returns true if the flag is equal
6673
// Returns false if timeout occurred
6774
template <typename T>
6875
bool
69-
wait_for_eq(std::atomic<T>& flag, T condition, size_t timeout_ms)
76+
wait_for_eq(std::atomic<T>& flag, T value, size_t timeout_ms)
7077
{
71-
return wait_for(flag, condition, timeout_ms, true);
78+
auto predicate = [value](std::atomic<T>& a) { return a.load() == value; };
79+
return wait_for(flag, timeout_ms, predicate);
7280
}
73-
7481
} // namespace rocattach
7582
} // namespace rocprofiler

0 commit comments

Comments
 (0)