5
5
#include " ThreadPool.h"
6
6
#include " System/Exceptions.h"
7
7
#include " System/myMath.h"
8
- #if !defined(UNITSYNC) && !defined(UNIT_TEST)
8
+ #if ( !defined(UNITSYNC) && !defined(UNIT_TEST) )
9
9
#include " System/OffscreenGLContext.h"
10
10
#endif
11
11
#include " System/TimeProfiler.h"
@@ -72,11 +72,12 @@ static spring::signal newTasksSignal[2];
72
72
73
73
static _threadlocal int threadnum (0 );
74
74
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 ;
80
81
#endif
81
82
82
83
@@ -162,7 +163,7 @@ static bool DoTask(int tid, bool async)
162
163
163
164
#ifdef USE_TASK_STATS_TRACKING
164
165
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 );
166
167
167
168
threadStats[async][tid].numTasksRun += 1 ;
168
169
threadStats[async][tid].sumExecTime += edt;
@@ -172,7 +173,7 @@ static bool DoTask(int tid, bool async)
172
173
threadStats[async][tid].minWaitTime = std::min (threadStats[async][tid].minWaitTime , wdt);
173
174
threadStats[async][tid].maxWaitTime = std::max (threadStats[async][tid].maxWaitTime , wdt);
174
175
#else
175
- tg->ExecuteLoop (false );
176
+ tg->ExecuteLoop (tid, false );
176
177
#endif
177
178
}
178
179
@@ -185,7 +186,7 @@ static bool DoTask(int tid, bool async)
185
186
186
187
#ifdef USE_TASK_STATS_TRACKING
187
188
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 );
189
190
190
191
threadStats[async][tid].numTasksRun += 1 ;
191
192
threadStats[async][tid].sumExecTime += edt;
@@ -195,7 +196,7 @@ static bool DoTask(int tid, bool async)
195
196
threadStats[async][tid].minWaitTime = std::min (threadStats[async][tid].minWaitTime , wdt);
196
197
threadStats[async][tid].maxWaitTime = std::max (threadStats[async][tid].maxWaitTime , wdt);
197
198
#else
198
- tg->ExecuteLoop (false );
199
+ tg->ExecuteLoop (tid, false );
199
200
#endif
200
201
}
201
202
}
@@ -251,7 +252,7 @@ void WaitForFinished(std::shared_ptr<ITaskGroup>&& taskGroup)
251
252
252
253
assert (!taskGroup->IsAsyncTask ());
253
254
assert (!taskGroup->SelfDelete ());
254
- taskGroup->ExecuteLoop (true );
255
+ taskGroup->ExecuteLoop (tid, true );
255
256
}
256
257
257
258
// NOTE:
@@ -351,7 +352,7 @@ void NotifyWorkerThreads(bool force, bool async)
351
352
static void SpawnThreads (int wantedNumThreads, int curNumThreads)
352
353
{
353
354
#ifndef UNITSYNC
354
- if (hasOGLthreads ) {
355
+ if (glThreadSupport ) {
355
356
try {
356
357
for (int i = curNumThreads; i < wantedNumThreads; ++i) {
357
358
exitFlags[i] = false ;
@@ -363,7 +364,7 @@ static void SpawnThreads(int wantedNumThreads, int curNumThreads)
363
364
// shared gl context creation failed
364
365
ThreadPool::SetThreadCount (0 );
365
366
366
- hasOGLthreads = false ;
367
+ glThreadSupport = false ;
367
368
curNumThreads = ThreadPool::GetNumThreads ();
368
369
}
369
370
} else
@@ -391,7 +392,7 @@ static void KillThreads(int wantedNumThreads, int curNumThreads)
391
392
assert (!workerThreads[ true ].empty ());
392
393
393
394
#ifndef UNITSYNC
394
- if (hasOGLthreads ) {
395
+ if (glThreadSupport ) {
395
396
{ auto th = reinterpret_cast <COffscreenGLThread*>(workerThreads[false ].back ()); th->join (); delete th; }
396
397
{ auto th = reinterpret_cast <COffscreenGLThread*>(workerThreads[ true ].back ()); th->join (); delete th; }
397
398
} else
@@ -452,15 +453,15 @@ static std::uint32_t FindWorkerThreadCore(std::int32_t index, std::uint32_t avai
452
453
return threadAvoidCore;
453
454
454
455
// fallback; use all
455
- return (~0 );
456
+ return (~0u );
456
457
}
457
458
458
459
459
460
460
461
461
462
void SetThreadCount (int wantedNumThreads)
462
463
{
463
- const int curNumThreads = GetNumThreads ();
464
+ const int curNumThreads = GetNumThreads (); // includes main
464
465
const int wtdNumThreads = Clamp (wantedNumThreads, 1 , GetMaxThreads ());
465
466
466
467
const char * fmts[4 ] = {
0 commit comments