ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ThreadPool.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*---------------------------- File ThreadPool.h ---------------------------*/
3/*--------------------------------------------------------------------------*/
111/*--------------------------------------------------------------------------*/
112/*----------------------------- DEFINITIONS --------------------------------*/
113/*--------------------------------------------------------------------------*/
114
115#ifndef __ADS_ThreadPool
116 #define __ADS_ThreadPool
117/* self-identification: #endif constant */
118
119/*--------------------------------------------------------------------------*/
120/*------------------------------ INCLUDES ----------------------------------*/
121/*--------------------------------------------------------------------------*/
122
123#include "ADS/Contract.h"
127
128#include <algorithm>
129#include <atomic>
130#include <chrono>
131#include <cstddef>
132#include <cstdint>
133#include <exception>
134#include <memory>
135#include <mutex>
136#include <thread>
137#include <vector>
138
139/*--------------------------------------------------------------------------*/
140/*--------------------------- NAMESPACE ------------------------------------*/
141/*--------------------------------------------------------------------------*/
142
143namespace ADS
144{
145
146/*--------------------------------------------------------------------------*/
147/*---------------------------- CLASS ThreadPool ----------------------------*/
148/*--------------------------------------------------------------------------*/
149
152{
153
154/*--------------------------------------------------------------------------*/
155 public:
161 explicit ThreadPool( std::size_t p = 0 , std::int64_t spin = 2000000 ,
162 std::int64_t maxSpin = 200000000 );
164 ThreadPool( const ThreadPool & ) = delete;
166 ThreadPool & operator=( const ThreadPool & ) = delete;
167
170
172 [[nodiscard]] std::size_t concurrency( void ) const {
173 return( f_slot.size() );
174 }
175
209 template< class F >
210 void bulk( std::size_t n , F && f );
211
235 template< class F , class G >
236 void invoke( F && f , G && g );
237
239 using lock_type = std::mutex;
240
269 template< class F >
270 void run( std::size_t p , F && f );
271
272/*--------------------------------------------------------------------------*/
273 private:
275 struct Task
276 {
277 void ( *run )( Task * ) noexcept;
278 };
279
281 struct alignas( 128 ) Slot
282 {
283 WorkStealingDeque< Task * > deque{ 256 };
284 std::uint64_t seed = 0;
285 };
286
288 struct Self
289 {
290 ThreadPool * pool;
291 std::size_t slot;
292 };
293
296 class Enter;
297
298 template< class F >
299 struct BulkJob;
300 template< class G >
301 struct InvokeTask;
302
304 void loop( std::size_t s );
305
308 template< class P >
309 bool lookFor( P & ready , std::chrono::steady_clock::time_point t0 ,
310 std::int64_t budget );
311
313 Task * find( std::size_t s );
314
316 bool help( std::size_t s );
317
319 bool claim( void );
320
322 static constexpr std::int64_t kLongestSpin = std::int64_t( 1 ) << 58;
323
325 static inline thread_local Self tl_self{ nullptr , 0 };
326
327 std::vector< std::unique_ptr< Slot > > f_slot;
328 std::vector< std::thread > f_thread;
329 EventCount f_event;
330 std::atomic< bool > f_stop{ false };
331 std::mutex f_master;
332 std::int64_t f_spin;
333 std::int64_t f_maxSpin;
334 std::mutex f_runOne;
335 std::mutex f_runLock;
336 std::atomic< bool > f_runOpen{ false };
337 std::size_t f_runNext = 0;
338 std::size_t f_runN = 0;
339 void ( *f_runCall )( void * , std::size_t ) = nullptr;
340 void * f_runCtx = nullptr;
341 std::atomic< std::size_t > f_runDone{ 0 };
342 }; // end( class ThreadPool )
343
344/*--------------------------------------------------------------------------*/
345
346} // end( namespace ADS )
347
348/*--------------------------------------------------------------------------*/
349/*-------------------------- IMPLEMENTATION --------------------------------*/
350/*--------------------------------------------------------------------------*/
351
352#include "ADS/parallel/impl/ThreadPool.tpp"
353
354/*--------------------------------------------------------------------------*/
355
356#endif /* ThreadPool.h included */
357
358/*--------------------------------------------------------------------------*/
359/*------------------------ End File ThreadPool.h ---------------------------*/
360/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
An event count, beyond the book: the way for a thread to sleep until a condition on a lock-free struc...
The layer of execution of the parallel algorithms of ADS, beyond the book: an executor runs the calls...
The deque of work stealing of Chase and Lev, beyond the book: its owner thread pushes and takes at th...
void invoke(F &&f, G &&g)
the calls f() and g() in parallel, returning when both have returned
std::size_t concurrency(void) const
the number of threads, the caller included
Definition ThreadPool.h:172
void run(std::size_t p, F &&f)
the calls of long-lived workers, each begun by a thread of the pool that holds no other one,...
std::mutex lock_type
the lock of the data shared by the workers of run()
Definition ThreadPool.h:239
~ThreadPool()
stops the workers, which first finish their tasks
ThreadPool & operator=(const ThreadPool &)=delete
no copy, since the workers hold the address of the pool
ThreadPool(std::size_t p=0, std::int64_t spin=2000000, std::int64_t maxSpin=200000000)
a pool of threads, the caller included (the number of the hardware threads if ), whose idle workers ...
void bulk(std::size_t n, F &&f)
the calls on the threads of the pool, returning when all have returned
ThreadPool(const ThreadPool &)=delete
no copy, since the workers hold the address of the pool
Definition ADT.h:96