Skip to content

Commit 57666f4

Browse files
committed
Repeat the terminate request of the thread_rescheduling test until the thread ends
set_thread_state carries out a request for a running thread later, in a helper task nothing waits for. Such a request (the 'signaled' one, or a 'suspended' one of the tree_boot tasks) may therefore arrive after the terminate request, and a request for the state a thread is already in does nothing: the terminate request could still be lost. It is now repeated whenever the thread is suspended, until the thread has terminated. Signed-off-by: Hartmut Kaiser <hartmut.kaiser@gmail.com>
1 parent a4d3c71 commit 57666f4

1 file changed

Lines changed: 17 additions & 11 deletions

File tree

‎tests/regressions/threads/thread_rescheduling.cpp‎

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <hpx/modules/testing.hpp>
1313
#include <hpx/thread.hpp>
1414

15+
#include <atomic>
1516
#include <chrono>
1617
#include <cstdint>
1718
#include <vector>
@@ -114,7 +115,7 @@ void tree_boot(
114115
detail::wait(promises);
115116
}
116117

117-
bool woken = false;
118+
std::atomic<bool> woken(false);
118119

119120
///////////////////////////////////////////////////////////////////////////////
120121
void test_dummy_thread(std::uint64_t)
@@ -163,19 +164,24 @@ int hpx_main(variables_map& vm)
163164
before.get();
164165
after.get();
165166

166-
// The thread may still be pending from the request above (it may not
167-
// have run yet). A request for the state a thread is already in does
168-
// nothing, so the terminate request would be lost and the thread
169-
// would suspend again forever. Wait for it to be suspended first.
170-
while (hpx::threads::get_thread_state(thread_id.noref()).state() !=
171-
hpx::threads::thread_schedule_state::suspended)
167+
// A request for the state a thread is already in does nothing: the
168+
// terminate request is lost if the thread is still pending from the
169+
// request above (it may not have run yet). A request for a thread that
170+
// is running is carried out later by a helper task nothing waits for,
171+
// so the requests above may also arrive after the terminate request
172+
// and suspend the thread again. Repeat the terminate request whenever
173+
// the thread is suspended until it has terminated.
174+
while (!woken)
172175
{
176+
if (hpx::threads::get_thread_state(thread_id.noref()).state() ==
177+
hpx::threads::thread_schedule_state::suspended)
178+
{
179+
set_thread_state(thread_id.noref(),
180+
hpx::threads::thread_schedule_state::pending,
181+
hpx::threads::thread_restart_state::terminate);
182+
}
173183
hpx::this_thread::yield();
174184
}
175-
176-
set_thread_state(thread_id.noref(),
177-
hpx::threads::thread_schedule_state::pending,
178-
hpx::threads::thread_restart_state::terminate);
179185
}
180186

181187
hpx::local::finalize();

0 commit comments

Comments
 (0)