Skip to content

Commit 9000511

Browse files
ladeakcaptainsafia
authored andcommitted
Use System.Threading.Lock in Kestrel (#57236)
1 parent 385bd93 commit 9000511

File tree

25 files changed

+38
-40
lines changed

25 files changed

+38
-40
lines changed

src/Servers/Kestrel/Core/src/Internal/CertificatePathWatcher.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ internal sealed partial class CertificatePathWatcher : IDisposable
1717
private readonly string _contentRootDir;
1818
private readonly ILogger<CertificatePathWatcher> _logger;
1919

20-
private readonly object _metadataLock = new();
20+
private readonly Lock _metadataLock = new();
2121

2222
/// <remarks>Acquire <see cref="_metadataLock"/> before accessing.</remarks>
2323
private readonly Dictionary<string, DirectoryWatchMetadata> _metadataForDirectory = new();

src/Servers/Kestrel/Core/src/Internal/Http/Http1OutputProducer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ internal class Http1OutputProducer : IHttpOutputProducer, IDisposable
3434
private readonly TimingPipeFlusher _flusher;
3535

3636
// This locks access to all of the below fields
37-
private readonly object _contextLock = new object();
37+
private readonly Lock _contextLock = new();
3838

3939
private bool _pipeWriterCompleted;
4040
private bool _aborted;

src/Servers/Kestrel/Core/src/Internal/Http/HttpProtocol.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ internal abstract partial class HttpProtocol : IHttpResponseControl
4343
private Stack<KeyValuePair<Func<object, Task>, object>>? _onStarting;
4444
private Stack<KeyValuePair<Func<object, Task>, object>>? _onCompleted;
4545

46-
private readonly object _abortLock = new object();
46+
private readonly Lock _abortLock = new();
4747
protected volatile bool _connectionAborted;
4848
private bool _preventRequestAbortedCancellation;
4949
private CancellationTokenSource? _abortedCts;

src/Servers/Kestrel/Core/src/Internal/Http2/FlowControl/InputFlowControl.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ internal sealed class InputFlowControl
2525
private FlowControl _flow;
2626
private int _pendingUpdateSize;
2727
private bool _windowUpdatesDisabled;
28-
private readonly object _flowLock = new object();
28+
private readonly Lock _flowLock = new();
2929

3030
public InputFlowControl(uint initialWindowSize, uint minWindowSizeIncrement)
3131
{

src/Servers/Kestrel/Core/src/Internal/Http2/Http2FrameWriter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ internal sealed class Http2FrameWriter
7171

7272
private bool IsFlowControlQueueLimitEnabled => _maximumFlowControlQueueSize > 0;
7373

74-
private readonly object _writeLock = new object();
74+
private readonly Lock _writeLock = new();
7575
private readonly Http2Frame _outgoingFrame;
7676
private readonly Http2HeadersEnumerator _headersEnumerator = new Http2HeadersEnumerator();
7777
private readonly ConcurrentPipeWriter _outputWriter;
@@ -99,7 +99,7 @@ internal sealed class Http2FrameWriter
9999
private bool _completed;
100100
private bool _aborted;
101101

102-
private readonly object _windowUpdateLock = new();
102+
private readonly Lock _windowUpdateLock = new();
103103
private long _connectionWindow;
104104
private readonly Queue<Http2OutputProducer> _waitingForMoreConnectionWindow = new();
105105
// This is the stream that consumed the last set of connection window

src/Servers/Kestrel/Core/src/Internal/Http2/Http2OutputProducer.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ internal sealed class Http2OutputProducer : IHttpOutputProducer, IHttpOutputAbor
2828

2929
private readonly MemoryPool<byte> _memoryPool;
3030
private readonly Http2Stream _stream;
31-
private readonly object _dataWriterLock = new object();
31+
private readonly Lock _dataWriterLock = new();
3232
private readonly Pipe _pipe;
3333
private readonly ConcurrentPipeWriter _pipeWriter;
3434
private readonly PipeReader _pipeReader;
@@ -599,7 +599,7 @@ public void Reset()
599599

600600
internal void OnRequestProcessingEnded()
601601
{
602-
var shouldCompleteStream = false;
602+
bool shouldCompleteStream;
603603
lock (_dataWriterLock)
604604
{
605605
if (_requestProcessingComplete)
@@ -625,7 +625,7 @@ internal void OnRequestProcessingEnded()
625625

626626
internal ValueTask<FlushResult> CompleteResponseAsync()
627627
{
628-
var shouldCompleteStream = false;
628+
bool shouldCompleteStream;
629629
ValueTask<FlushResult> task = default;
630630

631631
lock (_dataWriterLock)
@@ -705,8 +705,7 @@ internal Memory<byte> GetFakeMemory(int minSize)
705705

706706
public bool TryUpdateStreamWindow(int bytes)
707707
{
708-
var schedule = false;
709-
708+
bool schedule;
710709
lock (_dataWriterLock)
711710
{
712711
var maxUpdate = Http2PeerSettings.MaxWindowSize - _streamWindow;

src/Servers/Kestrel/Core/src/Internal/Http2/Http2Stream.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ internal abstract partial class Http2Stream : HttpProtocol, IThreadPoolWorkItem,
4444
internal long DrainExpirationTimestamp { get; set; }
4545

4646
private StreamCompletionFlags _completionState;
47-
private readonly object _completionLock = new object();
47+
private readonly Lock _completionLock = new();
4848

4949
public void Initialize(Http2StreamContext context)
5050
{

src/Servers/Kestrel/Core/src/Internal/Http3/Http3Connection.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ internal sealed class Http3Connection : IHttp3StreamLifetimeHandler, IRequestPro
3535
// so start highest opened request stream ID at -4.
3636
private const long DefaultHighestOpenedRequestStreamId = -4;
3737

38-
private readonly object _sync = new();
38+
private readonly Lock _sync = new();
3939
private readonly HttpMultiplexedConnectionContext _context;
40-
private readonly object _protocolSelectionLock = new();
40+
private readonly Lock _protocolSelectionLock = new();
4141
private readonly StreamCloseAwaitable _streamCompletionAwaitable = new();
4242
private readonly IProtocolErrorCodeFeature _errorCodeFeature;
4343
private readonly Dictionary<long, WebTransportSession>? _webtransportSessions;

src/Servers/Kestrel/Core/src/Internal/Http3/Http3ControlStream.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ internal abstract class Http3ControlStream : IHttp3Stream, IThreadPoolWorkItem
2828
private readonly Http3RawFrame _incomingFrame = new Http3RawFrame();
2929
private volatile int _isClosed;
3030
private long _headerType;
31-
private readonly object _completionLock = new();
31+
private readonly Lock _completionLock = new();
3232

3333
private bool _haveReceivedSettingsFrame;
3434
private StreamCompletionFlags _completionState;

src/Servers/Kestrel/Core/src/Internal/Http3/Http3FrameWriter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ internal sealed class Http3FrameWriter
2929
private const int MaxDataFrameSize = 16 * 1024;
3030
private const int HeaderBufferSize = 16 * 1024;
3131

32-
private readonly object _writeLock = new object();
32+
private readonly Lock _writeLock = new();
3333

3434
private readonly int _maxTotalHeaderSize;
3535
private readonly ConnectionContext _connectionContext;

0 commit comments

Comments
 (0)