Skip to content

Commit 8328898

Browse files
bghgaryCopilot
andcommitted
Add optional testing hook for blocking_concurrent_queue
Add a global before-wait callback invoked while holding the queue mutex, right before condition_variable::wait(). This enables deterministic testing of lost-wakeup race conditions in code that uses the queue. Behind #ifdef ARCANA_TESTING_HOOKS -- zero cost when not enabled. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 53e5b05 commit 8328898

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

Source/Shared/arcana/threading/blocking_concurrent_queue.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,24 @@
77
#include <queue>
88
#include <condition_variable>
99

10+
#ifdef ARCANA_TESTING_HOOKS
11+
#include <functional>
12+
#endif
13+
1014
namespace arcana
1115
{
16+
#ifdef ARCANA_TESTING_HOOKS
17+
namespace detail
18+
{
19+
inline std::function<void()> beforeWaitCallback{[]() {}};
20+
}
21+
22+
inline void set_before_wait_callback(std::function<void()> callback)
23+
{
24+
detail::beforeWaitCallback = std::move(callback);
25+
}
26+
#endif
27+
1228
template<typename T, size_t max_size = std::numeric_limits<size_t>::max()>
1329
class blocking_concurrent_queue
1430
{
@@ -95,6 +111,9 @@ namespace arcana
95111
{
96112
while (!cancel.cancelled() && m_data.empty())
97113
{
114+
#ifdef ARCANA_TESTING_HOOKS
115+
detail::beforeWaitCallback();
116+
#endif
98117
m_dataReady.wait(lock);
99118
}
100119
}
@@ -116,6 +135,9 @@ namespace arcana
116135
{
117136
while (!cancel.cancelled() && m_data.empty())
118137
{
138+
#ifdef ARCANA_TESTING_HOOKS
139+
detail::beforeWaitCallback();
140+
#endif
119141
m_dataReady.wait(lock);
120142
}
121143
}

0 commit comments

Comments
 (0)