Skip to content

Commit 57346a8

Browse files
danielmarbachlukebakken
authored andcommitted
Replace locker with Exchange
1 parent c4200ab commit 57346a8

File tree

4 files changed

+14
-70
lines changed

4 files changed

+14
-70
lines changed

projects/RabbitMQ.Client/Impl/AutorecoveringChannel.cs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ internal sealed class AutorecoveringChannel : IChannel, IRecoverable
4848
private AutorecoveringConnection _connection;
4949
private RecoveryAwareChannel _innerChannel;
5050
private bool _disposed;
51-
private bool _isDisposing;
52-
private readonly object _locker = new();
51+
private int _isDisposing;
5352

5453
private ushort _prefetchCountConsumer;
5554
private ushort _prefetchCountGlobal;
@@ -266,20 +265,11 @@ public void Dispose()
266265

267266
public async ValueTask DisposeAsync()
268267
{
269-
if (_disposed)
268+
if (Interlocked.Exchange(ref _isDisposing, 1) != 0)
270269
{
271270
return;
272271
}
273272

274-
lock (_locker)
275-
{
276-
if (_isDisposing)
277-
{
278-
return;
279-
}
280-
_isDisposing = true;
281-
}
282-
283273
try
284274
{
285275
if (IsOpen)
@@ -293,7 +283,6 @@ await this.AbortAsync()
293283
finally
294284
{
295285
_disposed = true;
296-
_isDisposing = false;
297286
}
298287
}
299288

projects/RabbitMQ.Client/Impl/AutorecoveringConnection.cs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ internal sealed partial class AutorecoveringConnection : IConnection
5050

5151
private Connection _innerConnection;
5252
private bool _disposed;
53-
private bool _isDisposing;
54-
private readonly object _locker = new();
53+
private int _isDisposing;
5554

5655
private Connection InnerConnection
5756
{
@@ -282,20 +281,11 @@ public void Dispose()
282281

283282
public async ValueTask DisposeAsync()
284283
{
285-
if (_disposed)
284+
if (Interlocked.Exchange(ref _isDisposing, 1) != 0)
286285
{
287286
return;
288287
}
289288

290-
lock (_locker)
291-
{
292-
if (_isDisposing)
293-
{
294-
return;
295-
}
296-
_isDisposing = true;
297-
}
298-
299289
try
300290
{
301291
await _innerConnection.DisposeAsync()
@@ -313,7 +303,6 @@ await _innerConnection.DisposeAsync()
313303
finally
314304
{
315305
_disposed = true;
316-
_isDisposing = false;
317306
}
318307
}
319308

projects/RabbitMQ.Client/Impl/Channel.cs

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,7 @@ internal partial class Channel : IChannel, IRecoverable
6464
internal readonly IConsumerDispatcher ConsumerDispatcher;
6565

6666
private bool _disposed;
67-
private bool _isDisposing;
68-
69-
private readonly object _locker = new();
67+
private int _isDisposing;
7068

7169
public Channel(ISession session, CreateChannelOptions createChannelOptions)
7270
{
@@ -531,20 +529,11 @@ void IDisposable.Dispose()
531529

532530
protected virtual void Dispose(bool disposing)
533531
{
534-
if (_disposed)
532+
if (Interlocked.Exchange(ref _isDisposing, 1) != 0)
535533
{
536534
return;
537535
}
538536

539-
lock (_locker)
540-
{
541-
if (_isDisposing)
542-
{
543-
return;
544-
}
545-
_isDisposing = true;
546-
}
547-
548537
if (disposing)
549538
{
550539
try
@@ -554,10 +543,7 @@ protected virtual void Dispose(bool disposing)
554543
this.AbortAsync().GetAwaiter().GetResult();
555544
}
556545

557-
if (_serverOriginatedChannelCloseTcs is not null)
558-
{
559-
_serverOriginatedChannelCloseTcs.Task.Wait(TimeSpan.FromSeconds(5));
560-
}
546+
_serverOriginatedChannelCloseTcs?.Task.Wait(TimeSpan.FromSeconds(5));
561547

562548
ConsumerDispatcher.Dispose();
563549
_rpcSemaphore.Dispose();
@@ -567,7 +553,6 @@ protected virtual void Dispose(bool disposing)
567553
finally
568554
{
569555
_disposed = true;
570-
_isDisposing = false;
571556
}
572557
}
573558
}
@@ -582,20 +567,11 @@ await DisposeAsyncCore()
582567

583568
protected virtual async ValueTask DisposeAsyncCore()
584569
{
585-
if (_disposed)
570+
if (Interlocked.Exchange(ref _isDisposing, 1) != 0)
586571
{
587572
return;
588573
}
589574

590-
lock (_locker)
591-
{
592-
if (_isDisposing)
593-
{
594-
return;
595-
}
596-
_isDisposing = true;
597-
}
598-
599575
try
600576
{
601577
if (IsOpen)
@@ -621,7 +597,6 @@ await _outstandingPublisherConfirmationsRateLimiter.DisposeAsync()
621597
finally
622598
{
623599
_disposed = true;
624-
_isDisposing = false;
625600
}
626601
}
627602

@@ -718,9 +693,11 @@ protected virtual ulong AdjustDeliveryTag(ulong deliveryTag)
718693

719694
protected async Task<bool> HandleChannelCloseAsync(IncomingCommand cmd, CancellationToken cancellationToken)
720695
{
721-
lock (_locker)
696+
var serverOriginatedChannelCloseTcs = _serverOriginatedChannelCloseTcs;
697+
if (serverOriginatedChannelCloseTcs is null)
722698
{
723-
_serverOriginatedChannelCloseTcs ??= new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
699+
// Attempt to assign the new TCS only if _tcs is still null
700+
_ = Interlocked.CompareExchange(ref _serverOriginatedChannelCloseTcs, new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously), null);
724701
}
725702

726703
try

projects/RabbitMQ.Client/Impl/Connection.cs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@ namespace RabbitMQ.Client.Framing
4646
internal sealed partial class Connection : IConnection
4747
{
4848
private bool _disposed;
49-
private bool _isDisposing;
50-
private readonly object _locker = new();
49+
private int _isDisposing;
5150
private volatile bool _closed;
5251

5352
private readonly ConnectionConfig _config;
@@ -499,20 +498,11 @@ public void Dispose()
499498

500499
public async ValueTask DisposeAsync()
501500
{
502-
if (_disposed)
501+
if (Interlocked.Exchange(ref _isDisposing, 1) != 0)
503502
{
504503
return;
505504
}
506505

507-
lock (_locker)
508-
{
509-
if (_isDisposing)
510-
{
511-
return;
512-
}
513-
_isDisposing = true;
514-
}
515-
516506
try
517507
{
518508
if (IsOpen)
@@ -534,7 +524,6 @@ await _channel0.DisposeAsync()
534524
finally
535525
{
536526
_disposed = true;
537-
_isDisposing = false;
538527
}
539528
}
540529

0 commit comments

Comments
 (0)