Line data Source code
1 : /**********************************************************************
2 : *
3 : * Project: CPL - Common Portability Library
4 : * Purpose: CPL worker thread pool
5 : * Author: Even Rouault, <even dot rouault at spatialys dot com>
6 : *
7 : **********************************************************************
8 : * Copyright (c) 2015, Even Rouault, <even dot rouault at spatialys dot com>
9 : *
10 : * SPDX-License-Identifier: MIT
11 : ****************************************************************************/
12 :
13 : #include "cpl_port.h"
14 : #include "cpl_worker_thread_pool.h"
15 :
16 : #include <cstddef>
17 : #include <memory>
18 :
19 : #include "cpl_conv.h"
20 : #include "cpl_error.h"
21 : #include "cpl_vsi.h"
22 :
23 : static thread_local CPLWorkerThreadPool *threadLocalCurrentThreadPool = nullptr;
24 :
25 : /************************************************************************/
26 : /* CPLWorkerThreadPool() */
27 : /************************************************************************/
28 :
29 : /** Instantiate a new pool of worker threads.
30 : *
31 : * The pool is in an uninitialized state after this call. The Setup() method
32 : * must be called.
33 : */
34 798 : CPLWorkerThreadPool::CPLWorkerThreadPool() : jobQueue{}
35 : {
36 798 : }
37 :
38 : /** Instantiate a new pool of worker threads.
39 : *
40 : * \param nThreads Number of threads in the pool.
41 : */
42 239 : CPLWorkerThreadPool::CPLWorkerThreadPool(int nThreads) : jobQueue{}
43 : {
44 239 : Setup(nThreads, nullptr, nullptr);
45 239 : }
46 :
47 : /************************************************************************/
48 : /* ~CPLWorkerThreadPool() */
49 : /************************************************************************/
50 :
51 : /** Destroys a pool of worker threads.
52 : *
53 : * Any still pending job will be completed before the destructor returns.
54 : */
55 1032 : CPLWorkerThreadPool::~CPLWorkerThreadPool()
56 : {
57 1032 : WaitCompletion();
58 :
59 : {
60 1032 : std::lock_guard<std::mutex> oGuard(m_mutex);
61 1032 : eState = CPLWTS_STOP;
62 : }
63 :
64 3540 : for (auto &wt : aWT)
65 : {
66 : {
67 5016 : std::lock_guard<std::mutex> oGuard(wt->m_mutex);
68 2508 : wt->m_cv.notify_one();
69 : }
70 2508 : CPLJoinThread(wt->hThread);
71 : }
72 :
73 1032 : CPLListDestroy(psWaitingWorkerThreadsList);
74 1032 : }
75 :
76 : /************************************************************************/
77 : /* GetThreadCount() */
78 : /************************************************************************/
79 :
80 1244 : int CPLWorkerThreadPool::GetThreadCount() const
81 : {
82 1244 : std::unique_lock<std::mutex> oGuard(m_mutex);
83 2488 : return m_nMaxThreads;
84 : }
85 :
86 : /************************************************************************/
87 : /* WorkerThreadFunction() */
88 : /************************************************************************/
89 :
90 3547 : void CPLWorkerThreadPool::WorkerThreadFunction(void *user_data)
91 : {
92 3547 : CPLWorkerThread *psWT = static_cast<CPLWorkerThread *>(user_data);
93 3547 : CPLWorkerThreadPool *poTP = psWT->poTP;
94 :
95 3547 : threadLocalCurrentThreadPool = poTP;
96 :
97 3547 : if (psWT->pfnInitFunc)
98 0 : psWT->pfnInitFunc(psWT->pInitData);
99 :
100 : while (true)
101 : {
102 66934 : std::function<void()> task = poTP->GetNextJob(psWT);
103 65850 : if (!task)
104 2508 : break;
105 :
106 63253 : task();
107 : #if DEBUG_VERBOSE
108 : CPLDebug("JOB", "%p finished a job", psWT);
109 : #endif
110 63373 : poTP->DeclareJobFinished();
111 63387 : }
112 2532 : }
113 :
114 : /************************************************************************/
115 : /* SubmitJob() */
116 : /************************************************************************/
117 :
118 : /** Queue a new job.
119 : *
120 : * @param pfnFunc Function to run for the job.
121 : * @param pData User data to pass to the job function.
122 : * @return true in case of success.
123 : */
124 7356 : bool CPLWorkerThreadPool::SubmitJob(CPLThreadFunc pfnFunc, void *pData)
125 : {
126 14677 : return SubmitJob([=] { pfnFunc(pData); });
127 : }
128 :
129 : /** Queue a new job.
130 : *
131 : * @param task Void function to execute.
132 : * @return true in case of success.
133 : */
134 73364 : bool CPLWorkerThreadPool::SubmitJob(std::function<void()> task)
135 : {
136 : #ifdef DEBUG
137 : {
138 146621 : std::unique_lock<std::mutex> oGuard(m_mutex);
139 73257 : CPLAssert(m_nMaxThreads > 0);
140 : }
141 : #endif
142 :
143 73254 : bool bMustIncrementWaitingWorkerThreadsAfterSubmission = false;
144 73254 : if (threadLocalCurrentThreadPool == this)
145 : {
146 : // If there are waiting threads or we have not started all allowed
147 : // threads, we can submit this job asynchronously
148 : {
149 114259 : std::unique_lock<std::mutex> oGuard(m_mutex);
150 68657 : if (nWaitingWorkerThreads > 0 ||
151 11523 : static_cast<int>(aWT.size()) < m_nMaxThreads)
152 : {
153 45621 : bMustIncrementWaitingWorkerThreadsAfterSubmission = true;
154 45621 : nWaitingWorkerThreads--;
155 : }
156 : }
157 57132 : if (!bMustIncrementWaitingWorkerThreadsAfterSubmission)
158 : {
159 : // otherwise there is a risk of deadlock, so execute synchronously.
160 11525 : task();
161 11512 : return true;
162 : }
163 : }
164 :
165 61736 : std::unique_lock<std::mutex> oGuard(m_mutex);
166 :
167 61828 : if (bMustIncrementWaitingWorkerThreadsAfterSubmission)
168 45695 : nWaitingWorkerThreads++;
169 :
170 61828 : if (static_cast<int>(aWT.size()) < m_nMaxThreads)
171 : {
172 : // CPLDebug("CPL", "Starting new thread...");
173 2208 : auto wt = std::make_unique<CPLWorkerThread>();
174 1104 : wt->poTP = this;
175 : //ABELL - Why should this fail? And this is a *pool* thread, not necessarily
176 : // tied to the submitted job. The submitted job still needs to run, even if
177 : // this fails. If we can't create a thread, should the entire pool become invalid?
178 1104 : wt->hThread = CPLCreateJoinableThread(WorkerThreadFunction, wt.get());
179 : /**
180 : if (!wt->hThread)
181 : {
182 : VSIFree(psJob);
183 : VSIFree(psItem);
184 : return false;
185 : }
186 : **/
187 1104 : if (wt->hThread)
188 1104 : aWT.emplace_back(std::move(wt));
189 : }
190 :
191 61721 : jobQueue.emplace(task);
192 61649 : nPendingJobs++;
193 :
194 61649 : if (psWaitingWorkerThreadsList)
195 : {
196 55294 : CPLWorkerThread *psWorkerThread =
197 55294 : static_cast<CPLWorkerThread *>(psWaitingWorkerThreadsList->pData);
198 :
199 55294 : CPLAssert(psWorkerThread->bMarkedAsWaiting);
200 55294 : psWorkerThread->bMarkedAsWaiting = false;
201 :
202 55294 : CPLList *psNext = psWaitingWorkerThreadsList->psNext;
203 55294 : CPLList *psToFree = psWaitingWorkerThreadsList;
204 55294 : psWaitingWorkerThreadsList = psNext;
205 55294 : nWaitingWorkerThreads--;
206 :
207 : #if DEBUG_VERBOSE
208 : CPLDebug("JOB", "Waking up %p", psWorkerThread);
209 : #endif
210 :
211 : #ifdef __COVERITY__
212 : CPLError(CE_Failure, CPLE_AppDefined, "Not implemented");
213 : #else
214 : {
215 110919 : std::lock_guard<std::mutex> oGuardWT(psWorkerThread->m_mutex);
216 : // coverity[uninit_use_in_call]
217 55555 : oGuard.unlock();
218 55535 : psWorkerThread->m_cv.notify_one();
219 : }
220 : #endif
221 :
222 55641 : CPLFree(psToFree);
223 : }
224 :
225 : // coverity[double_unlock]
226 62065 : return true;
227 : }
228 :
229 : /************************************************************************/
230 : /* SubmitJobs() */
231 : /************************************************************************/
232 :
233 : /** Queue several jobs
234 : *
235 : * @param pfnFunc Function to run for the job.
236 : * @param apData User data instances to pass to the job function.
237 : * @return true in case of success.
238 : */
239 170 : bool CPLWorkerThreadPool::SubmitJobs(CPLThreadFunc pfnFunc,
240 : const std::vector<void *> &apData)
241 : {
242 170 : if (apData.empty())
243 0 : return false;
244 :
245 : #ifdef DEBUG
246 : {
247 340 : std::unique_lock<std::mutex> oGuard(m_mutex);
248 170 : CPLAssert(m_nMaxThreads > 0);
249 : }
250 : #endif
251 :
252 170 : if (threadLocalCurrentThreadPool == this)
253 : {
254 : // If SubmitJob() is called from a worker thread of this queue,
255 : // then synchronously run the task to avoid deadlock.
256 0 : for (void *pData : apData)
257 0 : pfnFunc(pData);
258 0 : return true;
259 : }
260 :
261 340 : std::unique_lock<std::mutex> oGuard(m_mutex);
262 :
263 1604 : for (void *pData : apData)
264 : {
265 1434 : if (static_cast<int>(aWT.size()) < m_nMaxThreads)
266 : {
267 0 : std::unique_ptr<CPLWorkerThread> wt(new CPLWorkerThread);
268 0 : wt->poTP = this;
269 0 : wt->hThread =
270 0 : CPLCreateJoinableThread(WorkerThreadFunction, wt.get());
271 0 : if (wt->hThread == nullptr)
272 : {
273 0 : if (aWT.empty())
274 0 : return false;
275 : }
276 : else
277 : {
278 0 : aWT.emplace_back(std::move(wt));
279 : }
280 : }
281 :
282 2866 : jobQueue.emplace([=] { pfnFunc(pData); });
283 1434 : nPendingJobs++;
284 : }
285 :
286 585 : for (size_t i = 0; i < apData.size(); i++)
287 : {
288 436 : if (psWaitingWorkerThreadsList)
289 : {
290 : CPLWorkerThread *psWorkerThread;
291 :
292 415 : psWorkerThread = static_cast<CPLWorkerThread *>(
293 415 : psWaitingWorkerThreadsList->pData);
294 :
295 415 : CPLAssert(psWorkerThread->bMarkedAsWaiting);
296 415 : psWorkerThread->bMarkedAsWaiting = false;
297 :
298 415 : CPLList *psNext = psWaitingWorkerThreadsList->psNext;
299 415 : CPLList *psToFree = psWaitingWorkerThreadsList;
300 415 : psWaitingWorkerThreadsList = psNext;
301 415 : nWaitingWorkerThreads--;
302 :
303 : #if DEBUG_VERBOSE
304 : CPLDebug("JOB", "Waking up %p", psWorkerThread);
305 : #endif
306 : {
307 830 : std::lock_guard<std::mutex> oGuardWT(psWorkerThread->m_mutex);
308 : // coverity[uninit_use_in_call]
309 415 : oGuard.unlock();
310 415 : psWorkerThread->m_cv.notify_one();
311 : }
312 :
313 415 : CPLFree(psToFree);
314 415 : oGuard.lock();
315 : }
316 : else
317 : {
318 21 : break;
319 : }
320 : }
321 :
322 170 : return true;
323 : }
324 :
325 : /************************************************************************/
326 : /* WaitCompletion() */
327 : /************************************************************************/
328 :
329 : /** Wait for completion of part or whole jobs.
330 : *
331 : * @param nMaxRemainingJobs Maximum number of pendings jobs that are allowed
332 : * in the queue after this method has completed. Might
333 : * be 0 to wait for all jobs.
334 : */
335 5753 : void CPLWorkerThreadPool::WaitCompletion(int nMaxRemainingJobs)
336 : {
337 5753 : if (nMaxRemainingJobs < 0)
338 0 : nMaxRemainingJobs = 0;
339 11506 : std::unique_lock<std::mutex> oGuard(m_mutex);
340 5753 : m_cv.wait(oGuard, [this, nMaxRemainingJobs]
341 6932 : { return nPendingJobs <= nMaxRemainingJobs; });
342 5753 : }
343 :
344 : /************************************************************************/
345 : /* WaitEvent() */
346 : /************************************************************************/
347 :
348 : /** Wait for completion of at least one job, if there are any remaining,
349 : * or for WakeUpWaitEvent() to have been called.
350 : */
351 1424 : void CPLWorkerThreadPool::WaitEvent()
352 : {
353 : // NOTE - This isn't quite right. After nPendingJobsBefore is set but before
354 : // a notification occurs, jobs could be submitted which would increase
355 : // nPendingJobs, so a job completion may looks like a spurious wakeup.
356 1424 : std::unique_lock<std::mutex> oGuard(m_mutex);
357 1424 : if (nPendingJobs == 0)
358 22 : return;
359 1402 : const int nPendingJobsBefore = nPendingJobs;
360 1402 : m_cv.wait(oGuard, [this, nPendingJobsBefore]
361 3009 : { return nPendingJobs < nPendingJobsBefore || m_bNotifyEvent; });
362 1402 : m_bNotifyEvent = false;
363 : }
364 :
365 : /************************************************************************/
366 : /* WakeUpWaitEvent() */
367 : /************************************************************************/
368 :
369 : /** Wake-up WaitEvent().
370 : *
371 : * This method is thread-safe.
372 : *
373 : * @since GDAL 3.12
374 : */
375 267 : void CPLWorkerThreadPool::WakeUpWaitEvent()
376 : {
377 534 : std::unique_lock<std::mutex> oGuard(m_mutex);
378 267 : m_bNotifyEvent = true;
379 267 : m_cv.notify_one();
380 267 : }
381 :
382 : /************************************************************************/
383 : /* Setup() */
384 : /************************************************************************/
385 :
386 : /** Setup the pool.
387 : *
388 : * @param nThreads Number of threads to launch
389 : * @param pfnInitFunc Initialization function to run in each thread. May be NULL
390 : * @param pasInitData Array of initialization data. Its length must be nThreads,
391 : * or it should be NULL.
392 : * @return true if initialization was successful.
393 : */
394 619 : bool CPLWorkerThreadPool::Setup(int nThreads, CPLThreadFunc pfnInitFunc,
395 : void **pasInitData)
396 : {
397 619 : return Setup(nThreads, pfnInitFunc, pasInitData, true);
398 : }
399 :
400 : /** Setup the pool.
401 : *
402 : * @param nThreads Number of threads to launch
403 : * @param pfnInitFunc Initialization function to run in each thread. May be NULL
404 : * @param pasInitData Array of initialization data. Its length must be nThreads,
405 : * or it should be NULL.
406 : * @param bWaitallStarted Whether to wait for all threads to be fully started.
407 : * @return true if initialization was successful.
408 : */
409 645 : bool CPLWorkerThreadPool::Setup(int nThreads, CPLThreadFunc pfnInitFunc,
410 : void **pasInitData, bool bWaitallStarted)
411 : {
412 645 : CPLAssert(nThreads > 0);
413 :
414 1290 : if (nThreads > static_cast<int>(aWT.size()) && pfnInitFunc == nullptr &&
415 1290 : pasInitData == nullptr && !bWaitallStarted)
416 : {
417 25 : std::lock_guard<std::mutex> oGuard(m_mutex);
418 25 : if (nThreads > m_nMaxThreads)
419 25 : m_nMaxThreads = nThreads;
420 25 : return true;
421 : }
422 :
423 620 : bool bRet = true;
424 3064 : for (int i = static_cast<int>(aWT.size()); i < nThreads; i++)
425 : {
426 2444 : auto wt = std::make_unique<CPLWorkerThread>();
427 2444 : wt->pfnInitFunc = pfnInitFunc;
428 2444 : wt->pInitData = pasInitData ? pasInitData[i] : nullptr;
429 2444 : wt->poTP = this;
430 2444 : wt->hThread = CPLCreateJoinableThread(WorkerThreadFunction, wt.get());
431 2444 : if (wt->hThread == nullptr)
432 : {
433 0 : nThreads = i;
434 0 : bRet = false;
435 0 : break;
436 : }
437 2444 : aWT.emplace_back(std::move(wt));
438 : }
439 :
440 : {
441 1240 : std::lock_guard<std::mutex> oGuard(m_mutex);
442 620 : if (nThreads > m_nMaxThreads)
443 620 : m_nMaxThreads = nThreads;
444 : }
445 :
446 620 : if (bWaitallStarted)
447 : {
448 : // Wait all threads to be started
449 1240 : std::unique_lock<std::mutex> oGuard(m_mutex);
450 1544 : while (nWaitingWorkerThreads < nThreads)
451 : {
452 924 : m_cv.wait(oGuard);
453 : }
454 : }
455 :
456 620 : if (eState == CPLWTS_ERROR)
457 0 : bRet = false;
458 :
459 620 : return bRet;
460 : }
461 :
462 : /************************************************************************/
463 : /* DeclareJobFinished() */
464 : /************************************************************************/
465 :
466 63380 : void CPLWorkerThreadPool::DeclareJobFinished()
467 : {
468 126799 : std::lock_guard<std::mutex> oGuard(m_mutex);
469 63392 : nPendingJobs--;
470 63392 : m_cv.notify_one();
471 63414 : }
472 :
473 : /************************************************************************/
474 : /* GetNextJob() */
475 : /************************************************************************/
476 :
477 : std::function<void()>
478 66924 : CPLWorkerThreadPool::GetNextJob(CPLWorkerThread *psWorkerThread)
479 : {
480 66924 : std::unique_lock<std::mutex> oGuard(m_mutex);
481 : while (true)
482 : {
483 125503 : if (eState == CPLWTS_STOP)
484 65854 : return std::function<void()>();
485 :
486 122995 : if (jobQueue.size())
487 : {
488 : #if DEBUG_VERBOSE
489 : CPLDebug("JOB", "%p got a job", psWorkerThread);
490 : #endif
491 126676 : auto task = std::move(jobQueue.front());
492 63317 : jobQueue.pop();
493 63273 : return task;
494 : }
495 :
496 59613 : if (!psWorkerThread->bMarkedAsWaiting)
497 : {
498 59621 : psWorkerThread->bMarkedAsWaiting = true;
499 59621 : nWaitingWorkerThreads++;
500 :
501 : CPLList *psItem =
502 59621 : static_cast<CPLList *>(VSI_MALLOC_VERBOSE(sizeof(CPLList)));
503 59643 : if (psItem == nullptr)
504 : {
505 0 : eState = CPLWTS_ERROR;
506 0 : m_cv.notify_one();
507 :
508 0 : return nullptr;
509 : }
510 :
511 59643 : psItem->pData = psWorkerThread;
512 59643 : psItem->psNext = psWaitingWorkerThreadsList;
513 59643 : psWaitingWorkerThreadsList = psItem;
514 :
515 : #if DEBUG_VERBOSE
516 : CPLAssert(CPLListCount(psWaitingWorkerThreadsList) ==
517 : nWaitingWorkerThreads);
518 : #endif
519 : }
520 :
521 59635 : m_cv.notify_one();
522 :
523 : #if DEBUG_VERBOSE
524 : CPLDebug("JOB", "%p sleeping", psWorkerThread);
525 : #endif
526 :
527 : #ifdef __COVERITY__
528 : CPLError(CE_Failure, CPLE_AppDefined, "Not implemented");
529 : #else
530 118177 : std::unique_lock<std::mutex> oGuardThisThread(psWorkerThread->m_mutex);
531 : // coverity[uninit_use_in_call]
532 59650 : oGuard.unlock();
533 : // coverity[wait_not_in_locked_loop]
534 59632 : psWorkerThread->m_cv.wait(oGuardThisThread);
535 : // coverity[lock_order]
536 58452 : oGuard.lock();
537 : #endif
538 58519 : }
539 : }
540 :
541 : /************************************************************************/
542 : /* CreateJobQueue() */
543 : /************************************************************************/
544 :
545 : /** Create a new job queue based on this worker thread pool.
546 : *
547 : * The worker thread pool must remain alive while the returned object is
548 : * itself alive.
549 : *
550 : * @since GDAL 3.2
551 : */
552 29686 : std::unique_ptr<CPLJobQueue> CPLWorkerThreadPool::CreateJobQueue()
553 : {
554 29686 : return std::unique_ptr<CPLJobQueue>(new CPLJobQueue(this));
555 : }
556 :
557 : /************************************************************************/
558 : /* CPLJobQueue() */
559 : /************************************************************************/
560 :
561 : //! @cond Doxygen_Suppress
562 29738 : CPLJobQueue::CPLJobQueue(CPLWorkerThreadPool *poPool) : m_poPool(poPool)
563 : {
564 29718 : }
565 :
566 : //! @endcond
567 :
568 : /************************************************************************/
569 : /* ~CPLJobQueue() */
570 : /************************************************************************/
571 :
572 29820 : CPLJobQueue::~CPLJobQueue()
573 : {
574 29816 : WaitCompletion();
575 29825 : }
576 :
577 : /************************************************************************/
578 : /* DeclareJobFinished() */
579 : /************************************************************************/
580 :
581 65977 : void CPLJobQueue::DeclareJobFinished()
582 : {
583 132076 : std::lock_guard<std::mutex> oGuard(m_mutex);
584 65990 : m_nPendingJobs--;
585 65990 : m_cv.notify_one();
586 65996 : }
587 :
588 : /************************************************************************/
589 : /* SubmitJob() */
590 : /************************************************************************/
591 :
592 : /** Queue a new job.
593 : *
594 : * @param pfnFunc Function to run for the job.
595 : * @param pData User data to pass to the job function.
596 : * @return true in case of success.
597 : */
598 6765 : bool CPLJobQueue::SubmitJob(CPLThreadFunc pfnFunc, void *pData)
599 : {
600 13523 : return SubmitJob([=] { pfnFunc(pData); });
601 : }
602 :
603 : /** Queue a new job.
604 : *
605 : * @param task Task to execute.
606 : * @return true in case of success.
607 : */
608 65967 : bool CPLJobQueue::SubmitJob(std::function<void()> task)
609 : {
610 : {
611 65967 : std::lock_guard<std::mutex> oGuard(m_mutex);
612 65714 : m_nPendingJobs++;
613 : }
614 :
615 : // coverity[uninit_member,copy_constructor_call]
616 197564 : const auto lambda = [this, capturedTask = std::move(task)]
617 : {
618 65860 : capturedTask();
619 65915 : DeclareJobFinished();
620 65789 : };
621 : // cppcheck-suppress knownConditionTrueFalse
622 131687 : return m_poPool->SubmitJob(std::move(lambda));
623 : }
624 :
625 : /************************************************************************/
626 : /* WaitCompletion() */
627 : /************************************************************************/
628 :
629 : /** Wait for completion of part or whole jobs.
630 : *
631 : * @param nMaxRemainingJobs Maximum number of pendings jobs that are allowed
632 : * in the queue after this method has completed. Might
633 : * be 0 to wait for all jobs.
634 : */
635 59418 : void CPLJobQueue::WaitCompletion(int nMaxRemainingJobs)
636 : {
637 118846 : std::unique_lock<std::mutex> oGuard(m_mutex);
638 59396 : m_cv.wait(oGuard, [this, nMaxRemainingJobs]
639 85900 : { return m_nPendingJobs <= nMaxRemainingJobs; });
640 59443 : }
641 :
642 : /************************************************************************/
643 : /* WaitEvent() */
644 : /************************************************************************/
645 :
646 : /** Wait for completion for at least one job.
647 : *
648 : * @return true if there are remaining jobs.
649 : */
650 237 : bool CPLJobQueue::WaitEvent()
651 : {
652 : // NOTE - This isn't quite right. After nPendingJobsBefore is set but before
653 : // a notification occurs, jobs could be submitted which would increase
654 : // nPendingJobs, so a job completion may looks like a spurious wakeup.
655 474 : std::unique_lock<std::mutex> oGuard(m_mutex);
656 237 : if (m_nPendingJobs == 0)
657 1 : return false;
658 :
659 236 : const int nPendingJobsBefore = m_nPendingJobs;
660 236 : m_cv.wait(oGuard, [this, nPendingJobsBefore]
661 472 : { return m_nPendingJobs < nPendingJobsBefore; });
662 236 : return m_nPendingJobs > 0;
663 : }
|