Skip to content

Commit f45af38

Browse files
Replace SemaphoreLight with SemaphoreSlim (#1265)
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]>
1 parent 4c4883e commit f45af38

File tree

23 files changed

+46
-384
lines changed

23 files changed

+46
-384
lines changed

src/Renci.SshNet/Channels/Channel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ protected IConnectionInfo ConnectionInfo
285285
/// Gets the session semaphore to control number of session channels.
286286
/// </summary>
287287
/// <value>The session semaphore.</value>
288-
protected SemaphoreLight SessionSemaphore
288+
protected SemaphoreSlim SessionSemaphore
289289
{
290290
get { return _session.SessionSemaphore; }
291291
}

src/Renci.SshNet/Common/SemaphoreLight.cs

Lines changed: 0 additions & 244 deletions
This file was deleted.

src/Renci.SshNet/ISession.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ internal interface ISession : IDisposable
3636
/// <value>
3737
/// The session semaphore.
3838
/// </value>
39-
SemaphoreLight SessionSemaphore { get; }
39+
SemaphoreSlim SessionSemaphore { get; }
4040

4141
/// <summary>
4242
/// Gets a <see cref="WaitHandle"/> that can be used to wait for the message listener loop to complete.

src/Renci.SshNet/Session.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public class Session : ISession
8787
/// <remarks>
8888
/// Some server may restrict number to prevent authentication attacks.
8989
/// </remarks>
90-
private static readonly SemaphoreLight AuthenticationConnection = new SemaphoreLight(3);
90+
private static readonly SemaphoreSlim AuthenticationConnection = new SemaphoreSlim(3);
9191

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

197197
private Compressor _clientCompression;
198198

199-
private SemaphoreLight _sessionSemaphore;
199+
private SemaphoreSlim _sessionSemaphore;
200200

201201
private bool _isDisconnectMessageSent;
202202

@@ -213,15 +213,15 @@ public class Session : ISession
213213
/// <value>
214214
/// The session semaphore.
215215
/// </value>
216-
public SemaphoreLight SessionSemaphore
216+
public SemaphoreSlim SessionSemaphore
217217
{
218218
get
219219
{
220220
if (_sessionSemaphore is null)
221221
{
222222
lock (_connectAndLazySemaphoreInitLock)
223223
{
224-
_sessionSemaphore ??= new SemaphoreLight(ConnectionInfo.MaxSessions);
224+
_sessionSemaphore ??= new SemaphoreSlim(ConnectionInfo.MaxSessions);
225225
}
226226
}
227227

src/Renci.SshNet/Sftp/SftpFileReader.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ internal sealed class SftpFileReader : ISftpFileReader
1616
private readonly byte[] _handle;
1717
private readonly ISftpSession _sftpSession;
1818
private readonly uint _chunkSize;
19-
private readonly SemaphoreLight _semaphore;
19+
private readonly SemaphoreSlim _semaphore;
2020
private readonly object _readLock;
2121
private readonly ManualResetEvent _disposingWaitHandle;
2222
private readonly ManualResetEvent _readAheadCompleted;
@@ -62,7 +62,7 @@ public SftpFileReader(byte[] handle, ISftpSession sftpSession, uint chunkSize, i
6262
_sftpSession = sftpSession;
6363
_chunkSize = chunkSize;
6464
_fileSize = fileSize;
65-
_semaphore = new SemaphoreLight(maxPendingReads);
65+
_semaphore = new SemaphoreSlim(maxPendingReads);
6666
_queue = new Dictionary<int, BufferedRead>(maxPendingReads);
6767
_readLock = new object();
6868
_readAheadCompleted = new ManualResetEvent(initialState: false);

test/Renci.SshNet.Tests/Classes/Channels/ChannelSessionTest_Dispose_Disposed.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public class ChannelSessionTest_Dispose_Disposed : ChannelSessionTestBase
2323
private uint _remotePacketSize;
2424
private uint _remoteChannelNumber;
2525
private TimeSpan _channelCloseTimeout;
26-
private SemaphoreLight _sessionSemaphore;
26+
private SemaphoreSlim _sessionSemaphore;
2727
private IList<ChannelEventArgs> _channelClosedRegister;
2828
private List<ExceptionEventArgs> _channelExceptionRegister;
2929

@@ -38,7 +38,7 @@ protected override void SetupData()
3838
_remoteWindowSize = (uint)random.Next(0, int.MaxValue);
3939
_remotePacketSize = (uint)random.Next(100, 200);
4040
_channelCloseTimeout = TimeSpan.FromSeconds(random.Next(10, 20));
41-
_sessionSemaphore = new SemaphoreLight(1);
41+
_sessionSemaphore = new SemaphoreSlim(1);
4242
_channelClosedRegister = new List<ChannelEventArgs>();
4343
_channelExceptionRegister = new List<ExceptionEventArgs>();
4444
}

test/Renci.SshNet.Tests/Classes/Channels/ChannelSessionTest_Dispose_SessionIsConnectedAndChannelIsOpen_ChannelCloseAndChannelEofReceived_DisposeInEventHandler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class ChannelSessionTest_Dispose_SessionIsConnectedAndChannelIsOpen_Chann
2424
private List<ExceptionEventArgs> _channelExceptionRegister;
2525
private ChannelSession _channel;
2626
private MockSequence _sequence;
27-
private SemaphoreLight _sessionSemaphore;
27+
private SemaphoreSlim _sessionSemaphore;
2828
private int _initialSessionSemaphoreCount;
2929

3030
protected override void SetupData()
@@ -41,7 +41,7 @@ protected override void SetupData()
4141
_channelClosedRegister = new List<ChannelEventArgs>();
4242
_channelExceptionRegister = new List<ExceptionEventArgs>();
4343
_initialSessionSemaphoreCount = random.Next(10, 20);
44-
_sessionSemaphore = new SemaphoreLight(_initialSessionSemaphoreCount);
44+
_sessionSemaphore = new SemaphoreSlim(_initialSessionSemaphoreCount);
4545
}
4646

4747
protected override void SetupMocks()

test/Renci.SshNet.Tests/Classes/Channels/ChannelSessionTest_Dispose_SessionIsConnectedAndChannelIsOpen_ChannelCloseAndChannelEofReceived_SendChannelCloseMessageFailure.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public class ChannelSessionTest_Dispose_SessionIsConnectedAndChannelIsOpen_Chann
2222
private List<ExceptionEventArgs> _channelExceptionRegister;
2323
private ChannelSession _channel;
2424
private MockSequence _sequence;
25-
private SemaphoreLight _sessionSemaphore;
25+
private SemaphoreSlim _sessionSemaphore;
2626
private int _initialSessionSemaphoreCount;
2727

2828
protected override void SetupData()
@@ -38,7 +38,7 @@ protected override void SetupData()
3838
_channelClosedRegister = new List<ChannelEventArgs>();
3939
_channelExceptionRegister = new List<ExceptionEventArgs>();
4040
_initialSessionSemaphoreCount = random.Next(10, 20);
41-
_sessionSemaphore = new SemaphoreLight(_initialSessionSemaphoreCount);
41+
_sessionSemaphore = new SemaphoreSlim(_initialSessionSemaphoreCount);
4242
}
4343

4444
protected override void SetupMocks()
@@ -126,4 +126,4 @@ public void IsOpenShouldReturnFalse()
126126
Assert.IsFalse(_channel.IsOpen);
127127
}
128128
}
129-
}
129+
}

0 commit comments

Comments
 (0)