Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ReentrancyGuard.cs
1using System;
2using System.Threading;
3using System.Threading.Tasks;
4
6{
10 public sealed class ReentrancyGuard
11 {
12 private int gate = 0;
13
19 public async Task<bool> RunIfNotBusy(Func<Task> action)
20 {
21 if (action is null)
22 throw new ArgumentNullException(nameof(action));
23 if (Interlocked.CompareExchange(ref this.gate, 1, 0) != 0)
24 {
25 return false; // Already running
26 }
27 try
28 {
29 await action().ConfigureAwait(false);
30 return true;
31 }
32 finally
33 {
34 Interlocked.Exchange(ref this.gate, 0);
35 }
36 }
37
41 public bool IsBusy => this.gate != 0;
42 }
43}
Simple asynchronous reentrancy guard that ensures a critical async section is not executed concurrent...
async Task< bool > RunIfNotBusy(Func< Task > action)
Attempts to execute the asynchronous action if no prior execution is in progress. Returns false if th...
bool IsBusy
Returns true if an execution is currently in progress.