Skip to content

Commit f4f66be

Browse files
authored
eng: prefer Volatile.Read over Thread.VolatileRead (#2960)
1 parent 862a70e commit f4f66be

File tree

9 files changed

+36
-36
lines changed

9 files changed

+36
-36
lines changed

src/StackExchange.Redis/ConnectionMultiplexer.Debug.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace StackExchange.Redis;
55
public partial class ConnectionMultiplexer
66
{
77
private static int _collectedWithoutDispose;
8-
internal static int CollectedWithoutDispose => Thread.VolatileRead(ref _collectedWithoutDispose);
8+
internal static int CollectedWithoutDispose => Volatile.Read(ref _collectedWithoutDispose);
99

1010
/// <summary>
1111
/// Invoked by the garbage collector.

src/StackExchange.Redis/ConnectionMultiplexer.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,17 @@ public sealed partial class ConnectionMultiplexer : IInternalConnectionMultiplex
5858

5959
private int lastReconfigiureTicks = Environment.TickCount;
6060
internal long LastReconfigureSecondsAgo =>
61-
unchecked(Environment.TickCount - Thread.VolatileRead(ref lastReconfigiureTicks)) / 1000;
61+
unchecked(Environment.TickCount - Volatile.Read(ref lastReconfigiureTicks)) / 1000;
6262

6363
private int _activeHeartbeatErrors, lastHeartbeatTicks;
6464
internal long LastHeartbeatSecondsAgo =>
6565
pulse is null
6666
? -1
67-
: unchecked(Environment.TickCount - Thread.VolatileRead(ref lastHeartbeatTicks)) / 1000;
67+
: unchecked(Environment.TickCount - Volatile.Read(ref lastHeartbeatTicks)) / 1000;
6868

6969
private static int lastGlobalHeartbeatTicks = Environment.TickCount;
7070
internal static long LastGlobalHeartbeatSecondsAgo =>
71-
unchecked(Environment.TickCount - Thread.VolatileRead(ref lastGlobalHeartbeatTicks)) / 1000;
71+
unchecked(Environment.TickCount - Volatile.Read(ref lastGlobalHeartbeatTicks)) / 1000;
7272

7373
/// <inheritdoc cref="ConfigurationOptions.IncludeDetailInExceptions"/>
7474
[Obsolete($"Please use {nameof(ConfigurationOptions)}.{nameof(ConfigurationOptions.IncludeDetailInExceptions)} instead - this will be removed in 3.0.")]

src/StackExchange.Redis/PhysicalBridge.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ internal void OnFullyEstablished(PhysicalConnection connection, string source)
555555

556556
private bool DueForConnectRetry()
557557
{
558-
int connectTimeMilliseconds = unchecked(Environment.TickCount - Thread.VolatileRead(ref connectStartTicks));
558+
int connectTimeMilliseconds = unchecked(Environment.TickCount - Volatile.Read(ref connectStartTicks));
559559
return Multiplexer.RawConfig.ReconnectRetryPolicy.ShouldRetry(Interlocked.Read(ref connectTimeoutRetryCount), connectTimeMilliseconds);
560560
}
561561

src/StackExchange.Redis/PhysicalConnection.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,8 @@ private enum ReadMode : byte
261261
private readonly WeakReference _bridge;
262262
public PhysicalBridge? BridgeCouldBeNull => (PhysicalBridge?)_bridge.Target;
263263

264-
public long LastReadSecondsAgo => unchecked(Environment.TickCount - Thread.VolatileRead(ref lastReadTickCount)) / 1000;
265-
public long LastWriteSecondsAgo => unchecked(Environment.TickCount - Thread.VolatileRead(ref lastWriteTickCount)) / 1000;
264+
public long LastReadSecondsAgo => unchecked(Environment.TickCount - Volatile.Read(ref lastReadTickCount)) / 1000;
265+
public long LastWriteSecondsAgo => unchecked(Environment.TickCount - Volatile.Read(ref lastWriteTickCount)) / 1000;
266266

267267
private bool IncludeDetailInExceptions => BridgeCouldBeNull?.Multiplexer.RawConfig.IncludeDetailInExceptions ?? false;
268268

@@ -418,8 +418,8 @@ public void RecordConnectionFailed(
418418

419419
if (isCurrent && Interlocked.CompareExchange(ref failureReported, 1, 0) == 0)
420420
{
421-
int now = Environment.TickCount, lastRead = Thread.VolatileRead(ref lastReadTickCount), lastWrite = Thread.VolatileRead(ref lastWriteTickCount),
422-
lastBeat = Thread.VolatileRead(ref lastBeatTickCount);
421+
int now = Environment.TickCount, lastRead = Volatile.Read(ref lastReadTickCount), lastWrite = Volatile.Read(ref lastWriteTickCount),
422+
lastBeat = Volatile.Read(ref lastBeatTickCount);
423423

424424
int unansweredWriteTime = 0;
425425
lock (_writtenAwaitingResponse)
@@ -434,7 +434,7 @@ public void RecordConnectionFailed(
434434
var exMessage = new StringBuilder(failureType.ToString());
435435

436436
// If the reason for the shutdown was we asked for the socket to die, don't log it as an error (only informational)
437-
weAskedForThis = Thread.VolatileRead(ref clientSentQuit) != 0;
437+
weAskedForThis = Volatile.Read(ref clientSentQuit) != 0;
438438

439439
var pipe = connectingPipe ?? _ioPipe;
440440
if (pipe is SocketConnection sc)

src/StackExchange.Redis/Profiling/ProfilingSession.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ internal void Add(ProfiledCommand command)
2424
{
2525
if (command == null) return;
2626

27-
object? cur = Thread.VolatileRead(ref _untypedHead);
27+
object? cur = Volatile.Read(ref _untypedHead);
2828
while (true)
2929
{
3030
command.NextElement = (ProfiledCommand?)cur;

src/StackExchange.Redis/ServerEndPoint.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,7 @@ internal void OnFullyEstablished(PhysicalConnection connection, string source)
719719
}
720720

721721
internal int LastInfoReplicationCheckSecondsAgo =>
722-
unchecked(Environment.TickCount - Thread.VolatileRead(ref lastInfoReplicationCheckTicks)) / 1000;
722+
unchecked(Environment.TickCount - Volatile.Read(ref lastInfoReplicationCheckTicks)) / 1000;
723723

724724
private EndPoint? primaryEndPoint;
725725
public EndPoint? PrimaryEndPoint

tests/StackExchange.Redis.Tests/FailoverTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ public async Task SubscriptionsSurviveConnectionFailureAsync()
217217
await sub.PingAsync();
218218
await Task.Delay(200).ConfigureAwait(false);
219219

220-
var counter1 = Thread.VolatileRead(ref counter);
220+
var counter1 = Volatile.Read(ref counter);
221221
Log($"Expecting 1 message, got {counter1}");
222222
Assert.Equal(1, counter1);
223223

@@ -274,9 +274,9 @@ public async Task SubscriptionsSurviveConnectionFailureAsync()
274274

275275
// Give it a few seconds to get our messages
276276
Log("Waiting for 2 messages");
277-
await UntilConditionAsync(TimeSpan.FromSeconds(5), () => Thread.VolatileRead(ref counter) == 2);
277+
await UntilConditionAsync(TimeSpan.FromSeconds(5), () => Volatile.Read(ref counter) == 2);
278278

279-
var counter2 = Thread.VolatileRead(ref counter);
279+
var counter2 = Volatile.Read(ref counter);
280280
Log($"Expecting 2 messages, got {counter2}");
281281
Assert.Equal(2, counter2);
282282

tests/StackExchange.Redis.Tests/PubSubTests.cs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,19 @@ public async Task ExplicitPublishMode()
3030
#pragma warning restore CS0618
3131
await UntilConditionAsync(
3232
TimeSpan.FromSeconds(10),
33-
() => Thread.VolatileRead(ref b) == 1
34-
&& Thread.VolatileRead(ref c) == 1
35-
&& Thread.VolatileRead(ref d) == 1);
36-
Assert.Equal(0, Thread.VolatileRead(ref a));
37-
Assert.Equal(1, Thread.VolatileRead(ref b));
38-
Assert.Equal(1, Thread.VolatileRead(ref c));
39-
Assert.Equal(1, Thread.VolatileRead(ref d));
33+
() => Volatile.Read(ref b) == 1
34+
&& Volatile.Read(ref c) == 1
35+
&& Volatile.Read(ref d) == 1);
36+
Assert.Equal(0, Volatile.Read(ref a));
37+
Assert.Equal(1, Volatile.Read(ref b));
38+
Assert.Equal(1, Volatile.Read(ref c));
39+
Assert.Equal(1, Volatile.Read(ref d));
4040

4141
#pragma warning disable CS0618
4242
pub.Publish("*bcd", "efg");
4343
#pragma warning restore CS0618
44-
await UntilConditionAsync(TimeSpan.FromSeconds(10), () => Thread.VolatileRead(ref a) == 1);
45-
Assert.Equal(1, Thread.VolatileRead(ref a));
44+
await UntilConditionAsync(TimeSpan.FromSeconds(10), () => Volatile.Read(ref a) == 1);
45+
Assert.Equal(1, Volatile.Read(ref a));
4646
}
4747

4848
[Theory]
@@ -86,7 +86,7 @@ public async Task TestBasicPubSub(string? channelPrefix, bool wildCard, string b
8686
{
8787
Assert.Empty(received);
8888
}
89-
Assert.Equal(0, Thread.VolatileRead(ref secondHandler));
89+
Assert.Equal(0, Volatile.Read(ref secondHandler));
9090
#pragma warning disable CS0618
9191
var count = sub.Publish(pubChannel, "def");
9292
#pragma warning restore CS0618
@@ -99,8 +99,8 @@ public async Task TestBasicPubSub(string? channelPrefix, bool wildCard, string b
9999
Assert.Single(received);
100100
}
101101
// Give handler firing a moment
102-
await UntilConditionAsync(TimeSpan.FromSeconds(2), () => Thread.VolatileRead(ref secondHandler) == 1);
103-
Assert.Equal(1, Thread.VolatileRead(ref secondHandler));
102+
await UntilConditionAsync(TimeSpan.FromSeconds(2), () => Volatile.Read(ref secondHandler) == 1);
103+
Assert.Equal(1, Volatile.Read(ref secondHandler));
104104

105105
// unsubscribe from first; should still see second
106106
#pragma warning disable CS0618
@@ -113,9 +113,9 @@ public async Task TestBasicPubSub(string? channelPrefix, bool wildCard, string b
113113
Assert.Single(received);
114114
}
115115

116-
await UntilConditionAsync(TimeSpan.FromSeconds(2), () => Thread.VolatileRead(ref secondHandler) == 2);
116+
await UntilConditionAsync(TimeSpan.FromSeconds(2), () => Volatile.Read(ref secondHandler) == 2);
117117

118-
var secondHandlerCount = Thread.VolatileRead(ref secondHandler);
118+
var secondHandlerCount = Volatile.Read(ref secondHandler);
119119
Log("Expecting 2 from second handler, got: " + secondHandlerCount);
120120
Assert.Equal(2, secondHandlerCount);
121121
Assert.Equal(1, count);
@@ -130,7 +130,7 @@ public async Task TestBasicPubSub(string? channelPrefix, bool wildCard, string b
130130
{
131131
Assert.Single(received);
132132
}
133-
secondHandlerCount = Thread.VolatileRead(ref secondHandler);
133+
secondHandlerCount = Volatile.Read(ref secondHandler);
134134
Log("Expecting 2 from second handler, got: " + secondHandlerCount);
135135
Assert.Equal(2, secondHandlerCount);
136136
Assert.Equal(0, count);
@@ -170,7 +170,7 @@ public async Task TestBasicPubSubFireAndForget()
170170
{
171171
Assert.Empty(received);
172172
}
173-
Assert.Equal(0, Thread.VolatileRead(ref secondHandler));
173+
Assert.Equal(0, Volatile.Read(ref secondHandler));
174174
await PingAsync(pub, sub).ForAwait();
175175
var count = sub.Publish(key, "def", CommandFlags.FireAndForget);
176176
await PingAsync(pub, sub).ForAwait();
@@ -182,7 +182,7 @@ public async Task TestBasicPubSubFireAndForget()
182182
{
183183
Assert.Single(received);
184184
}
185-
Assert.Equal(1, Thread.VolatileRead(ref secondHandler));
185+
Assert.Equal(1, Volatile.Read(ref secondHandler));
186186

187187
sub.Unsubscribe(key);
188188
count = sub.Publish(key, "ghi", CommandFlags.FireAndForget);
@@ -241,7 +241,7 @@ public async Task TestPatternPubSub()
241241
{
242242
Assert.Empty(received);
243243
}
244-
Assert.Equal(0, Thread.VolatileRead(ref secondHandler));
244+
Assert.Equal(0, Volatile.Read(ref secondHandler));
245245

246246
await PingAsync(pub, sub).ForAwait();
247247
var count = sub.Publish(RedisChannel.Literal("abc"), "def");
@@ -254,8 +254,8 @@ public async Task TestPatternPubSub()
254254
}
255255

256256
// Give reception a bit, the handler could be delayed under load
257-
await UntilConditionAsync(TimeSpan.FromSeconds(2), () => Thread.VolatileRead(ref secondHandler) == 1);
258-
Assert.Equal(1, Thread.VolatileRead(ref secondHandler));
257+
await UntilConditionAsync(TimeSpan.FromSeconds(2), () => Volatile.Read(ref secondHandler) == 1);
258+
Assert.Equal(1, Volatile.Read(ref secondHandler));
259259

260260
#pragma warning disable CS0618
261261
sub.Unsubscribe("a*c");

tests/StackExchange.Redis.Tests/SyncContextTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public MySyncContext(TextWriter log)
118118
_log = log;
119119
SetSynchronizationContext(this);
120120
}
121-
public int OpCount => Thread.VolatileRead(ref _opCount);
121+
public int OpCount => Volatile.Read(ref _opCount);
122122
private int _opCount;
123123
private void Incr() => Interlocked.Increment(ref _opCount);
124124

0 commit comments

Comments
 (0)