Skip to content

Commit

Permalink
Add regression test for WAIT_TIMEOUT under mutex contention
Browse files Browse the repository at this point in the history
Test case based off the pseudocode provided by @qwertymaster617 in #18.
  • Loading branch information
mqudsi committed Jun 27, 2021
1 parent 0bc424c commit 11a56d0
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
1 change: 1 addition & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ basic_tests = ['ManualResetInitialState',
'AutoResetInitialState',
'ManualResetBasicTests',
'AutoResetBasicTests',
'EventContention',
]
# Tests that required wfmo
wfmo_tests = [
Expand Down
39 changes: 39 additions & 0 deletions tests/EventContention.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// This tests contention on an auto-reset event that is always available at a high level, to verify
// that other threads acquiring the protective pthread mutex don't result in a spurious WAIT_TIMEOUT
// error for any `WaitForEvent()` callers.
//
// See https://github.com/neosmart/pevents/issues/18

#ifdef _WIN32
#include <Windows.h>
#endif
#include <cassert>
#include <iostream>
#include <pevents.h>
#include <thread>

using namespace neosmart;

int main() {
// Create an auto-reset event that is initially signalled
neosmart_event_t event = CreateEvent(false, true);


// Create n threads to constantly call SetEvent() in a tight loop
for (int i = 0; i < 16; ++i) {
std::thread t1([&] {
SetEvent(event);
});
t1.detach();
}

// Call WaitForEvent() in a tight loop; we can expect it to always be available.
for (int i = 0; i < 200000; ++i) {
int result = WaitForEvent(event, 0);
assert(result == 0);
// Guarantee this thread always calls `WaitForEvent()` on a signalled event
SetEvent(event);
}

return 0;
}

0 comments on commit 11a56d0

Please sign in to comment.