-
Notifications
You must be signed in to change notification settings - Fork 34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add CVar to disable pow3r parallel processing #124
Changes from all commits
35d48cf
fd2545f
108374a
1a39b5c
ab14630
3ea1539
e65c240
36341b5
48e362b
5bbd6b4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,9 +3,11 @@ | |
using Content.Server.Power.Components; | ||
using Content.Server.Power.NodeGroups; | ||
using Content.Server.Power.Pow3r; | ||
using Content.Shared.CCVar; | ||
using Content.Shared.Power; | ||
using JetBrains.Annotations; | ||
using Robust.Server.GameObjects; | ||
using Robust.Shared.Configuration; | ||
using Robust.Shared.Threading; | ||
|
||
namespace Content.Server.Power.EntitySystems | ||
|
@@ -18,19 +20,21 @@ public sealed class PowerNetSystem : EntitySystem | |
{ | ||
[Dependency] private readonly AppearanceSystem _appearance = default!; | ||
[Dependency] private readonly PowerNetConnectorSystem _powerNetConnector = default!; | ||
[Dependency] private readonly IConfigurationManager _cfg = default!; | ||
[Dependency] private readonly IParallelManager _parMan = default!; | ||
|
||
private readonly PowerState _powerState = new(); | ||
private readonly HashSet<PowerNet> _powerNetReconnectQueue = new(); | ||
private readonly HashSet<ApcNet> _apcNetReconnectQueue = new(); | ||
|
||
private readonly BatteryRampPegSolver _solver = new(); | ||
private BatteryRampPegSolver _solver = new(); | ||
|
||
public override void Initialize() | ||
{ | ||
base.Initialize(); | ||
|
||
UpdatesAfter.Add(typeof(NodeGroupSystem)); | ||
_solver = new(_cfg.GetCVar(CCVars.DebugPow3rDisableParallel)); | ||
|
||
SubscribeLocalEvent<ApcPowerReceiverComponent, ComponentInit>(ApcPowerReceiverInit); | ||
SubscribeLocalEvent<ApcPowerReceiverComponent, ComponentShutdown>(ApcPowerReceiverShutdown); | ||
|
@@ -52,6 +56,13 @@ public override void Initialize() | |
SubscribeLocalEvent<PowerSupplierComponent, ComponentShutdown>(PowerSupplierShutdown); | ||
SubscribeLocalEvent<PowerSupplierComponent, EntityPausedEvent>(PowerSupplierPaused); | ||
SubscribeLocalEvent<PowerSupplierComponent, EntityUnpausedEvent>(PowerSupplierUnpaused); | ||
|
||
Subs.CVar(_cfg, CCVars.DebugPow3rDisableParallel, DebugPow3rDisableParallelChanged); | ||
} | ||
|
||
private void DebugPow3rDisableParallelChanged(bool val) | ||
{ | ||
_solver = new(val); | ||
Comment on lines
+63
to
+65
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add thread safety and error handling to configuration change handler The current implementation might be unsafe if the solver is being used when reconfiguration occurs. Consider adding thread synchronization and error handling. - private void DebugPow3rDisableParallelChanged(bool val)
- {
- _solver = new(val);
- }
+ private readonly object _solverLock = new object();
+
+ private void DebugPow3rDisableParallelChanged(bool val)
+ {
+ try
+ {
+ lock (_solverLock)
+ {
+ _solver = new(val);
+ }
+ }
+ catch (Exception ex)
+ {
+ Logger.Error($"Failed to reconfigure solver: {ex}");
+ // Consider reverting the configuration value
+ _cfg.SetCVar(CCVars.DebugPow3rDisableParallel, !val);
+ }
+ } Also, ensure that the Update method uses the same lock: public override void Update(float frameTime)
{
base.Update(frameTime);
ReconnectNetworks();
// Synchronize batteries
RaiseLocalEvent(new NetworkBatteryPreSync());
// Run power solver.
- _solver.Tick(frameTime, _powerState, _parMan);
+ lock (_solverLock)
+ {
+ _solver.Tick(frameTime, _powerState, _parMan);
+ }
|
||
} | ||
|
||
private void ApcPowerReceiverInit(EntityUid uid, ApcPowerReceiverComponent component, ComponentInit args) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Optimize solver recreation and add error handling
The configuration change handler could benefit from these improvements: