cpl_worker_thread_pool.h

Go to the documentation of this file.
00001 /**********************************************************************
00002  * $Id: cpl_worker_thread_pool.h 33666 2016-03-07 05:21: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 class CPLWorkerThreadPool;
00045 
00046 typedef struct
00047 {
00048     CPLThreadFunc  pfnFunc;
00049     void          *pData;
00050 } CPLWorkerThreadJob;
00051 
00052 typedef struct
00053 {
00054     CPLThreadFunc        pfnInitFunc;
00055     void                *pInitData;
00056     CPLWorkerThreadPool *poTP;
00057     CPLJoinableThread   *hThread;
00058     int                  bMarkedAsWaiting;
00059     //CPLWorkerThreadJob  *psNextJob;
00060 
00061     CPLMutex            *hMutex;
00062     CPLCond             *hCond;
00063 } CPLWorkerThread;
00064 
00065 typedef enum
00066 {
00067     CPLWTS_OK,
00068     CPLWTS_STOP,
00069     CPLWTS_ERROR
00070 } CPLWorkerThreadState;
00071 
00072 class CPL_DLL CPLWorkerThreadPool
00073 {
00074         std::vector<CPLWorkerThread> aWT;
00075         CPLCond* hCond;
00076         CPLMutex* hMutex;
00077         volatile CPLWorkerThreadState eState;
00078         CPLList* psJobQueue;
00079         volatile int nPendingJobs;
00080 
00081         CPLList* psWaitingWorkerThreadsList;
00082         int nWaitingWorkerThreads;
00083 
00084         static void WorkerThreadFunction(void* user_data);
00085 
00086         void DeclareJobFinished();
00087         CPLWorkerThreadJob* GetNextJob(CPLWorkerThread* psWorkerThread);
00088 
00089     public:
00090         CPLWorkerThreadPool();
00091        ~CPLWorkerThreadPool();
00092 
00093         bool Setup(int nThreads,
00094                    CPLThreadFunc pfnInitFunc,
00095                    void** pasInitData);
00096         bool SubmitJob(CPLThreadFunc pfnFunc, void* pData);
00097         bool SubmitJobs(CPLThreadFunc pfnFunc, const std::vector<void*>& apData);
00098         void WaitCompletion(int nMaxRemainingJobs = 0);
00099 
00100         int GetThreadCount() const { return (int)aWT.size(); }
00101 };
00102 
00103 #endif // CPL_WORKER_THREAD_POOL_H_INCLUDED_

Generated for GDAL by doxygen 1.6.1.