Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
thread_pool_start_failure_test.cc
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
43#include <gtest/gtest.h>
44#include <thread_pool.H>
45
46#include <cstddef>
47#include <cstdlib>
48#include <new>
49#include <stdexcept>
50#include <thread>
51
52// ThreadSanitizer has its own global operator new and delete, which collide
53// with the ones below at link time; these tests are skipped under it.
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
59# endif
60#endif
61#ifndef ALEPH_START_FAILURE_TEST_UNDER_TSAN
62# define ALEPH_START_FAILURE_TEST_UNDER_TSAN 0
63#endif
64
65namespace
66{
67 // successful allocations of this thread before one fails; -1 never fails
68 thread_local int allocations_before_failure = -1;
69
70 // allocations of this thread, counted while it is not negative
71 thread_local long counted_allocations = -1;
72
73 void fail_allocation_after(const int n) noexcept { allocations_before_failure = n; }
74
75 void stop_failing() noexcept { allocations_before_failure = -1; }
76
77 // Whether starting a std::thread allocates through the global operator new
78 bool thread_start_allocates()
79 {
80 counted_allocations = 0;
81 std::thread([] {}).join();
82 const bool allocates = counted_allocations > 0;
83 counted_allocations = -1;
84 return allocates;
85 }
86
87#if !ALEPH_START_FAILURE_TEST_UNDER_TSAN
88 void * allocate(std::size_t size)
89 {
90 if (counted_allocations >= 0)
91 ++counted_allocations;
92
93 if (allocations_before_failure == 0)
94 {
95 allocations_before_failure = -1;
96 throw std::bad_alloc();
97 }
98 if (allocations_before_failure > 0)
99 --allocations_before_failure;
100
101 if (void * ptr = std::malloc(size == 0 ? 1 : size))
102 return ptr;
103 throw std::bad_alloc();
104 }
105#endif
106}
107
108#if !ALEPH_START_FAILURE_TEST_UNDER_TSAN
109
110void * operator new(std::size_t size) { return allocate(size); }
111
112void * operator new[](std::size_t size) { return allocate(size); }
113
114void * operator new(std::size_t size, const std::nothrow_t &) noexcept
115{
116 try
117 {
118 return allocate(size);
119 }
120 catch (...)
121 {
122 return nullptr;
123 }
124}
125
126void * operator new[](std::size_t size, const std::nothrow_t &) noexcept
127{
128 try
129 {
130 return allocate(size);
131 }
132 catch (...)
133 {
134 return nullptr;
135 }
136}
137
138void operator delete(void * ptr) noexcept { std::free(ptr); }
139
140void operator delete[](void * ptr) noexcept { std::free(ptr); }
141
142void operator delete(void * ptr, std::size_t) noexcept { std::free(ptr); }
143
144void operator delete[](void * ptr, std::size_t) noexcept { std::free(ptr); }
145
146void operator delete(void * ptr, const std::nothrow_t &) noexcept { std::free(ptr); }
147
148void operator delete[](void * ptr, const std::nothrow_t &) noexcept { std::free(ptr); }
149
150#endif // !ALEPH_START_FAILURE_TEST_UNDER_TSAN
151
152using namespace Aleph;
153
154// A worker that could not be started left the ones started before it running
155// on members that were being destroyed: std::terminate, or a hang. Now the
156// constructor stops and joins them, and rethrows. Every allocation of the
157// constructor fails in turn, so each worker fails to start once.
158TEST(ThreadPoolStartFailure, ConstructorStopsTheStartedWorkersAndRethrows)
159{
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";
164
165 int failures = 0;
166 for (int n = 0;; ++n)
167 {
168 ASSERT_LT(n, 100) << "the constructor never stopped allocating";
169 fail_allocation_after(n);
170 try
171 {
172 ThreadPool pool(4);
173 stop_failing();
174 EXPECT_EQ(pool.num_threads(), 4u);
175 EXPECT_EQ(pool.enqueue([] { return 42; }).get(), 42);
176 break;
177 }
178 catch (const std::bad_alloc &)
179 {
180 stop_failing();
181 ++failures;
182 }
183 }
184
185 // at least the state of each of the 4 threads
186 EXPECT_GE(failures, 4);
187}
188
189// A failed resize() left the pool with the new workers it could start, or
190// with none and not stopped, so that a task would wait forever. Now it stops
191// and joins them, and the pool is stopped.
192TEST(ThreadPoolStartFailure, FailedResizeLeavesThePoolStopped)
193{
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";
198
199 int failures = 0;
200 for (int n = 0;; ++n)
201 {
202 ASSERT_LT(n, 100) << "resize() never stopped allocating";
203 ThreadPool pool(2);
204 fail_allocation_after(n);
205 try
206 {
207 pool.resize(5);
208 stop_failing();
209 EXPECT_EQ(pool.num_threads(), 5u);
210 EXPECT_EQ(pool.enqueue([] { return 42; }).get(), 42);
211 break;
212 }
213 catch (const std::bad_alloc &)
214 {
215 stop_failing();
216 ++failures;
217 EXPECT_TRUE(pool.is_stopped()) << n;
218 EXPECT_EQ(pool.num_threads(), 0u) << n;
219 EXPECT_TRUE(pool.is_idle()) << n;
220 EXPECT_THROW((void) pool.enqueue([] { return 0; }), std::runtime_error) << n;
221 }
222 }
223
224 // at least the state of each of the 5 new threads
225 EXPECT_GE(failures, 5);
226}
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.
#define TEST(name)
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.
Definition ah-arena.H:478
A modern, efficient thread pool for parallel task execution.
#define ALEPH_START_FAILURE_TEST_UNDER_TSAN