GDAL
cpl_worker_thread_pool.h
Go to the documentation of this file.
1 /**********************************************************************
2  * $Id: cpl_worker_thread_pool.h 37003 2016-12-23 14:54:07Z goatbar $
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 {
77  std::vector<CPLWorkerThread> aWT;
78  CPLCond* hCond;
79  CPLMutex* hMutex;
80  volatile CPLWorkerThreadState eState;
81  CPLList* psJobQueue;
82  volatile int nPendingJobs;
83 
84  CPLList* psWaitingWorkerThreadsList;
85  int nWaitingWorkerThreads;
86 
87  static void WorkerThreadFunction(void* user_data);
88 
89  void DeclareJobFinished();
90  CPLWorkerThreadJob* GetNextJob(CPLWorkerThread* psWorkerThread);
91 
92  public:
95 
96  bool Setup(int nThreads,
97  CPLThreadFunc pfnInitFunc,
98  void** pasInitData);
99  bool SubmitJob(CPLThreadFunc pfnFunc, void* pData);
100  bool SubmitJobs(CPLThreadFunc pfnFunc, const std::vector<void*>& apData);
101  void WaitCompletion(int nMaxRemainingJobs = 0);
102 
104  int GetThreadCount() const { return (int)aWT.size(); }
105 };
106 
107 #endif // CPL_WORKER_THREAD_POOL_H_INCLUDED_
List element structure.
Definition: cpl_list.h:48
int GetThreadCount() const
Return the number of threads setup.
Definition: cpl_worker_thread_pool.h:104
Simplest list implementation.
Pool of worker threads.
Definition: cpl_worker_thread_pool.h:75

Generated for GDAL by doxygen 1.8.8.