diff --git a/src/base/system.cpp b/src/base/system.cpp index cb59a29bf..1bd0b64a7 100644 --- a/src/base/system.cpp +++ b/src/base/system.cpp @@ -824,16 +824,14 @@ void *thread_init(void (*threadfunc)(void *), void *u, const char *name) #if defined(CONF_PLATFORM_MACOS) && defined(__MAC_10_10) && __MAC_OS_X_VERSION_MIN_REQUIRED >= __MAC_10_10 pthread_attr_set_qos_class_np(&attr, QOS_CLASS_USER_INTERACTIVE, 0); #endif - int result = pthread_create(&id, &attr, thread_run, data); - if(result != 0) - { - dbg_msg("thread", "creating %s thread failed: %d", name, result); - return 0; - } + dbg_assert(pthread_create(&id, &attr, thread_run, data) == 0, "pthread_create failure"); return (void *)id; } #elif defined(CONF_FAMILY_WINDOWS) - return CreateThread(NULL, 0, thread_run, data, 0, NULL); + HANDLE thread = CreateThread(nullptr, 0, thread_run, data, 0, nullptr); + dbg_assert(thread != nullptr, "CreateThread failure"); + // TODO: Set thread name using SetThreadDescription (would require minimum Windows 10 version 1607) + return thread; #else #error not implemented #endif diff --git a/src/engine/client/client.cpp b/src/engine/client/client.cpp index 9ef83ab32..f1edb4786 100644 --- a/src/engine/client/client.cpp +++ b/src/engine/client/client.cpp @@ -4612,7 +4612,7 @@ int main(int argc, const char **argv) }); // create the components - IEngine *pEngine = CreateEngine(GAME_NAME, pFutureConsoleLogger, 2); + IEngine *pEngine = CreateEngine(GAME_NAME, pFutureConsoleLogger, 2 * std::thread::hardware_concurrency() + 2); IConsole *pConsole = CreateConsole(CFGFLAG_CLIENT).release(); IStorage *pStorage = CreateStorage(IStorage::STORAGETYPE_CLIENT, argc, (const char **)argv); IConfigManager *pConfigManager = CreateConfigManager(); diff --git a/src/engine/server/main.cpp b/src/engine/server/main.cpp index 83fb3a18f..2f12200ab 100644 --- a/src/engine/server/main.cpp +++ b/src/engine/server/main.cpp @@ -112,7 +112,7 @@ int main(int argc, const char **argv) IKernel *pKernel = IKernel::Create(); // create the components - IEngine *pEngine = CreateEngine(GAME_NAME, pFutureConsoleLogger, 2); + IEngine *pEngine = CreateEngine(GAME_NAME, pFutureConsoleLogger, 2 * std::thread::hardware_concurrency() + 2); IEngineMap *pEngineMap = CreateEngineMap(); IGameServer *pGameServer = CreateGameServer(); IConsole *pConsole = CreateConsole(CFGFLAG_SERVER | CFGFLAG_ECON).release(); diff --git a/src/engine/shared/jobs.cpp b/src/engine/shared/jobs.cpp index 387ab2144..36ee17d8d 100644 --- a/src/engine/shared/jobs.cpp +++ b/src/engine/shared/jobs.cpp @@ -19,7 +19,6 @@ int IJob::Status() CJobPool::CJobPool() { // empty the pool - m_NumThreads = 0; m_Shutdown = false; m_Lock = lock_create(); sphore_init(&m_Semaphore); @@ -69,21 +68,23 @@ void CJobPool::WorkerThread(void *pUser) void CJobPool::Init(int NumThreads) { // start threads - m_NumThreads = NumThreads > MAX_THREADS ? MAX_THREADS : NumThreads; + char aName[32]; + m_vpThreads.reserve(NumThreads); for(int i = 0; i < NumThreads; i++) - m_apThreads[i] = thread_init(WorkerThread, this, "CJobPool worker"); + { + str_format(aName, sizeof(aName), "CJobPool worker %d", i); + m_vpThreads.push_back(thread_init(WorkerThread, this, aName)); + } } void CJobPool::Destroy() { m_Shutdown = true; - for(int i = 0; i < m_NumThreads; i++) + for(size_t i = 0; i < m_vpThreads.size(); i++) sphore_signal(&m_Semaphore); - for(int i = 0; i < m_NumThreads; i++) - { - if(m_apThreads[i]) - thread_wait(m_apThreads[i]); - } + for(void *pThread : m_vpThreads) + thread_wait(pThread); + m_vpThreads.clear(); lock_destroy(m_Lock); sphore_destroy(&m_Semaphore); } diff --git a/src/engine/shared/jobs.h b/src/engine/shared/jobs.h index fa99166f9..2a24efbdc 100644 --- a/src/engine/shared/jobs.h +++ b/src/engine/shared/jobs.h @@ -7,6 +7,7 @@ #include #include +#include class CJobPool; @@ -37,12 +38,7 @@ public: class CJobPool { - enum - { - MAX_THREADS = 32 - }; - int m_NumThreads; - void *m_apThreads[MAX_THREADS]; + std::vector m_vpThreads; std::atomic m_Shutdown; LOCK m_Lock;