Skip to content

Commit 3955a00

Browse files
author
rtri
committed
fix #5557
1 parent 2057a70 commit 3955a00

File tree

3 files changed

+224
-85
lines changed

3 files changed

+224
-85
lines changed

rts/System/Threading/ThreadPool.cpp

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#include "ThreadPool.h"
66
#include "System/Exceptions.h"
77
#include "System/myMath.h"
8-
#if !defined(UNITSYNC) && !defined(UNIT_TEST)
8+
#if (!defined(UNITSYNC) && !defined(UNIT_TEST))
99
#include "System/OffscreenGLContext.h"
1010
#endif
1111
#include "System/TimeProfiler.h"
@@ -72,11 +72,12 @@ static spring::signal newTasksSignal[2];
7272

7373
static _threadlocal int threadnum(0);
7474

75-
76-
#if !defined(UNITSYNC) && !defined(UNIT_TEST)
77-
static bool hasOGLthreads = false; // disable for now (not used atm)
78-
#else
79-
static bool hasOGLthreads = false;
75+
#ifndef UNITSYNC
76+
// if enabled, allows OpenGL calls from ThreadPool tasks
77+
// so certain logic (e.g. loading models) can be written
78+
// without forcing GL code to run within the main thread
79+
// this is highly experimental, use at own risk
80+
static bool glThreadSupport = false;
8081
#endif
8182

8283

@@ -162,7 +163,7 @@ static bool DoTask(int tid, bool async)
162163

163164
#ifdef USE_TASK_STATS_TRACKING
164165
const uint64_t wdt = tg->GetDeltaTime(spring_now());
165-
const uint64_t edt = tg->ExecuteLoop(false);
166+
const uint64_t edt = tg->ExecuteLoop(tid, false);
166167

167168
threadStats[async][tid].numTasksRun += 1;
168169
threadStats[async][tid].sumExecTime += edt;
@@ -172,7 +173,7 @@ static bool DoTask(int tid, bool async)
172173
threadStats[async][tid].minWaitTime = std::min(threadStats[async][tid].minWaitTime, wdt);
173174
threadStats[async][tid].maxWaitTime = std::max(threadStats[async][tid].maxWaitTime, wdt);
174175
#else
175-
tg->ExecuteLoop(false);
176+
tg->ExecuteLoop(tid, false);
176177
#endif
177178
}
178179

@@ -185,7 +186,7 @@ static bool DoTask(int tid, bool async)
185186

186187
#ifdef USE_TASK_STATS_TRACKING
187188
const uint64_t wdt = tg->GetDeltaTime(spring_now());
188-
const uint64_t edt = tg->ExecuteLoop(false);
189+
const uint64_t edt = tg->ExecuteLoop(tid, false);
189190

190191
threadStats[async][tid].numTasksRun += 1;
191192
threadStats[async][tid].sumExecTime += edt;
@@ -195,7 +196,7 @@ static bool DoTask(int tid, bool async)
195196
threadStats[async][tid].minWaitTime = std::min(threadStats[async][tid].minWaitTime, wdt);
196197
threadStats[async][tid].maxWaitTime = std::max(threadStats[async][tid].maxWaitTime, wdt);
197198
#else
198-
tg->ExecuteLoop(false);
199+
tg->ExecuteLoop(tid, false);
199200
#endif
200201
}
201202
}
@@ -251,7 +252,7 @@ void WaitForFinished(std::shared_ptr<ITaskGroup>&& taskGroup)
251252

252253
assert(!taskGroup->IsAsyncTask());
253254
assert(!taskGroup->SelfDelete());
254-
taskGroup->ExecuteLoop(true);
255+
taskGroup->ExecuteLoop(tid, true);
255256
}
256257

257258
// NOTE:
@@ -351,7 +352,7 @@ void NotifyWorkerThreads(bool force, bool async)
351352
static void SpawnThreads(int wantedNumThreads, int curNumThreads)
352353
{
353354
#ifndef UNITSYNC
354-
if (hasOGLthreads) {
355+
if (glThreadSupport) {
355356
try {
356357
for (int i = curNumThreads; i < wantedNumThreads; ++i) {
357358
exitFlags[i] = false;
@@ -363,7 +364,7 @@ static void SpawnThreads(int wantedNumThreads, int curNumThreads)
363364
// shared gl context creation failed
364365
ThreadPool::SetThreadCount(0);
365366

366-
hasOGLthreads = false;
367+
glThreadSupport = false;
367368
curNumThreads = ThreadPool::GetNumThreads();
368369
}
369370
} else
@@ -391,7 +392,7 @@ static void KillThreads(int wantedNumThreads, int curNumThreads)
391392
assert(!workerThreads[ true].empty());
392393

393394
#ifndef UNITSYNC
394-
if (hasOGLthreads) {
395+
if (glThreadSupport) {
395396
{ auto th = reinterpret_cast<COffscreenGLThread*>(workerThreads[false].back()); th->join(); delete th; }
396397
{ auto th = reinterpret_cast<COffscreenGLThread*>(workerThreads[ true].back()); th->join(); delete th; }
397398
} else
@@ -452,15 +453,15 @@ static std::uint32_t FindWorkerThreadCore(std::int32_t index, std::uint32_t avai
452453
return threadAvoidCore;
453454

454455
// fallback; use all
455-
return (~0);
456+
return (~0u);
456457
}
457458

458459

459460

460461

461462
void SetThreadCount(int wantedNumThreads)
462463
{
463-
const int curNumThreads = GetNumThreads();
464+
const int curNumThreads = GetNumThreads(); // includes main
464465
const int wtdNumThreads = Clamp(wantedNumThreads, 1, GetMaxThreads());
465466

466467
const char* fmts[4] = {

0 commit comments

Comments
 (0)