Skip to content

Commit 547c530

Browse files
committed
bug fix
1 parent f9dcba0 commit 547c530

4 files changed

Lines changed: 29 additions & 3 deletions

File tree

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"cSpell.words": [
33
"Cloudtoid",
44
"Diagnoser",
5+
"finalizer",
56
"Interprocess",
67
"Kaby",
78
"Onnx",

src/Interprocess/Queue/Subscriber.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ private unsafe bool TryDequeueImpl(
127127
var readOffset = Header->ReadOffset;
128128
var messageHeader = (MessageHeader*)Buffer.GetPointer(readOffset);
129129

130-
// was this message fully written by the publisher? if not, wait for the publisher to finish writting it
130+
// was this message fully written by the publisher? if not, wait for the publisher to finish writing it
131131
while (Interlocked.CompareExchange(
132132
ref messageHeader->State,
133133
MessageHeader.LockedToBeConsumedState,

src/Interprocess/Semaphore/MacOS/Interop.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,10 @@ internal static bool Wait(IntPtr handle, int millisecondsTimeout)
9696
}
9797
else
9898
{
99-
var start = DateTime.Now;
99+
var stopwatch = ValueStopwatch.StartNew();
100100
while (!TryWait(handle))
101101
{
102-
if ((DateTime.Now - start).Milliseconds > millisecondsTimeout)
102+
if (stopwatch.GetElapsedTime().TotalMilliseconds > millisecondsTimeout)
103103
return false;
104104

105105
Thread.Yield();

src/Interprocess/ValueStopwatch.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System.Diagnostics;
2+
3+
namespace Cloudtoid.Interprocess;
4+
5+
// Inspired by https://github.com/dotnet/aspnetcore/blob/main/src/Shared/ValueStopwatch/ValueStopwatch.cs
6+
internal readonly struct ValueStopwatch
7+
{
8+
private readonly long start;
9+
private ValueStopwatch(long start) => this.start = start;
10+
public readonly bool IsActive => start != 0;
11+
public static ValueStopwatch StartNew() => new(Stopwatch.GetTimestamp());
12+
13+
public readonly TimeSpan GetElapsedTime()
14+
{
15+
// 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.
16+
// So it being 0 is a clear indication of default(ValueStopwatch)
17+
if (!IsActive)
18+
{
19+
throw new InvalidOperationException(
20+
"An uninitialized, or 'default', ValueStopwatch cannot be used to get elapsed time.");
21+
}
22+
23+
return Stopwatch.GetElapsedTime(start);
24+
}
25+
}

0 commit comments

Comments
 (0)