2019-01-11 19:12:36 +00:00
|
|
|
#include <Common/ThreadPool.h>
|
2023-03-22 07:49:22 +00:00
|
|
|
#include <Common/CurrentMetrics.h>
|
2016-09-26 10:15:33 +00:00
|
|
|
|
2019-06-29 23:23:53 +00:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
2016-09-26 10:15:33 +00:00
|
|
|
/** Reproduces bug in ThreadPool.
|
|
|
|
* It get stuck if we call 'wait' many times from many other threads simultaneously.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2023-03-22 07:49:22 +00:00
|
|
|
namespace CurrentMetrics
|
|
|
|
{
|
|
|
|
extern const Metric LocalThread;
|
|
|
|
extern const Metric LocalThreadActive;
|
|
|
|
}
|
|
|
|
|
2019-06-29 23:23:53 +00:00
|
|
|
TEST(ThreadPool, ConcurrentWait)
|
2016-09-26 10:15:33 +00:00
|
|
|
{
|
|
|
|
auto worker = []
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < 100000000; ++i)
|
|
|
|
__asm__ volatile ("nop");
|
|
|
|
};
|
|
|
|
|
|
|
|
constexpr size_t num_threads = 4;
|
|
|
|
constexpr size_t num_jobs = 4;
|
|
|
|
|
2023-03-22 07:49:22 +00:00
|
|
|
ThreadPool pool(CurrentMetrics::LocalThread, CurrentMetrics::LocalThreadActive, num_threads);
|
2016-09-26 10:15:33 +00:00
|
|
|
|
|
|
|
for (size_t i = 0; i < num_jobs; ++i)
|
2019-10-17 14:41:27 +00:00
|
|
|
pool.scheduleOrThrowOnError(worker);
|
2016-09-26 10:15:33 +00:00
|
|
|
|
|
|
|
constexpr size_t num_waiting_threads = 4;
|
|
|
|
|
2023-03-22 07:49:22 +00:00
|
|
|
ThreadPool waiting_pool(CurrentMetrics::LocalThread, CurrentMetrics::LocalThreadActive, num_waiting_threads);
|
2016-09-26 10:15:33 +00:00
|
|
|
|
|
|
|
for (size_t i = 0; i < num_waiting_threads; ++i)
|
2019-10-17 14:41:27 +00:00
|
|
|
waiting_pool.scheduleOrThrowOnError([&pool] { pool.wait(); });
|
2016-09-26 10:15:33 +00:00
|
|
|
|
|
|
|
waiting_pool.wait();
|
|
|
|
}
|