Skip to content

Commit

Permalink
Merge pull request #1578 from heinezen/fix/threading-race-condition
Browse files Browse the repository at this point in the history
Prevent engine loop from being optimized out in release builds
  • Loading branch information
TheJJ authored Oct 17, 2023
2 parents a8d033f + 232a23b commit 80fd9cc
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
6 changes: 5 additions & 1 deletion configure
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ def build_type(args):
ret = 'Debug'
elif mode == 'release':
ret = 'Release'
elif mode == 'relwithdebinfo':
ret = 'RelWithDebInfo'
elif mode == 'minsizerel':
ret = 'MinSizeRel'

return {
"build_type": ret
Expand Down Expand Up @@ -355,7 +359,7 @@ def parse_args():
formatter_class=argparse.ArgumentDefaultsHelpFormatter)

cli.add_argument("--mode", "-m",
choices=["debug", "release"],
choices=["debug", "release", "relwithdebinfo", "minsizerel"],
default=getenv("BUILDMODE", default="debug"),
help="controls cmake build mode")
cli.add_argument("--optimize", "-O",
Expand Down
27 changes: 14 additions & 13 deletions libopenage/engine/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,32 +32,28 @@ Engine::Engine(mode mode,
// time loop
this->time_loop = std::make_shared<time::TimeLoop>();

// game simulation
// this is run in the main thread
this->simulation = std::make_shared<gamestate::GameSimulation>(this->root_dir,
this->cvar_manager,
this->time_loop);
this->simulation->set_modpacks(mods);

// presenter (optional)
if (this->run_mode == mode::FULL) {
this->presenter = std::make_shared<presenter::Presenter>(this->root_dir,
this->simulation,
this->time_loop);
}

// spawn thread to run time loop
this->threads.emplace_back([&]() {
this->time_loop->run();

this->time_loop.reset();
});
this->threads.emplace_back([&]() {
this->simulation->run();

this->simulation.reset();

if (this->run_mode != mode::FULL) {
this->running = false;
}
});

// if presenter is used, run it in a separate thread
if (this->run_mode == mode::FULL) {
this->threads.emplace_back([&]() {
this->presenter->run(debug_graphics);
Expand All @@ -69,13 +65,18 @@ Engine::Engine(mode mode,
});
}

log::log(INFO << "Created " << this->threads.size() << " threads"
<< " (" << std::jthread::hardware_concurrency() << " available)");
log::log(INFO << "Using " << this->threads.size() + 1 << " threads "
<< "(" << std::jthread::hardware_concurrency() << " available)");
}

void Engine::loop() {
while (this->running) {
// TODO
// Run the main game simulation loop:
this->simulation->run();

// After stopping, clean up the simulation
this->simulation.reset();
if (this->run_mode != mode::FULL) {
this->running = false;
}
}

Expand Down

0 comments on commit 80fd9cc

Please sign in to comment.