Improve Interlocked pattern on IsRunning
Related
MODIFY
WalletWasabi/Services/CcjClient.cs
WalletWasabi/Services/CcjClient.cs
@@ -144,10 +144,7 @@ namespace WalletWasabi.Services
await DequeueSpentCoinsFromMixNoLockAsync();
// If stop was requested return.
- if (IsRunning == false)
- {
- return;
- }
+ if (!IsRunning) return;
// if mixing >= connConf
if (State.GetActivelyMixingRounds().Any())
@@ -187,10 +184,7 @@ namespace WalletWasabi.Services
}
finally
{
- if (IsStopping)
- {
- Interlocked.Exchange(ref _running, 3);
- }
+ Interlocked.CompareExchange(ref _running, 3, 2); // If IsStopping, make it stopped.
}
});
}
@@ -1028,10 +1022,7 @@ namespace WalletWasabi.Services
{
Synchronizer.ResponseArrived -= Synchronizer_ResponseArrivedAsync;
- if (IsRunning)
- {
- Interlocked.Exchange(ref _running, 2);
- }
+ Interlocked.CompareExchange(ref _running, 2, 1); // If running, make it stopping.
Cancel?.Cancel();
while (IsStopping)
{

MODIFY
WalletWasabi/Services/ConfigWatcher.cs
WalletWasabi/Services/ConfigWatcher.cs
@@ -66,20 +66,14 @@ namespace WalletWasabi.Services
}
finally
{
- if (IsStopping)
- {
- Interlocked.Exchange(ref _running, 3);
- }
+ Interlocked.CompareExchange(ref _running, 3, 2); // If IsStopping, make it stopped.
}
});
}
public async Task StopAsync()
{
- if (IsRunning)
- {
- Interlocked.Exchange(ref _running, 2);
- }
+ Interlocked.CompareExchange(ref _running, 2, 1); // If running, make it stopping.
Stop?.Cancel();
while (IsStopping)
{

MODIFY
WalletWasabi/Services/IndexBuilderService.cs
WalletWasabi/Services/IndexBuilderService.cs
@@ -367,10 +367,7 @@ namespace WalletWasabi.Services
}
finally
{
- if (IsStopping)
- {
- Interlocked.Exchange(ref _running, 3);
- }
+ Interlocked.CompareExchange(ref _running, 3, 2); // If IsStopping, make it stopped.
Interlocked.Decrement(ref _runner);
}
});
@@ -474,10 +471,7 @@ namespace WalletWasabi.Services
TrustedNodeNotifyingBehavior.BlockInv -= TrustedNodeNotifyingBehavior_BlockInv;
}
- if (IsRunning)
- {
- Interlocked.Exchange(ref _running, 2);
- }
+ Interlocked.CompareExchange(ref _running, 2, 1); // If running, make it stopping.
while (IsStopping)
{
await Task.Delay(50);

MODIFY
WalletWasabi/Services/UpdateChecker.cs
WalletWasabi/Services/UpdateChecker.cs
@@ -84,10 +84,7 @@ namespace WalletWasabi.Services
}
finally
{
- if (IsStopping)
- {
- Interlocked.Exchange(ref _running, 3);
- }
+ Interlocked.CompareExchange(ref _running, 3, 2); // If IsStopping, make it stopped.
}
});
}
@@ -102,10 +99,7 @@ namespace WalletWasabi.Services
{
if (disposing)
{
- if (IsRunning)
- {
- Interlocked.Exchange(ref _running, 2);
- }
+ Interlocked.CompareExchange(ref _running, 2, 1); // If running, make it stopping.
Stop?.Cancel();
while (IsStopping)
{

MODIFY
WalletWasabi/Services/WasabiSynchronizer.cs
WalletWasabi/Services/WasabiSynchronizer.cs
@@ -258,10 +258,7 @@ namespace WalletWasabi.Services
try
{
// If stop was requested return.
- if (!IsRunning)
- {
- return;
- }
+ if (!IsRunning) return;
while (AreRequestsBlocked())
{
@@ -435,10 +432,7 @@ namespace WalletWasabi.Services
}
finally
{
- if (IsStopping)
- {
- Interlocked.Exchange(ref _running, 3);
- }
+ Interlocked.CompareExchange(ref _running, 3, 2); // If IsStopping, make it stopped.
}
});
}
@@ -539,10 +533,7 @@ namespace WalletWasabi.Services
{
if (disposing)
{
- if (IsRunning)
- {
- Interlocked.Exchange(ref _running, 2);
- }
+ Interlocked.CompareExchange(ref _running, 2, 1); // If running, make it stopping.
Cancel?.Cancel();
while (IsStopping)
{

MODIFY
WalletWasabi/TorSocks5/TorProcessManager.cs
WalletWasabi/TorSocks5/TorProcessManager.cs
@@ -258,7 +258,7 @@ namespace WalletWasabi.TorSocks5
try
{
// If stop was requested return.
- if (IsRunning == false) return;
+ if (!IsRunning) return;
await Task.Delay(torMisbehaviorCheckPeriod, Stop.Token).ConfigureAwait(false);
@@ -316,10 +316,7 @@ namespace WalletWasabi.TorSocks5
}
finally
{
- if (IsStopping)
- {
- Interlocked.Exchange(ref _running, 3);
- }
+ Interlocked.CompareExchange(ref _running, 3, 2); // If IsStopping, make it stopped.
}
});
}
@@ -334,10 +331,7 @@ namespace WalletWasabi.TorSocks5
{
if (disposing)
{
- if (IsRunning)
- {
- Interlocked.Exchange(ref _running, 2);
- }
+ Interlocked.CompareExchange(ref _running, 2, 1); // If running, make it stopping.
if (TorSocks5EndPoint == null) Interlocked.Exchange(ref _running, 3);

Improve Interlocked pattern on IsRunning
Related https://github.com/zkSNACKs/WalletWasabi/issues/1276