30#ifndef CPL_WORKER_THREAD_POOL_H_INCLUDED_
31#define CPL_WORKER_THREAD_POOL_H_INCLUDED_
33#include "cpl_multiproc.h"
36#include <condition_variable>
49struct CPLWorkerThreadJob;
55 CPLWorkerThread() =
default;
57 CPLThreadFunc pfnInitFunc =
nullptr;
58 void *pInitData =
nullptr;
59 CPLWorkerThreadPool *poTP =
nullptr;
60 CPLJoinableThread *hThread =
nullptr;
61 bool bMarkedAsWaiting =
false;
64 std::condition_variable m_cv{};
72} CPLWorkerThreadState;
82 std::vector<std::unique_ptr<CPLWorkerThread>> aWT{};
84 std::condition_variable m_cv{};
85 volatile CPLWorkerThreadState eState = CPLWTS_OK;
87 volatile int nPendingJobs = 0;
89 CPLList* psWaitingWorkerThreadsList =
nullptr;
90 int nWaitingWorkerThreads = 0;
92 static void WorkerThreadFunction(
void* user_data);
94 void DeclareJobFinished();
95 CPLWorkerThreadJob* GetNextJob(CPLWorkerThread* psWorkerThread);
102 bool Setup(
int nThreads,
103 CPLThreadFunc pfnInitFunc,
105 bool Setup(
int nThreads,
106 CPLThreadFunc pfnInitFunc,
108 bool bWaitallStarted);
112 bool SubmitJob(CPLThreadFunc pfnFunc,
void* pData);
113 bool SubmitJobs(CPLThreadFunc pfnFunc,
const std::vector<void*>& apData);
126 std::mutex m_mutex{};
127 std::condition_variable m_cv{};
128 int m_nPendingJobs = 0;
130 static void JobQueueFunction(
void*);
131 void DeclareJobFinished();
145 bool SubmitJob(CPLThreadFunc pfnFunc,
void* pData);
146 void WaitCompletion(
int nMaxRemainingJobs = 0);
Job queue.
Definition cpl_worker_thread_pool.h:123
CPLWorkerThreadPool * GetPool()
Return the owning worker thread pool.
Definition cpl_worker_thread_pool.h:143
Pool of worker threads.
Definition cpl_worker_thread_pool.h:79
CPLWorkerThreadPool()
Instantiate a new pool of worker threads.
Definition cpl_worker_thread_pool.cpp:57
bool Setup(int nThreads, CPLThreadFunc pfnInitFunc, void **pasInitData)
Setup the pool.
Definition cpl_worker_thread_pool.cpp:346
int GetThreadCount() const
Return the number of threads setup.
Definition cpl_worker_thread_pool.h:118
std::unique_ptr< CPLJobQueue > CreateJobQueue()
Create a new job queue based on this worker thread pool.
Definition cpl_worker_thread_pool.cpp:491
bool SubmitJobs(CPLThreadFunc pfnFunc, const std::vector< void * > &apData)
Queue several jobs.
Definition cpl_worker_thread_pool.cpp:198
void WaitEvent()
Wait for completion of at least one job, if there are any remaining.
Definition cpl_worker_thread_pool.cpp:316
bool SubmitJob(CPLThreadFunc pfnFunc, void *pData)
Queue a new job.
Definition cpl_worker_thread_pool.cpp:130
void WaitCompletion(int nMaxRemainingJobs=0)
Wait for completion of part or whole jobs.
Definition cpl_worker_thread_pool.cpp:299
Simplest list implementation.
struct _CPLList CPLList
List element structure.
Definition cpl_list.h:48
#define CPL_DISALLOW_COPY_ASSIGN(ClassName)
Helper to remove the copy and assignment constructors so that the compiler will not generate the defa...
Definition cpl_port.h:955