Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions src/Npgsql/Internal/NpgsqlConnector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,20 +190,20 @@ internal string InferredUserName
/// cancellation is delivered. This reduces the chance that a cancellation meant for a previous
/// command will accidentally cancel a later one, see #615.
/// </summary>
object CancelLock { get; } = new();
Lock CancelLock { get; } = new();

/// <summary>
/// A lock that's taken to make sure no other concurrent operation is running.
/// Break takes it to set the state of the connector.
/// Anyone else should immediately check the state and exit
/// if the connector is closed.
/// </summary>
object SyncObj { get; } = new();
Lock SyncObj { get; } = new();

/// <summary>
/// A lock that's used to wait for the Cleanup to complete while breaking the connection.
/// </summary>
object CleanupLock { get; } = new();
Lock CleanupLock { get; } = new();

readonly bool _isKeepAliveEnabled;
readonly Timer? _keepAliveTimer;
Expand Down Expand Up @@ -1918,7 +1918,7 @@ internal void PerformImmediateUserCancellation()
return;
// The connector is still alive, take the CancelLock before exiting SingleUseLock.
// If a break will happen after, it's going to wait for the cancellation to complete.
Monitor.Enter(CancelLock);
CancelLock.Enter();
}

try
Expand All @@ -1939,7 +1939,7 @@ internal void PerformImmediateUserCancellation()
}
finally
{
Monitor.Exit(CancelLock);
CancelLock.Exit();
}
}

Expand All @@ -1954,7 +1954,7 @@ void PerformDelayedUserCancellation()
return;
// The connector is still alive, take the CancelLock before exiting SingleUseLock.
// If a break will happen after, it's going to wait for the cancellation to complete.
Monitor.Enter(CancelLock);
CancelLock.Enter();
}

try
Expand All @@ -1963,7 +1963,7 @@ void PerformDelayedUserCancellation()
}
finally
{
Monitor.Exit(CancelLock);
CancelLock.Exit();
}
}

Expand Down Expand Up @@ -2265,14 +2265,14 @@ internal Exception Break(Exception reason, bool markHostAsOfflineOnConnecting =
{
Debug.Assert(!IsClosed);

Monitor.Enter(SyncObj);
SyncObj.Enter();

var state = State;
if (state == ConnectorState.Broken)
{
// We're already broken.
// Exit SingleUseLock to unblock other threads (like cancellation).
Monitor.Exit(SyncObj);
SyncObj.Exit();
// Wait for the break to complete before going forward.
lock (CleanupLock) { }
return reason;
Expand All @@ -2287,12 +2287,12 @@ internal Exception Break(Exception reason, bool markHostAsOfflineOnConnecting =
Interlocked.CompareExchange(ref _breakReason, reason, null);
State = ConnectorState.Broken;
// Take the CleanupLock while in SingleUseLock to make sure concurrent Break doesn't take it first.
Monitor.Enter(CleanupLock);
CleanupLock.Enter();
}
finally
{
// Unblock other threads (like cancellation) to proceed and exit gracefully.
Monitor.Exit(SyncObj);
SyncObj.Exit();
}

try
Expand Down Expand Up @@ -2343,7 +2343,7 @@ internal Exception Break(Exception reason, bool markHostAsOfflineOnConnecting =
}
finally
{
Monitor.Exit(CleanupLock);
CleanupLock.Exit();
}
}

Expand Down Expand Up @@ -2801,7 +2801,7 @@ internal void EndUserAction()
void PerformKeepAlive(object? state)
{
Debug.Assert(_isKeepAliveEnabled);
if (!Monitor.TryEnter(SyncObj))
if (!SyncObj.TryEnter())
return;

try
Expand Down Expand Up @@ -2833,7 +2833,7 @@ void PerformKeepAlive(object? state)
}
finally
{
Monitor.Exit(SyncObj);
SyncObj.Exit();
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/Npgsql/TypeMapping/GlobalTypeMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Text.Json;
using System.Threading;
using Npgsql.Internal;
using Npgsql.Internal.Postgres;
using Npgsql.Internal.ResolverFactories;
Expand All @@ -13,7 +14,7 @@ sealed class GlobalTypeMapper : INpgsqlTypeMapper
{
readonly UserTypeMapper _userTypeMapper = new();
readonly List<PgTypeInfoResolverFactory> _pluginResolverFactories = [];
readonly object _sync = new();
readonly Lock _sync = new();
PgTypeInfoResolverFactory[] _typeMappingResolvers = [];

internal IEnumerable<PgTypeInfoResolverFactory> GetPluginResolverFactories()
Expand Down
2 changes: 1 addition & 1 deletion src/Npgsql/Util/ResettableCancellationTokenSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ sealed class ResettableCancellationTokenSource(TimeSpan timeout) : IDisposable
/// <summary>
/// Used, so we wouldn't concurrently use the cts for the cancellation, while it's being disposed
/// </summary>
readonly object lockObject = new();
readonly Lock lockObject = new();

#if DEBUG
bool _isRunning;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Npgsql.Tests.Support;
sealed class SingleThreadSynchronizationContext : SynchronizationContext, IDisposable
{
readonly BlockingCollection<CallbackAndState> _tasks = new();
readonly object _lockObject = new();
readonly Lock _lockObject = new();
volatile Thread? _thread;
bool _doingWork;

Expand Down
2 changes: 1 addition & 1 deletion test/Npgsql.Tests/Support/TestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public abstract class TestBase

static readonly SemaphoreSlim DatabaseCreationLock = new(1);

static readonly object dataSourceLockObject = new();
static readonly Lock dataSourceLockObject = new();

static ConcurrentDictionary<string, NpgsqlDataSource> DataSources = new(StringComparer.Ordinal);

Expand Down
Loading