wip: don't lock command mutex for each mass/spring in FillGraph()

This commit is contained in:
2026-02-24 02:05:44 +01:00
parent 550970f8a1
commit 0e6bb132c8
4 changed files with 26 additions and 6 deletions

View File

@ -30,7 +30,7 @@ constexpr float ROT_SPEED = 1.0;
constexpr float CAMERA_SMOOTH_SPEED = 15.0; constexpr float CAMERA_SMOOTH_SPEED = 15.0;
// Physics Engine // Physics Engine
constexpr float SIM_SPEED = 4.0; // How large each update should be constexpr float SIM_SPEED = 0.2; // How large each update should be
constexpr float TIMESTEP = 1.0 / 90; // Do 90 physics updates per second constexpr float TIMESTEP = 1.0 / 90; // Do 90 physics updates per second
constexpr float MASS = 1.0; // Mass spring system constexpr float MASS = 1.0; // Mass spring system
constexpr float SPRING_CONSTANT = 5.0; // Mass spring system constexpr float SPRING_CONSTANT = 5.0; // Mass spring system

View File

@ -188,6 +188,10 @@ public:
auto AddSpringCmd(const State &a, const State &b) -> void; auto AddSpringCmd(const State &a, const State &b) -> void;
auto ClearCmd() -> void; auto ClearCmd() -> void;
auto NonLockingAddMassCmd(const State &_state) -> void;
auto NonLockingAddSpringCmd(const State &a, const State &b) -> void;
}; };
// https://en.cppreference.com/w/cpp/utility/variant/visit // https://en.cppreference.com/w/cpp/utility/variant/visit

View File

@ -402,3 +402,12 @@ auto ThreadedPhysics::ClearCmd() -> void {
state.pending_commands.push(ClearGraph{}); state.pending_commands.push(ClearGraph{});
} }
} }
auto ThreadedPhysics::NonLockingAddMassCmd(const State &_state) -> void {
state.pending_commands.push(AddMass{_state});
}
auto ThreadedPhysics::NonLockingAddSpringCmd(const State &a, const State &b)
-> void {
state.pending_commands.push(AddSpring{a, b});
}

View File

@ -37,12 +37,19 @@ auto StateManager::FillGraph() -> void {
std::pair<std::unordered_set<State>, std::vector<std::pair<State, State>>> std::pair<std::unordered_set<State>, std::vector<std::pair<State, State>>>
closure = current_state.Closure(); closure = current_state.Closure();
{
std::lock_guard<LockableBase(std::mutex)> lock(physics.state.command_mtx);
for (const auto &state : closure.first) { for (const auto &state : closure.first) {
physics.AddMassCmd(state); physics.NonLockingAddMassCmd(state);
} }
for (const auto &[from, to] : closure.second) { for (const auto &[from, to] : closure.second) {
physics.AddSpringCmd(from, to); physics.NonLockingAddSpringCmd(from, to);
} }
}
// TODO: We have only dispatched the commands, the states won't be downloaded
// when calling this... Make FindWinningStates() another command?
FindWinningStates(); FindWinningStates();
} }