Skip to content

Commit 9c7f7fa

Browse files
committed
clean up park_section_no_miss test
1 parent 8c5ffb6 commit 9c7f7fa

File tree

1 file changed

+24
-26
lines changed

1 file changed

+24
-26
lines changed

tests/park_section_no_miss/park_section_no_miss.cpp

+24-26
Original file line numberDiff line numberDiff line change
@@ -7,44 +7,44 @@
77
#include <iostream>
88
#include <thread>
99

10-
static constexpr int NUM_ITERATIONS = 15; // total iterations; each has a scenario
11-
static constexpr auto MAX_BLOCK_MS = 5000; // 5s => generous for slow/VM systems
10+
static constexpr int NUM_ITERATIONS = 15; // total iterations; each has a scenario
11+
static constexpr auto MAX_BLOCK_MS = 5000; // 5s => generous for slow/VM systems
1212

13-
// Shared data for consumer/producer synchronization
14-
static std::mutex g_mutex;
13+
/// Shared data for consumer/producer synchronization
14+
static std::mutex g_mutex;
1515
static std::condition_variable g_cv;
1616

17-
// Let consumer tell producer "I just set the park bit"
17+
/// Let consumer tell producer "I just set the park bit"
1818
static bool g_consumerReady = false;
1919

20-
// Let producer tell consumer "I have woken you (or tried to wake you)"
20+
/// Let producer tell consumer "I have woken you (or tried to wake you)"
2121
static bool g_wakeDone = false;
2222

23-
// We’ll store test failures if we detect a lost wake
24-
static std::atomic<bool> g_testFailed{false};
23+
/// We’ll store test failures if we detect a lost wake
24+
static std::atomic g_testFailed{false};
2525

26-
int main()
27-
{
26+
int main() {
2827
// 1) Create our thread parking handle
29-
tpark_handle_t* handle = tparkCreateHandle();
28+
tpark_handle_t *handle = tparkCreateHandle();
3029

3130
// 2) Producer: wakes the consumer under different timing scenarios
3231
std::thread producer([&]() {
33-
for (int i = 0; i < NUM_ITERATIONS && !g_testFailed.load(); i++)
34-
{
32+
for (int i = 0; i < NUM_ITERATIONS && !g_testFailed.load(); i++) {
3533
// Acquire lock and wait until consumer sets the park bit
36-
std::unique_lock<std::mutex> lk(g_mutex);
34+
std::unique_lock lk(g_mutex);
3735
g_cv.wait(lk, [] { return g_consumerReady || g_testFailed.load(); });
3836
if (g_testFailed.load()) break;
3937

4038
// Decide how long to wait before calling wake
4139
// scenario 0 = immediate
4240
// scenario 1 = 100 ms
4341
// scenario 2 = 500 ms
44-
int scenario = i % 3;
45-
auto sleepMs = (scenario == 0) ? 0
46-
: (scenario == 1) ? 100
47-
: 500;
42+
const int scenario = i % 3;
43+
auto sleepMs = (scenario == 0)
44+
? 0
45+
: (scenario == 1)
46+
? 100
47+
: 500;
4848

4949
// Sleep some time to let the consumer possibly call parkWait
5050
lk.unlock();
@@ -63,16 +63,15 @@ int main()
6363

6464
// 3) Consumer: sets the park bit, signals producer, then calls parkWait
6565
std::thread consumer([&]() {
66-
for (int i = 0; i < NUM_ITERATIONS; i++)
67-
{
66+
for (int i = 0; i < NUM_ITERATIONS; i++) {
6867
// 3.1) "Begin Park": set bit=1
6968
{
70-
std::unique_lock<std::mutex> lk(g_mutex);
69+
std::unique_lock lk(g_mutex);
7170
tparkBeginPark(handle);
7271

7372
// Indicate "ready" so producer knows to do a wake
7473
g_consumerReady = true;
75-
g_wakeDone = false;
74+
g_wakeDone = false;
7675
g_cv.notify_one();
7776

7877
// Wait until producer has definitely done a wake attempt
@@ -86,11 +85,10 @@ int main()
8685
tparkWait(handle, /*unlocked=*/true);
8786
auto t2 = std::chrono::steady_clock::now();
8887

89-
auto msBlocked = std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1).count();
90-
if (msBlocked > MAX_BLOCK_MS)
91-
{
88+
if (const auto msBlocked = std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1).count();
89+
msBlocked > MAX_BLOCK_MS) {
9290
std::cerr << "[Iteration " << i << "] BLOCKED too long ("
93-
<< msBlocked << " ms). Potential lost wake or deadlock.\n";
91+
<< msBlocked << " ms). Potential lost wake or deadlock.\n";
9492
g_testFailed.store(true);
9593
return;
9694
}

0 commit comments

Comments
 (0)