2018-03-15 15:21:05 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2024-05-29 16:29:08 +10:00
|
|
|
#include <cruft/util/job/queue.hpp>
|
|
|
|
#include <cruft/util/tap.hpp>
|
2017-07-03 17:05:01 +10:00
|
|
|
|
2018-03-15 15:21:05 +11:00
|
|
|
#include <chrono>
|
|
|
|
#include <iostream>
|
2020-08-03 11:20:06 +10:00
|
|
|
#include <thread>
|
2018-03-15 15:21:05 +11:00
|
|
|
|
2018-03-22 14:59:03 +11:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
void sleep_inc (std::atomic<int> &count) noexcept
|
|
|
|
{
|
|
|
|
++count;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-15 15:21:05 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2017-07-03 17:05:01 +10:00
|
|
|
int
|
|
|
|
main (void)
|
|
|
|
{
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::TAP::logger tap;
|
2017-07-03 17:05:01 +10:00
|
|
|
|
|
|
|
// dispatch `INNER' simple jobs `OUTTER' times that simply increment an
|
|
|
|
// atomic variable and quit. this tests that all threads are created,
|
|
|
|
// executed, and finished. it's not definitive, but executing this many
|
|
|
|
// items this many times seems reasonably reliable in exposing deadlocks.
|
|
|
|
bool success = true;
|
2018-03-22 14:59:03 +11:00
|
|
|
constexpr int OUTTER = 4;
|
|
|
|
constexpr int INNER = 1<<10;
|
2017-07-03 17:05:01 +10:00
|
|
|
|
|
|
|
for (auto i = 0; i < OUTTER && success; ++i) {
|
|
|
|
std::atomic<int> count = 0;
|
|
|
|
|
|
|
|
{
|
2020-08-03 11:20:06 +10:00
|
|
|
cruft::job::queue q {std::thread::hardware_concurrency (), INNER};
|
2018-08-05 14:42:02 +10:00
|
|
|
std::vector<cruft::job::queue::cookie> cookies;
|
2018-07-24 15:49:11 +10:00
|
|
|
std::generate_n (std::back_inserter (cookies), INNER, [&] () {
|
|
|
|
return q.submit (
|
|
|
|
sleep_inc,
|
|
|
|
std::ref (count)
|
2018-03-15 15:21:05 +11:00
|
|
|
);
|
2018-07-24 15:49:11 +10:00
|
|
|
});
|
2017-07-03 17:05:01 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
success = count == INNER && success;
|
|
|
|
}
|
|
|
|
|
2021-04-13 16:05:08 +10:00
|
|
|
tap.expect (success, "{} trivial increment jobs of size {}", OUTTER, INNER);
|
2017-07-03 17:05:01 +10:00
|
|
|
return tap.status ();
|
|
|
|
}
|