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>
49 struct CPLWorkerThreadJob;
52 struct CPLWorkerThread
55 CPLWorkerThread() =
default;
57 CPLThreadFunc pfnInitFunc =
nullptr;
58 void *pInitData =
nullptr;
60 CPLJoinableThread *hThread =
nullptr;
61 bool bMarkedAsWaiting =
false;
64 std::condition_variable m_cv{};
72 } CPLWorkerThreadState;
73 #endif // ndef DOXYGEN_SKIP
82 std::vector<std::unique_ptr<CPLWorkerThread>> aWT{};
84 std::condition_variable m_cv{};
85 volatile CPLWorkerThreadState eState = CPLWTS_OK;
89 CPLList* psWaitingWorkerThreadsList =
nullptr;
90 int nWaitingWorkerThreads = 0;
92 int m_nMaxThreads = 0;
94 static void WorkerThreadFunction(
void* user_data);
96 void DeclareJobFinished();
97 CPLWorkerThreadJob* GetNextJob(CPLWorkerThread* psWorkerThread);
104 bool Setup(
int nThreads,
105 CPLThreadFunc pfnInitFunc,
107 bool Setup(
int nThreads,
108 CPLThreadFunc pfnInitFunc,
110 bool bWaitallStarted);
112 std::unique_ptr<CPLJobQueue> CreateJobQueue();
114 bool SubmitJob(CPLThreadFunc pfnFunc,
void* pData);
115 bool SubmitJobs(CPLThreadFunc pfnFunc,
const std::vector<void*>& apData);
116 void WaitCompletion(
int nMaxRemainingJobs = 0);
128 std::mutex m_mutex{};
129 std::condition_variable m_cv{};
130 int m_nPendingJobs = 0;
132 static void JobQueueFunction(
void*);
133 void DeclareJobFinished();
147 bool SubmitJob(CPLThreadFunc pfnFunc,
void* pData);
151 #endif // CPL_WORKER_THREAD_POOL_H_INCLUDED_