Skip to content

Commit

Permalink
Removed arbritrary wait => Now use SpinWait
Browse files Browse the repository at this point in the history
  • Loading branch information
Fernando Cerqueira committed Dec 5, 2023
1 parent d213e78 commit c7c6978
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/internal/RingBufferManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,8 @@ public RingBufferValue<T> Accquire(CancellationToken? cancellation = null)
return new RingBufferValue<T>(Name, TimeSpan.Zero, false, default, null);
}

var spin = new SpinWait();

if (_recoveryBuffer)
{
int fullavailable;
Expand All @@ -233,7 +235,7 @@ public RingBufferValue<T> Accquire(CancellationToken? cancellation = null)
}
if (fullavailable != _currentCapacity)
{
localcancellation.WaitHandle.WaitOne(5);
spin.SpinOnce();
return new RingBufferValue<T>(Name, TimeSpan.Zero, false, default, null);
}
lock (_lockAccquire)
Expand Down Expand Up @@ -332,7 +334,7 @@ public RingBufferValue<T> Accquire(CancellationToken? cancellation = null)
_blockexceptionsBuffer.Add(new RingBufferException(Name, $"Accquire timeout {sw.Elapsed}"), _managertoken.Token);
break;
}
localcancellation.WaitHandle.WaitOne(5);
spin.SpinOnce();
}
}
//not ok
Expand Down Expand Up @@ -508,6 +510,7 @@ private void Startup(TimeSpan timeoutfullcapacity)
{
if (_healthHandler != null)
{
var spin = new SpinWait();
while (!_managertoken.IsCancellationRequested)
{
DateTime oldacquisition;
Expand Down Expand Up @@ -551,7 +554,7 @@ private void Startup(TimeSpan timeoutfullcapacity)
}
WriteLogInfo(DateTime.Now, $"{Name} Internal Buffer Health done");
}
_managertoken.Token.WaitHandle.WaitOne(100);
spin.SpinOnce();
}
WriteLogInfo(DateTime.Now, $"{Name} Buffer Health Thread Stoped");
}
Expand Down Expand Up @@ -782,9 +785,10 @@ private void Startup(TimeSpan timeoutfullcapacity)
{
if (ScaleCapacity && UserScale == ScaleMode.Automatic)
{
var spin = new SpinWait();
while (!_managertoken.IsCancellationRequested && !_WarmupComplete)
{
_managertoken.Token.WaitHandle.WaitOne(5);
spin.SpinOnce();
}
}
while (ScaleCapacity && UserScale == ScaleMode.Automatic && !_managertoken.IsCancellationRequested)
Expand Down Expand Up @@ -937,9 +941,10 @@ private void Startup(TimeSpan timeoutfullcapacity)
_blockrenewBuffer.Add(new RingBufferValue<T>(ScaleType.ToDefaultCapacity));

warmupcts.CancelAfter(timeoutfullcapacity);
var spin = new SpinWait();
while (!warmupcts.Token.IsCancellationRequested && _counterBuffer != Capacity)
{
warmupcts.Token.WaitHandle.WaitOne(5);
spin.SpinOnce();
}

WriteLogInfo(DateTime.Now, $"{Name} Warmup complete with {_counterBuffer} items of {Capacity}");
Expand Down

0 comments on commit c7c6978

Please sign in to comment.