Line data Source code
1 : /********************************************************************** 2 : * 3 : * Project: GDAL 4 : * Purpose: Global thread pool 5 : * Author: Even Rouault, <even dot rouault at spatialys dot com> 6 : * 7 : ********************************************************************** 8 : * Copyright (c) 2020, Even Rouault, <even dot rouault at spatialys dot com> 9 : * 10 : * SPDX-License-Identifier: MIT 11 : ****************************************************************************/ 12 : 13 : #include "gdal_thread_pool.h" 14 : 15 : #include <mutex> 16 : 17 : // For unclear reasons, attempts at making this a std::unique_ptr<>, even 18 : // through a GetCompressThreadPool() method like GetMutexThreadPool(), lead 19 : // to "ctest -R autotest_alg" (and other autotest components as well) 20 : // to hang forever once the tests have terminated. 21 : static CPLWorkerThreadPool *gpoCompressThreadPool = nullptr; 22 : 23 1054 : static std::mutex &GetMutexThreadPool() 24 : { 25 : static std::mutex gMutexThreadPool; 26 1054 : return gMutexThreadPool; 27 : } 28 : 29 121 : CPLWorkerThreadPool *GDALGetGlobalThreadPool(int nThreads) 30 : { 31 121 : std::lock_guard oGuard(GetMutexThreadPool()); 32 121 : if (gpoCompressThreadPool == nullptr) 33 : { 34 5 : gpoCompressThreadPool = new CPLWorkerThreadPool(); 35 5 : if (!gpoCompressThreadPool->Setup(nThreads, nullptr, nullptr, false)) 36 : { 37 0 : delete gpoCompressThreadPool; 38 0 : gpoCompressThreadPool = nullptr; 39 : } 40 : } 41 116 : else if (nThreads > gpoCompressThreadPool->GetThreadCount()) 42 : { 43 : // Increase size of thread pool 44 2 : gpoCompressThreadPool->Setup(nThreads, nullptr, nullptr, false); 45 : } 46 242 : return gpoCompressThreadPool; 47 : } 48 : 49 933 : void GDALDestroyGlobalThreadPool() 50 : { 51 933 : std::lock_guard oGuard(GetMutexThreadPool()); 52 933 : delete gpoCompressThreadPool; 53 933 : gpoCompressThreadPool = nullptr; 54 933 : }