diff options
Diffstat (limited to 'src/thirdparty/unrar/threadmisc.cpp')
-rw-r--r-- | src/thirdparty/unrar/threadmisc.cpp | 28 |
1 files changed, 24 insertions, 4 deletions
diff --git a/src/thirdparty/unrar/threadmisc.cpp b/src/thirdparty/unrar/threadmisc.cpp index d56867be4..4ad5af2e5 100644 --- a/src/thirdparty/unrar/threadmisc.cpp +++ b/src/thirdparty/unrar/threadmisc.cpp @@ -58,6 +58,18 @@ ThreadPool* CreateThreadPool() if (GlobalPoolUseCount++ == 0) GlobalPool=new ThreadPool(MaxPoolThreads); + // We use a simple thread pool, which does not allow to add tasks from + // different functions and threads in the same time. It is ok for RAR, + // but UnRAR.dll can be used in multithreaded environment. So if one of + // threads requests a copy of global pool and another copy is already + // in use, we create and return a new pool instead of existing global. + if (GlobalPoolUseCount > 1) + { + ThreadPool *Pool = new ThreadPool(MaxPoolThreads); + CriticalSectionEnd(&PoolCreateSync.CritSection); + return Pool; + } + CriticalSectionEnd(&PoolCreateSync.CritSection); return GlobalPool; } @@ -65,12 +77,20 @@ ThreadPool* CreateThreadPool() void DestroyThreadPool(ThreadPool *Pool) { - CriticalSectionStart(&PoolCreateSync.CritSection); + if (Pool!=NULL) + { + CriticalSectionStart(&PoolCreateSync.CritSection); - if (Pool!=NULL && Pool==GlobalPool && GlobalPoolUseCount > 0 && --GlobalPoolUseCount == 0) - delete GlobalPool; + if (Pool==GlobalPool && GlobalPoolUseCount > 0 && --GlobalPoolUseCount == 0) + delete GlobalPool; - CriticalSectionEnd(&PoolCreateSync.CritSection); + // To correctly work in multithreaded environment UnRAR.dll creates + // new pools if global pool is already in use. We delete such pools here. + if (Pool!=GlobalPool) + delete Pool; + + CriticalSectionEnd(&PoolCreateSync.CritSection); + } } |