Skip to content

Commit 5efe41e

Browse files
committed
Made keep alive a runtime setting instead
1 parent 3977e24 commit 5efe41e

File tree

4 files changed

+62
-43
lines changed

4 files changed

+62
-43
lines changed

JobSystem/Src/CMakeLists.txt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@ set(JobSystemHeaderDirectory "${CMAKE_CURRENT_SOURCE_DIR}/")
1919
find_package(Threads REQUIRED)
2020

2121
add_library (JobSystem STATIC ${JobSystemSources})
22-
if(${JOBSYSTEM_KEEP_ALIVE})
23-
target_compile_definitions(JobSystem PUBLIC JBSYSTEM_KEEP_ALIVE)
24-
endif()
2522
target_include_directories(JobSystem PUBLIC ${JobSystemHeaderDirectory})
2623
set_property(TARGET JobSystem PROPERTY CXX_STANDARD 23)
2724

JobSystem/Src/JobSystem/JobSystem.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ namespace JbSystem
8686
return false;
8787
}
8888

89-
JobSystem::JobSystem(unsigned int threadCount, WorkerThreadLoop workerLoop) : _showStats(true)
89+
JobSystem::JobSystem(unsigned int threadCount, WorkerThreadLoop workerLoop) : _showStats(true), _keepAlive(false)
9090
{
9191
if (threadCount < _minimumActiveWorkers)
9292
{
@@ -255,6 +255,16 @@ namespace JbSystem
255255
return remainingJobs;
256256
}
257257

258+
void JobSystem::SetKeepAlive(bool option)
259+
{
260+
_keepAlive.store(option);
261+
}
262+
263+
bool JobSystem::IsKeepAliveEnabled() const
264+
{
265+
return _keepAlive.load(std::memory_order_relaxed);
266+
}
267+
258268
void JobSystem::WaitForAllJobs()
259269
{
260270
bool wasActive = false;

JobSystem/Src/JobSystem/JobSystem.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,10 @@ namespace JbSystem
184184
void OptimizePerformance();
185185
void MaybeOptimize();
186186

187+
void SetKeepAlive(bool option);
188+
189+
bool IsKeepAliveEnabled() const;
190+
187191
/// <summary>
188192
/// is the job system active or not
189193
/// </summary>
@@ -261,6 +265,8 @@ namespace JbSystem
261265
// Deadlock prevention
262266
JbSystem::Mutex _spawnedThreadsMutex;
263267
std::unordered_map<std::thread::id, std::thread> _spawnedThreadsExecutingIgnoredJobs;
268+
269+
std::atomic<bool> _keepAlive;
264270
};
265271

266272
template <typename... Args>

JobSystem/Src/JobSystem/WorkerThread.cpp

Lines changed: 45 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,14 @@ namespace JbSystem
112112
{
113113
_jobsystem->WorkerLoop(this);
114114

115-
#ifdef JBSYSTEM_KEEP_ALIVE
116-
std::this_thread::yield();
117-
#else
118-
break;
119-
#endif
115+
if (_jobsystem->IsKeepAliveEnabled())
116+
{
117+
std::this_thread::yield();
118+
}
119+
else
120+
{
121+
break;
122+
}
120123
}
121124
}
122125

@@ -198,46 +201,28 @@ namespace JbSystem
198201

199202
_modifyingThread.lock();
200203

201-
#ifndef JBSYSTEM_KEEP_ALIVE
202-
if (IsActive() || _shutdownRequested.load())
203-
{
204-
_modifyingThread.unlock();
205-
return;
206-
}
207-
208-
if (_worker.get_id() != std::thread::id())
204+
if (_jobsystem->IsKeepAliveEnabled())
209205
{
210-
if (_worker.joinable())
211-
_worker.join();
212-
else
213-
_worker.detach();
214-
}
215-
216-
_shutdownRequested.store(false);
217-
Active.store(true);
218-
_worker = std::thread(
219-
[this](JobSystemWorker* worker)
206+
if (IsActive() || _shutdownRequested.load())
220207
{
221-
std::unique_lock ul(_isRunningMutex);
222-
_isRunning.store(true);
223-
224-
worker->KeepAliveLoop();
225-
_isRunning.store(false);
226-
Active.store(false);
227-
},
228-
this);
229-
#else
230-
if (!_shutdownRequested.load())
231-
{
232-
Active.store(true);
233-
if (_worker.joinable())
208+
_modifyingThread.unlock();
209+
return;
210+
}
211+
212+
if (_worker.get_id() != std::thread::id())
234213
{
235-
_worker.join();
214+
if (_worker.joinable())
215+
_worker.join();
216+
else
217+
_worker.detach();
236218
}
219+
220+
_shutdownRequested.store(false);
221+
Active.store(true);
237222
_worker = std::thread(
238223
[this](JobSystemWorker* worker)
239224
{
240-
const std::unique_lock ul(_isRunningMutex);
225+
std::unique_lock ul(_isRunningMutex);
241226
_isRunning.store(true);
242227

243228
worker->KeepAliveLoop();
@@ -246,7 +231,28 @@ namespace JbSystem
246231
},
247232
this);
248233
}
249-
#endif
234+
else
235+
{
236+
if (!_shutdownRequested.load())
237+
{
238+
Active.store(true);
239+
if (_worker.joinable())
240+
{
241+
_worker.detach();
242+
}
243+
_worker = std::thread(
244+
[this](JobSystemWorker* worker)
245+
{
246+
const std::unique_lock ul(_isRunningMutex);
247+
_isRunning.store(true);
248+
249+
worker->KeepAliveLoop();
250+
_isRunning.store(false);
251+
Active.store(false);
252+
},
253+
this);
254+
}
255+
}
250256

251257
_modifyingThread.unlock();
252258
_jobsystem->OptimizePerformance(); // Determin best scaling options

0 commit comments

Comments
 (0)