GDAL
cpl_worker_thread_pool.h
Go to the documentation of this file.
1 /**********************************************************************
2  * $Id: cpl_worker_thread_pool.h 9b93d5aaef0e512d52da849390cb72856db540b6 2018-07-01 22:10:36 +0200 Even Rouault $
3  *
4  * Project: CPL - Common Portability Library
5  * Purpose: CPL worker thread pool
6  * Author: Even Rouault, <even dot rouault at spatialys dot com>
7  *
8  **********************************************************************
9  * Copyright (c) 2015, Even Rouault, <even dot rouault at spatialys dot com>
10  *
11  * Permission is hereby granted, free of charge, to any person obtaining a
12  * copy of this software and associated documentation files (the "Software"),
13  * to deal in the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15  * and/or sell copies of the Software, and to permit persons to whom the
16  * Software is furnished to do so, subject to the following conditions:
17  *
18  * The above copyright notice and this permission notice shall be included
19  * in all copies or substantial portions of the Software.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  * DEALINGS IN THE SOFTWARE.
28  ****************************************************************************/
29 
30 #ifndef CPL_WORKER_THREAD_POOL_H_INCLUDED_
31 #define CPL_WORKER_THREAD_POOL_H_INCLUDED_
32 
33 #include "cpl_multiproc.h"
34 #include "cpl_list.h"
35 #include <vector>
36 
44 #ifndef DOXYGEN_SKIP
46 
47 typedef struct
48 {
49  CPLThreadFunc pfnFunc;
50  void *pData;
51 } CPLWorkerThreadJob;
52 
53 typedef struct
54 {
55  CPLThreadFunc pfnInitFunc;
56  void *pInitData;
57  CPLWorkerThreadPool *poTP;
58  CPLJoinableThread *hThread;
59  int bMarkedAsWaiting;
60  // CPLWorkerThreadJob *psNextJob;
61 
62  CPLMutex *hMutex;
63  CPLCond *hCond;
64 } CPLWorkerThread;
65 
66 typedef enum
67 {
68  CPLWTS_OK,
69  CPLWTS_STOP,
70  CPLWTS_ERROR
71 } CPLWorkerThreadState;
72 #endif // ndef DOXYGEN_SKIP
73 
75 class CPL_DLL CPLWorkerThreadPool
76 {
78 
79  std::vector<CPLWorkerThread> aWT{};
80  CPLCond* hCond = nullptr;
81  CPLMutex* hMutex = nullptr;
82  volatile CPLWorkerThreadState eState = CPLWTS_OK;
83  CPLList* psJobQueue = nullptr;
84  volatile int nPendingJobs = 0;
85 
86  CPLList* psWaitingWorkerThreadsList = nullptr;
87  int nWaitingWorkerThreads = 0;
88 
89  static void WorkerThreadFunction(void* user_data);
90 
91  void DeclareJobFinished();
92  CPLWorkerThreadJob* GetNextJob(CPLWorkerThread* psWorkerThread);
93 
94  public:
97 
98  bool Setup(int nThreads,
99  CPLThreadFunc pfnInitFunc,
100  void** pasInitData);
101  bool Setup(int nThreads,
102  CPLThreadFunc pfnInitFunc,
103  void** pasInitData,
104  bool bWaitallStarted);
105  bool SubmitJob(CPLThreadFunc pfnFunc, void* pData);
106  bool SubmitJobs(CPLThreadFunc pfnFunc, const std::vector<void*>& apData);
107  void WaitCompletion(int nMaxRemainingJobs = 0);
108  void WaitEvent();
109 
111  int GetThreadCount() const { return static_cast<int>(aWT.size()); }
112 };
113 
114 #endif // CPL_WORKER_THREAD_POOL_H_INCLUDED_
List element structure.
Definition: cpl_list.h:51
int GetThreadCount() const
Return the number of threads setup.
Definition: cpl_worker_thread_pool.h:111
Simplest list implementation.
Pool of worker threads.
Definition: cpl_worker_thread_pool.h:75
#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:989

Generated for GDAL by doxygen 1.8.13.