Skip to content

Commit

Permalink
Improved SemaphoreSlim handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ashneilson committed Feb 24, 2022
1 parent 2907c29 commit b887146
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 22 deletions.
20 changes: 10 additions & 10 deletions RICADO.Unitronics/Channels/EthernetChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,13 @@ public void Dispose()

public async Task InitializeAsync(int timeout, CancellationToken cancellationToken)
{
try
if (!_semaphore.Wait(0))
{
if (!_semaphore.Wait(0))
{
await _semaphore.WaitAsync(cancellationToken);
}
await _semaphore.WaitAsync(cancellationToken);
}

try
{
destroyClient();

await initializeClient(timeout, cancellationToken);
Expand All @@ -107,13 +107,13 @@ public async Task<ProcessMessageResult> ProcessMessageAsync(ReadOnlyMemory<byte>

while (attempts <= retries)
{
try
if (!_semaphore.Wait(0))
{
if (!_semaphore.Wait(0))
{
await _semaphore.WaitAsync(cancellationToken);
}
await _semaphore.WaitAsync(cancellationToken);
}

try
{
if (attempts > 0)
{
await destroyAndInitializeClient(timeout, cancellationToken);
Expand Down
10 changes: 8 additions & 2 deletions RICADO.Unitronics/Channels/SerialOverLANChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,13 @@ public async Task InitializeAsync(int timeout, CancellationToken cancellationTok
}
}

try
if (!_initializeSemaphore.Wait(0))
{
await _initializeSemaphore.WaitAsync(cancellationToken);
}

try
{
if (IsInitialized)
{
return;
Expand Down Expand Up @@ -165,10 +168,13 @@ public async Task<ProcessMessageResult> ProcessMessageAsync(ReadOnlyMemory<byte>

while (attempts <= retries)
{
try
if (!_requestSemaphore.Wait(0))
{
await _requestSemaphore.WaitAsync(cancellationToken);
}

try
{
if (attempts > 0)
{
await destroyAndInitializeClient(unitId, timeout, cancellationToken);
Expand Down
20 changes: 10 additions & 10 deletions RICADO.Unitronics/Channels/SerialOverLANFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,13 @@ public async Task<SerialOverLANChannel> GetOrCreate(Guid uniqueId, string remote

SerialOverLANChannel channel;

try
if (!_semaphore.Wait(0))
{
if (!_semaphore.Wait(0))
{
await _semaphore.WaitAsync(cancellationToken);
}
await _semaphore.WaitAsync(cancellationToken);
}

try
{
channel = _channels.GetOrAdd(channelKey, (key) =>
{
return new SerialOverLANChannel(remoteHost, port);
Expand Down Expand Up @@ -118,13 +118,13 @@ public async Task<bool> TryRemove(Guid uniqueId, string remoteHost, int port, Ca

string channelKey = getChannelKey(remoteHost, port);

try
if (!_semaphore.Wait(0))
{
if (!_semaphore.Wait(0))
{
await _semaphore.WaitAsync(cancellationToken);
}
await _semaphore.WaitAsync(cancellationToken);
}

try
{
if (_channels.TryGetValue(channelKey, out SerialOverLANChannel existingChannel))
{
existingChannel.UnregisterPLC(uniqueId);
Expand Down

0 comments on commit b887146

Please sign in to comment.