GDAL

- Main Page
- Related Pages
- Classes
- Files

- File List
- File Members

cpl_worker_thread_pool.h
Go to the documentation of this file.
1 /**********************************************************************
2 * $Id: cpl_worker_thread_pool.h 33666 2016-03-07 05:21: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 class CPLWorkerThreadPool;
45
46 typedef struct
47 {
48 CPLThreadFunc pfnFunc;
49 void *pData;
50 } CPLWorkerThreadJob;
51
52 typedef struct
53 {
54 CPLThreadFunc pfnInitFunc;
55 void *pInitData;
56 CPLWorkerThreadPool *poTP;
57 CPLJoinableThread *hThread;
58 int bMarkedAsWaiting;
59 //CPLWorkerThreadJob *psNextJob;
60
61 CPLMutex *hMutex;
62 CPLCond *hCond;
63 } CPLWorkerThread;
64
65 typedef enum
66 {
67 CPLWTS_OK,
68 CPLWTS_STOP,
69 CPLWTS_ERROR
70 } CPLWorkerThreadState;
71
72 class CPL_DLL CPLWorkerThreadPool
73 {
74 std::vector<CPLWorkerThread> aWT;
75 CPLCond* hCond;
76 CPLMutex* hMutex;
77 volatile CPLWorkerThreadState eState;
78 CPLList* psJobQueue;
79 volatile int nPendingJobs;
80
81 CPLList* psWaitingWorkerThreadsList;
82 int nWaitingWorkerThreads;
83
84 static void WorkerThreadFunction(void* user_data);
85
86 void DeclareJobFinished();
87 CPLWorkerThreadJob* GetNextJob(CPLWorkerThread* psWorkerThread);
88
89 public:
90 CPLWorkerThreadPool();
91 ~CPLWorkerThreadPool();
92
93 bool Setup(int nThreads,
94 CPLThreadFunc pfnInitFunc,
95 void** pasInitData);
96 bool SubmitJob(CPLThreadFunc pfnFunc, void* pData);
97 bool SubmitJobs(CPLThreadFunc pfnFunc, const std::vector<void*>& apData);
98 void WaitCompletion(int nMaxRemainingJobs = 0);
99
100 int GetThreadCount() const { return (int)aWT.size(); }
101 };
102
103 #endif // CPL_WORKER_THREAD_POOL_H_INCLUDED_
_CPLList
List element structure.
Definition: cpl_list.h:48
CPLWorkerThread
Definition: cpl_worker_thread_pool.h:52
cpl_list.h
Simplest list implementation.
CPLWorkerThreadPool
Definition: cpl_worker_thread_pool.h:72
CPLWorkerThreadJob
Definition: cpl_worker_thread_pool.h:46

Generated for GDAL by 1.8.8.