Skip to content

Commit 988907f

Browse files
authored
Insert Thread.Yield() in some spinloops (#392)
1 parent 8aa4422 commit 988907f

File tree

3 files changed

+8
-7
lines changed

3 files changed

+8
-7
lines changed

cs/src/core/Allocator/AllocatorBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -845,7 +845,7 @@ public void ShiftBeginAddress(long newBeginAddress)
845845
epoch.Suspend();
846846

847847
// Wait for flush to complete
848-
while (FlushedUntilAddress < newBeginAddress) ;
848+
while (FlushedUntilAddress < newBeginAddress) Thread.Yield();
849849

850850
// Then shift head address
851851
var h = Utility.MonotonicUpdate(ref HeadAddress, newBeginAddress, out long old);

cs/src/core/Index/FASTER/LogAccessor.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Reflection;
6+
using System.Threading;
67

78
namespace FASTER.core
89
{
@@ -82,7 +83,7 @@ public void ShiftHeadAddress(long newHeadAddress, bool wait)
8283
fht.epoch.Resume();
8384
allocator.ShiftHeadAddress(newHeadAddress);
8485
fht.epoch.Suspend();
85-
while (wait && allocator.SafeHeadAddress < newHeadAddress) ;
86+
while (wait && allocator.SafeHeadAddress < newHeadAddress) Thread.Yield();
8687
}
8788
else
8889
{
@@ -137,7 +138,7 @@ public void ShiftReadOnlyAddress(long newReadOnlyAddress, bool wait)
137138
fht.epoch.Suspend();
138139

139140
// Wait for flush to complete
140-
while (wait && allocator.FlushedUntilAddress < newReadOnlyAddress) ;
141+
while (wait && allocator.FlushedUntilAddress < newReadOnlyAddress) Thread.Yield();
141142
}
142143
else
143144
{

cs/src/core/Index/FasterLog/FasterLog.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ public void WaitForCommit(long untilAddress = 0)
414414
var tailAddress = untilAddress;
415415
if (tailAddress == 0) tailAddress = allocator.GetTailAddress();
416416

417-
while (CommittedUntilAddress < tailAddress) ;
417+
while (CommittedUntilAddress < tailAddress) Thread.Yield();
418418
}
419419

420420
/// <summary>
@@ -546,7 +546,7 @@ public long EnqueueAndWaitForCommit(byte[] entry)
546546
{
547547
long logicalAddress;
548548
while (!TryEnqueue(entry, out logicalAddress)) ;
549-
while (CommittedUntilAddress < logicalAddress + 1) ;
549+
while (CommittedUntilAddress < logicalAddress + 1) Thread.Yield();
550550
return logicalAddress;
551551
}
552552

@@ -560,7 +560,7 @@ public long EnqueueAndWaitForCommit(ReadOnlySpan<byte> entry)
560560
{
561561
long logicalAddress;
562562
while (!TryEnqueue(entry, out logicalAddress)) ;
563-
while (CommittedUntilAddress < logicalAddress + 1) ;
563+
while (CommittedUntilAddress < logicalAddress + 1) Thread.Yield();
564564
return logicalAddress;
565565
}
566566

@@ -574,7 +574,7 @@ public long EnqueueAndWaitForCommit(IReadOnlySpanBatch readOnlySpanBatch)
574574
{
575575
long logicalAddress;
576576
while (!TryEnqueue(readOnlySpanBatch, out logicalAddress)) ;
577-
while (CommittedUntilAddress < logicalAddress + 1) ;
577+
while (CommittedUntilAddress < logicalAddress + 1) Thread.Yield();
578578
return logicalAddress;
579579
}
580580

0 commit comments

Comments
 (0)