Skip to content

Commit

Permalink
Replace SemaphoreLight with SemaphoreSlim (#1265)
Browse files Browse the repository at this point in the history
This paves the way for asynchronous synchronisation via WaitAsync
(and eliminates a timing test which sometimes fails in CI)

Co-authored-by: Wojciech Nagórski <[email protected]>
  • Loading branch information
Rob-Hague and WojciechNagorski authored Dec 9, 2023
1 parent 4c4883e commit f45af38
Show file tree
Hide file tree
Showing 23 changed files with 46 additions and 384 deletions.
2 changes: 1 addition & 1 deletion src/Renci.SshNet/Channels/Channel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ protected IConnectionInfo ConnectionInfo
/// Gets the session semaphore to control number of session channels.
/// </summary>
/// <value>The session semaphore.</value>
protected SemaphoreLight SessionSemaphore
protected SemaphoreSlim SessionSemaphore
{
get { return _session.SessionSemaphore; }
}
Expand Down
244 changes: 0 additions & 244 deletions src/Renci.SshNet/Common/SemaphoreLight.cs

This file was deleted.

2 changes: 1 addition & 1 deletion src/Renci.SshNet/ISession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ internal interface ISession : IDisposable
/// <value>
/// The session semaphore.
/// </value>
SemaphoreLight SessionSemaphore { get; }
SemaphoreSlim SessionSemaphore { get; }

/// <summary>
/// Gets a <see cref="WaitHandle"/> that can be used to wait for the message listener loop to complete.
Expand Down
8 changes: 4 additions & 4 deletions src/Renci.SshNet/Session.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public class Session : ISession
/// <remarks>
/// Some server may restrict number to prevent authentication attacks.
/// </remarks>
private static readonly SemaphoreLight AuthenticationConnection = new SemaphoreLight(3);
private static readonly SemaphoreSlim AuthenticationConnection = new SemaphoreSlim(3);

/// <summary>
/// Holds the factory to use for creating new services.
Expand Down Expand Up @@ -196,7 +196,7 @@ public class Session : ISession

private Compressor _clientCompression;

private SemaphoreLight _sessionSemaphore;
private SemaphoreSlim _sessionSemaphore;

private bool _isDisconnectMessageSent;

Expand All @@ -213,15 +213,15 @@ public class Session : ISession
/// <value>
/// The session semaphore.
/// </value>
public SemaphoreLight SessionSemaphore
public SemaphoreSlim SessionSemaphore
{
get
{
if (_sessionSemaphore is null)
{
lock (_connectAndLazySemaphoreInitLock)
{
_sessionSemaphore ??= new SemaphoreLight(ConnectionInfo.MaxSessions);
_sessionSemaphore ??= new SemaphoreSlim(ConnectionInfo.MaxSessions);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/Renci.SshNet/Sftp/SftpFileReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ internal sealed class SftpFileReader : ISftpFileReader
private readonly byte[] _handle;
private readonly ISftpSession _sftpSession;
private readonly uint _chunkSize;
private readonly SemaphoreLight _semaphore;
private readonly SemaphoreSlim _semaphore;
private readonly object _readLock;
private readonly ManualResetEvent _disposingWaitHandle;
private readonly ManualResetEvent _readAheadCompleted;
Expand Down Expand Up @@ -62,7 +62,7 @@ public SftpFileReader(byte[] handle, ISftpSession sftpSession, uint chunkSize, i
_sftpSession = sftpSession;
_chunkSize = chunkSize;
_fileSize = fileSize;
_semaphore = new SemaphoreLight(maxPendingReads);
_semaphore = new SemaphoreSlim(maxPendingReads);
_queue = new Dictionary<int, BufferedRead>(maxPendingReads);
_readLock = new object();
_readAheadCompleted = new ManualResetEvent(initialState: false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class ChannelSessionTest_Dispose_Disposed : ChannelSessionTestBase
private uint _remotePacketSize;
private uint _remoteChannelNumber;
private TimeSpan _channelCloseTimeout;
private SemaphoreLight _sessionSemaphore;
private SemaphoreSlim _sessionSemaphore;
private IList<ChannelEventArgs> _channelClosedRegister;
private List<ExceptionEventArgs> _channelExceptionRegister;

Expand All @@ -38,7 +38,7 @@ protected override void SetupData()
_remoteWindowSize = (uint)random.Next(0, int.MaxValue);
_remotePacketSize = (uint)random.Next(100, 200);
_channelCloseTimeout = TimeSpan.FromSeconds(random.Next(10, 20));
_sessionSemaphore = new SemaphoreLight(1);
_sessionSemaphore = new SemaphoreSlim(1);
_channelClosedRegister = new List<ChannelEventArgs>();
_channelExceptionRegister = new List<ExceptionEventArgs>();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class ChannelSessionTest_Dispose_SessionIsConnectedAndChannelIsOpen_Chann
private List<ExceptionEventArgs> _channelExceptionRegister;
private ChannelSession _channel;
private MockSequence _sequence;
private SemaphoreLight _sessionSemaphore;
private SemaphoreSlim _sessionSemaphore;
private int _initialSessionSemaphoreCount;

protected override void SetupData()
Expand All @@ -41,7 +41,7 @@ protected override void SetupData()
_channelClosedRegister = new List<ChannelEventArgs>();
_channelExceptionRegister = new List<ExceptionEventArgs>();
_initialSessionSemaphoreCount = random.Next(10, 20);
_sessionSemaphore = new SemaphoreLight(_initialSessionSemaphoreCount);
_sessionSemaphore = new SemaphoreSlim(_initialSessionSemaphoreCount);
}

protected override void SetupMocks()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class ChannelSessionTest_Dispose_SessionIsConnectedAndChannelIsOpen_Chann
private List<ExceptionEventArgs> _channelExceptionRegister;
private ChannelSession _channel;
private MockSequence _sequence;
private SemaphoreLight _sessionSemaphore;
private SemaphoreSlim _sessionSemaphore;
private int _initialSessionSemaphoreCount;

protected override void SetupData()
Expand All @@ -38,7 +38,7 @@ protected override void SetupData()
_channelClosedRegister = new List<ChannelEventArgs>();
_channelExceptionRegister = new List<ExceptionEventArgs>();
_initialSessionSemaphoreCount = random.Next(10, 20);
_sessionSemaphore = new SemaphoreLight(_initialSessionSemaphoreCount);
_sessionSemaphore = new SemaphoreSlim(_initialSessionSemaphoreCount);
}

protected override void SetupMocks()
Expand Down Expand Up @@ -126,4 +126,4 @@ public void IsOpenShouldReturnFalse()
Assert.IsFalse(_channel.IsOpen);
}
}
}
}
Loading

0 comments on commit f45af38

Please sign in to comment.