Skip to content

Commit

Permalink
bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
prezaei committed Jan 12, 2025
1 parent f9dcba0 commit 547c530
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"cSpell.words": [
"Cloudtoid",
"Diagnoser",
"finalizer",
"Interprocess",
"Kaby",
"Onnx",
Expand Down
2 changes: 1 addition & 1 deletion src/Interprocess/Queue/Subscriber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ private unsafe bool TryDequeueImpl(
var readOffset = Header->ReadOffset;
var messageHeader = (MessageHeader*)Buffer.GetPointer(readOffset);

// was this message fully written by the publisher? if not, wait for the publisher to finish writting it
// was this message fully written by the publisher? if not, wait for the publisher to finish writing it
while (Interlocked.CompareExchange(
ref messageHeader->State,
MessageHeader.LockedToBeConsumedState,
Expand Down
4 changes: 2 additions & 2 deletions src/Interprocess/Semaphore/MacOS/Interop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,10 @@ internal static bool Wait(IntPtr handle, int millisecondsTimeout)
}
else
{
var start = DateTime.Now;
var stopwatch = ValueStopwatch.StartNew();
while (!TryWait(handle))
{
if ((DateTime.Now - start).Milliseconds > millisecondsTimeout)
if (stopwatch.GetElapsedTime().TotalMilliseconds > millisecondsTimeout)
return false;

Thread.Yield();
Expand Down
25 changes: 25 additions & 0 deletions src/Interprocess/ValueStopwatch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System.Diagnostics;

namespace Cloudtoid.Interprocess;

// Inspired by https://github.com/dotnet/aspnetcore/blob/main/src/Shared/ValueStopwatch/ValueStopwatch.cs
internal readonly struct ValueStopwatch
{
private readonly long start;
private ValueStopwatch(long start) => this.start = start;
public readonly bool IsActive => start != 0;
public static ValueStopwatch StartNew() => new(Stopwatch.GetTimestamp());

public readonly TimeSpan GetElapsedTime()
{
// Start timestamp can't be zero in an initialized ValueStopwatch. It would have to be literally the first thing executed when the machine boots to be 0.
// So it being 0 is a clear indication of default(ValueStopwatch)
if (!IsActive)
{
throw new InvalidOperationException(
"An uninitialized, or 'default', ValueStopwatch cannot be used to get elapsed time.");
}

return Stopwatch.GetElapsedTime(start);
}
}

0 comments on commit 547c530

Please sign in to comment.