|
GDAL
|
00001 /********************************************************************** 00002 * $Id: cpl_worker_thread_pool.h 37003 2016-12-23 14:54:07Z goatbar $ 00003 * 00004 * Project: CPL - Common Portability Library 00005 * Purpose: CPL worker thread pool 00006 * Author: Even Rouault, <even dot rouault at spatialys dot com> 00007 * 00008 ********************************************************************** 00009 * Copyright (c) 2015, Even Rouault, <even dot rouault at spatialys dot com> 00010 * 00011 * Permission is hereby granted, free of charge, to any person obtaining a 00012 * copy of this software and associated documentation files (the "Software"), 00013 * to deal in the Software without restriction, including without limitation 00014 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 00015 * and/or sell copies of the Software, and to permit persons to whom the 00016 * Software is furnished to do so, subject to the following conditions: 00017 * 00018 * The above copyright notice and this permission notice shall be included 00019 * in all copies or substantial portions of the Software. 00020 * 00021 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00022 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00023 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 00024 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00025 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 00026 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 00027 * DEALINGS IN THE SOFTWARE. 00028 ****************************************************************************/ 00029 00030 #ifndef CPL_WORKER_THREAD_POOL_H_INCLUDED_ 00031 #define CPL_WORKER_THREAD_POOL_H_INCLUDED_ 00032 00033 #include "cpl_multiproc.h" 00034 #include "cpl_list.h" 00035 #include <vector> 00036 00044 #ifndef DOXYGEN_SKIP 00045 class CPLWorkerThreadPool; 00046 00047 typedef struct 00048 { 00049 CPLThreadFunc pfnFunc; 00050 void *pData; 00051 } CPLWorkerThreadJob; 00052 00053 typedef struct 00054 { 00055 CPLThreadFunc pfnInitFunc; 00056 void *pInitData; 00057 CPLWorkerThreadPool *poTP; 00058 CPLJoinableThread *hThread; 00059 int bMarkedAsWaiting; 00060 // CPLWorkerThreadJob *psNextJob; 00061 00062 CPLMutex *hMutex; 00063 CPLCond *hCond; 00064 } CPLWorkerThread; 00065 00066 typedef enum 00067 { 00068 CPLWTS_OK, 00069 CPLWTS_STOP, 00070 CPLWTS_ERROR 00071 } CPLWorkerThreadState; 00072 #endif // ndef DOXYGEN_SKIP 00073 00075 class CPL_DLL CPLWorkerThreadPool 00076 { 00077 std::vector<CPLWorkerThread> aWT; 00078 CPLCond* hCond; 00079 CPLMutex* hMutex; 00080 volatile CPLWorkerThreadState eState; 00081 CPLList* psJobQueue; 00082 volatile int nPendingJobs; 00083 00084 CPLList* psWaitingWorkerThreadsList; 00085 int nWaitingWorkerThreads; 00086 00087 static void WorkerThreadFunction(void* user_data); 00088 00089 void DeclareJobFinished(); 00090 CPLWorkerThreadJob* GetNextJob(CPLWorkerThread* psWorkerThread); 00091 00092 public: 00093 CPLWorkerThreadPool(); 00094 ~CPLWorkerThreadPool(); 00095 00096 bool Setup(int nThreads, 00097 CPLThreadFunc pfnInitFunc, 00098 void** pasInitData); 00099 bool SubmitJob(CPLThreadFunc pfnFunc, void* pData); 00100 bool SubmitJobs(CPLThreadFunc pfnFunc, const std::vector<void*>& apData); 00101 void WaitCompletion(int nMaxRemainingJobs = 0); 00102 00104 int GetThreadCount() const { return (int)aWT.size(); } 00105 }; 00106 00107 #endif // CPL_WORKER_THREAD_POOL_H_INCLUDED_
1.7.6.1.