mirror of
https://github.com/ddnet/ddnet.git
synced 2024-11-10 10:08:18 +00:00
Merge pull request #7353 from Robyt3/Engine-Jobpool-Refactoring
Minor refactoring of engine jobpool
This commit is contained in:
commit
042a04d610
|
@ -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
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
|
||||
#include <atomic>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
class CJobPool;
|
||||
|
||||
|
@ -37,12 +38,7 @@ public:
|
|||
|
||||
class CJobPool
|
||||
{
|
||||
enum
|
||||
{
|
||||
MAX_THREADS = 32
|
||||
};
|
||||
int m_NumThreads;
|
||||
void *m_apThreads[MAX_THREADS];
|
||||
std::vector<void *> m_vpThreads;
|
||||
std::atomic<bool> m_Shutdown;
|
||||
|
||||
LOCK m_Lock;
|
||||
|
|
Loading…
Reference in a new issue