|
NumericAL
A header-only C++20 lexer built on REAL
|
The threads every kernel shares, started once: a parallel_for costs a wake-up, not a thread start. More...
#include <parallel.hpp>
Public Member Functions | |
| thread_pool (const thread_pool &)=delete | |
| thread_pool & | operator= (const thread_pool &)=delete |
| thread_pool (thread_pool &&)=delete | |
| thread_pool & | operator= (thread_pool &&)=delete |
| ~thread_pool ()=delete | |
| bool | release_on_workers (void(*release)(), std::chrono::milliseconds wait) |
Runs release on every worker, each on its own thread between two runs, never inside one, and waits until all have, or wait has passed. | |
| bool | try_run (chunk_runner run, const void *body, std::size_t count, std::size_t chunk, std::size_t chunks, std::size_t helpers, side_runner side=nullptr, const void *side_body=nullptr) |
Runs chunks chunks of chunk indices up to count on the caller and at most helpers workers, then rethrows the first exception in index order. | |
Static Public Member Functions | |
| static thread_pool & | shared () |
| The pool, started on first use with a worker per hardware thread beside the caller. | |
| static std::mutex & | start_lock () noexcept |
| The lock under which shared makes and starts the pool, and which a fork takes before it forks. | |
| static thread_pool * | started () noexcept |
| The pool if shared has started it, else nullptr (asking starts nothing). | |
Private Member Functions | |
| thread_pool ()=default | |
| void | start () |
Starts the workers (after construction: each uses the pool), worker_cores: on Linux one per core, pinned there. Free to move, as a caller is, two threads at times shared one core's hyperthreads (a product of order 1024 on eight AVX2 cores took 8.1 ms, or 10.4 in most processes). | |
| void | serve (worker_bell &bell) |
A worker on core: waits for a run (polling a while, then asleep), works on it unless it sits it out, waits for the next. | |
Static Private Member Functions | |
| static void | lock_for_fork () noexcept |
| The started pool, nullptr before. | |
| static void | unlock_after_fork () noexcept |
pthread_atfork's handler after a fork, in the parent and in the child: the start lock released. | |
| static std::atomic< thread_pool * > & | instance () noexcept |
Private Attributes | |
| std::mutex | lock_ |
| Guards current_ and generation_. | |
| std::vector< std::unique_ptr< worker_bell > > | bells_ |
| Each worker's, added once it runs; read once the pool is published. | |
| std::shared_ptr< pool_job > | current_ |
| The latest run. | |
| std::atomic< std::size_t > | generation_ {0} |
| Counts the runs (polled without the lock). | |
| std::atomic< bool > | busy_ {false} |
| Whether a run holds the pool. | |
| std::mutex | release_lock_ |
| One release request at a time. | |
| std::atomic< void(*)()> | release_ {nullptr} |
| What a release request runs on each worker. | |
| std::atomic< std::size_t > | releases_ {0} |
| Counts the release requests. | |
| std::atomic< std::size_t > | released_ {0} |
| Counts the workers' acknowledgements. | |
| std::size_t | workers_ {0} |
| The threads started. | |
| long | owner_ {current_process()} |
| The process whose threads they are. | |
The threads every kernel shares, started once: a parallel_for costs a wake-up, not a thread start.
The caller takes chunks beside the workers, each taking the next chunk as it frees up (a slower core takes fewer). One run at a time: a parallel_for while the pool is busy (another thread's, or one nested in a body) runs its chunks itself, with the same result. The pool is never destroyed: its threads wait for work until the process ends, so no static destructor races them.
Definition at line 409 of file parallel.hpp.
|
delete |
|
delete |
|
delete |
|
privatedefault |
|
inlinestaticprivatenoexcept |
Definition at line 600 of file parallel.hpp.
The started pool, nullptr before.
pthread_atfork's handler before a fork: the start lock taken, so that the fork waits for a start.
Definition at line 587 of file parallel.hpp.
|
delete |
|
delete |
|
inline |
Runs release on every worker, each on its own thread between two runs, never inside one, and waits until all have, or wait has passed.
Definition at line 465 of file parallel.hpp.
|
inlineprivate |
A worker on core: waits for a run (polling a while, then asleep), works on it unless it sits it out, waits for the next.
Definition at line 646 of file parallel.hpp.
|
inlinestatic |
The pool, started on first use with a worker per hardware thread beside the caller.
| std::system_error | If the system starts no thread: the workers already started stay with the one pool, which a later call starts on from the first one missing (a new pool each time left them orphans). |
Definition at line 424 of file parallel.hpp.
|
inlineprivate |
Starts the workers (after construction: each uses the pool), worker_cores: on Linux one per core, pinned there. Free to move, as a caller is, two threads at times shared one core's hyperthreads (a product of order 1024 on eight AVX2 cores took 8.1 ms, or 10.4 in most processes).
Definition at line 611 of file parallel.hpp.
|
inlinestaticnoexcept |
The lock under which shared makes and starts the pool, and which a fork takes before it forks.
Definition at line 448 of file parallel.hpp.
|
inlinestaticnoexcept |
The pool if shared has started it, else nullptr (asking starts nothing).
Definition at line 455 of file parallel.hpp.
|
inline |
Runs chunks chunks of chunk indices up to count on the caller and at most helpers workers, then rethrows the first exception in index order.
side, the caller runs it first, while the workers take chunks, and its exception comes back before theirs. Definition at line 495 of file parallel.hpp.
pthread_atfork's handler after a fork, in the parent and in the child: the start lock released.
Definition at line 593 of file parallel.hpp.
|
private |
Each worker's, added once it runs; read once the pool is published.
Definition at line 704 of file parallel.hpp.
Whether a run holds the pool.
Definition at line 707 of file parallel.hpp.
|
private |
The latest run.
Definition at line 705 of file parallel.hpp.
|
private |
Counts the runs (polled without the lock).
Definition at line 706 of file parallel.hpp.
|
private |
Guards current_ and generation_.
Definition at line 703 of file parallel.hpp.
|
private |
The process whose threads they are.
Definition at line 716 of file parallel.hpp.
What a release request runs on each worker.
Definition at line 709 of file parallel.hpp.
|
private |
One release request at a time.
Definition at line 708 of file parallel.hpp.
|
private |
Counts the workers' acknowledgements.
Definition at line 711 of file parallel.hpp.
|
private |
Counts the release requests.
Definition at line 710 of file parallel.hpp.
|
private |
The threads started.
Definition at line 712 of file parallel.hpp.