43#include <gtest/gtest.h>
54#if defined(__SANITIZE_THREAD__)
55# define ALEPH_START_FAILURE_TEST_UNDER_TSAN 1
56#elif defined(__has_feature)
57# if __has_feature(thread_sanitizer)
58# define ALEPH_START_FAILURE_TEST_UNDER_TSAN 1
61#ifndef ALEPH_START_FAILURE_TEST_UNDER_TSAN
62# define ALEPH_START_FAILURE_TEST_UNDER_TSAN 0
68 thread_local int allocations_before_failure = -1;
71 thread_local long counted_allocations = -1;
73 void fail_allocation_after(
const int n)
noexcept { allocations_before_failure = n; }
75 void stop_failing()
noexcept { allocations_before_failure = -1; }
78 bool thread_start_allocates()
80 counted_allocations = 0;
81 std::thread([] {}).
join();
82 const bool allocates = counted_allocations > 0;
83 counted_allocations = -1;
87#if !ALEPH_START_FAILURE_TEST_UNDER_TSAN
90 if (counted_allocations >= 0)
91 ++counted_allocations;
93 if (allocations_before_failure == 0)
95 allocations_before_failure = -1;
96 throw std::bad_alloc();
98 if (allocations_before_failure > 0)
99 --allocations_before_failure;
101 if (
void * ptr = std::malloc(
size == 0 ? 1 :
size))
103 throw std::bad_alloc();
108#if !ALEPH_START_FAILURE_TEST_UNDER_TSAN
114void *
operator new(std::size_t
size,
const std::nothrow_t &)
noexcept
126void *
operator new[](std::size_t
size,
const std::nothrow_t &)
noexcept
138void operator delete(
void * ptr)
noexcept { std::free(ptr); }
140void operator delete[](
void * ptr)
noexcept { std::free(ptr); }
142void operator delete(
void * ptr, std::size_t)
noexcept { std::free(ptr); }
144void operator delete[](
void * ptr, std::size_t)
noexcept { std::free(ptr); }
146void operator delete(
void * ptr,
const std::nothrow_t &)
noexcept { std::free(ptr); }
148void operator delete[](
void * ptr,
const std::nothrow_t &)
noexcept { std::free(ptr); }
152using namespace Aleph;
158TEST(ThreadPoolStartFailure, ConstructorStopsTheStartedWorkersAndRethrows)
161 GTEST_SKIP() <<
"ThreadSanitizer replaces the global operator new";
162 if (not thread_start_allocates())
163 GTEST_SKIP() <<
"std::thread does not allocate through operator new here";
166 for (
int n = 0;; ++n)
168 ASSERT_LT(n, 100) <<
"the constructor never stopped allocating";
169 fail_allocation_after(n);
175 EXPECT_EQ(pool.
enqueue([] { return 42; }).get(), 42);
178 catch (
const std::bad_alloc &)
186 EXPECT_GE(failures, 4);
192TEST(ThreadPoolStartFailure, FailedResizeLeavesThePoolStopped)
195 GTEST_SKIP() <<
"ThreadSanitizer replaces the global operator new";
196 if (not thread_start_allocates())
197 GTEST_SKIP() <<
"std::thread does not allocate through operator new here";
200 for (
int n = 0;; ++n)
202 ASSERT_LT(n, 100) <<
"resize() never stopped allocating";
204 fail_allocation_after(n);
210 EXPECT_EQ(pool.
enqueue([] { return 42; }).get(), 42);
213 catch (
const std::bad_alloc &)
219 EXPECT_TRUE(pool.
is_idle()) << n;
220 EXPECT_THROW((
void) pool.
enqueue([] { return 0; }), std::runtime_error) << n;
225 EXPECT_GE(failures, 5);
A reusable thread pool for efficient parallel task execution.
size_t num_threads() const noexcept
Get the number of worker threads.
bool is_idle() const
Check if the pool is idle (no pending or running tasks).
void resize(size_t new_size)
Resize the pool to a different number of workers.
bool is_stopped() const noexcept
Check if the pool has been shut down.
auto enqueue(F &&f, Args &&... args) -> std::future< std::invoke_result_t< F, Args... > >
Submit a task for execution and get a future for the result.
Main namespace for Aleph-w library functions.
size_t size(Node *root) noexcept
See compute_cardinality_rec(Node * root).
std::ostream & join(const C &c, const std::string &sep, std::ostream &out)
Join elements of an Aleph-style container into a stream.
T * allocate(AhArenaAllocator &arena, Args &&... args)
Allocate and construct an object in an arena.
A modern, efficient thread pool for parallel task execution.
#define ALEPH_START_FAILURE_TEST_UNDER_TSAN