Skip to content

Commit

Permalink
Core: Wait: Switch to ManualResetEventSlim from AutoResetEvent for th…
Browse files Browse the repository at this point in the history
…read syncronization.
  • Loading branch information
Xian55 committed Sep 4, 2024
1 parent 37639b2 commit 9d5de4b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
4 changes: 2 additions & 2 deletions Core/Addon/AddonReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Core;
public sealed class AddonReader : IAddonReader
{
private readonly IAddonDataProvider reader;
private readonly AutoResetEvent resetEvent;
private readonly ManualResetEventSlim resetEvent;

private readonly PlayerReader playerReader;
private readonly CreatureDB creatureDb;
Expand All @@ -33,7 +33,7 @@ public sealed class AddonReader : IAddonReader
public double AvgUpdateLatency { private set; get; }

public AddonReader(IAddonDataProvider reader,
PlayerReader playerReader, AutoResetEvent resetEvent,
PlayerReader playerReader, ManualResetEventSlim resetEvent,
CreatureDB creatureDb,
CombatLog combatLog,
DataFrame[] frames,
Expand Down
2 changes: 1 addition & 1 deletion Core/DependencyInjection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ public static IServiceCollection AddCoreNormal(

public static IServiceCollection AddCoreBase(this IServiceCollection s)
{
s.AddSingleton<AutoResetEvent>(x => new(false));
s.AddSingleton<ManualResetEventSlim>(x => new(false));
s.AddSingleton<Wait>();

s.AddSingleton<StartupClientVersion>();
Expand Down
18 changes: 13 additions & 5 deletions Core/GoalsComponent/Wait.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,31 @@ namespace Core;

public sealed class Wait
{
private readonly AutoResetEvent globalTime;
private readonly ManualResetEventSlim globalTime;
private readonly CancellationToken token;

public Wait(AutoResetEvent globalTime, CancellationTokenSource cts)
public Wait(ManualResetEventSlim globalTime, CancellationTokenSource cts)
{
this.globalTime = globalTime;
this.token = cts.Token;
}

public void Update()
{
globalTime.WaitOne();
globalTime.Wait();
globalTime.Reset();
}

public bool Update(int timeout)
public bool Update(int timeoutMs)
{
return globalTime.WaitOne(timeout);
bool result = globalTime.Wait(timeoutMs);
if (!result)
{
return result;
}

globalTime.Reset();
return result;
}

public void Fixed(int durationMs)
Expand Down

0 comments on commit 9d5de4b

Please sign in to comment.