Skip to content

Commit 6ab339c

Browse files
committed
Use Lock type in .NET 9.0.
1 parent 31fe2cc commit 6ab339c

File tree

5 files changed

+58
-14
lines changed

5 files changed

+58
-14
lines changed

src/MySqlConnector/Authentication/AuthenticationPlugins.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ internal static bool TryGetPlugin(string name, [NotNullWhen(true)] out IAuthenti
3535
return s_plugins.TryGetValue(name, out plugin);
3636
}
3737

38+
#if NET9_0_OR_GREATER
39+
private static readonly Lock s_lock = new();
40+
#else
3841
private static readonly object s_lock = new();
42+
#endif
3943
private static readonly Dictionary<string, IAuthenticationPlugin> s_plugins = [];
4044
}

src/MySqlConnector/Core/ILoadBalancer.cs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,18 @@ internal sealed class RandomLoadBalancer : ILoadBalancer
2727
public IReadOnlyList<string> LoadBalance(IReadOnlyList<string> hosts)
2828
{
2929
#pragma warning disable CA5394 // Do not use insecure randomness
30-
// from https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#The_modern_algorithm
30+
#if NET8_0_OR_GREATER
31+
var shuffled = hosts.ToArray();
32+
lock (m_lock)
33+
m_random.Shuffle(shuffled);
34+
return shuffled;
35+
#else
3136
var shuffled = new List<string>(hosts);
37+
// from https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#The_modern_algorithm
3238
for (var i = hosts.Count - 1; i >= 1; i--)
3339
{
3440
int j;
35-
lock (m_random)
41+
lock (m_lock)
3642
j = m_random.Next(i + 1);
3743
if (i != j)
3844
{
@@ -42,11 +48,21 @@ public IReadOnlyList<string> LoadBalance(IReadOnlyList<string> hosts)
4248
}
4349
}
4450
return shuffled;
51+
#endif
4552
}
4653

47-
private RandomLoadBalancer() => m_random = new();
54+
private RandomLoadBalancer()
55+
{
56+
m_random = new();
57+
m_lock = new();
58+
}
4859

4960
private readonly Random m_random;
61+
#if NET9_0_OR_GREATER
62+
private readonly Lock m_lock;
63+
#else
64+
private readonly object m_lock;
65+
#endif
5066
}
5167

5268
internal sealed class RoundRobinLoadBalancer : ILoadBalancer
@@ -67,6 +83,10 @@ public IReadOnlyList<string> LoadBalance(IReadOnlyList<string> hosts)
6783
return shuffled;
6884
}
6985

86+
#if NET9_0_OR_GREATER
87+
private readonly Lock m_lock;
88+
#else
7089
private readonly object m_lock;
90+
#endif
7191
private uint m_counter;
7292
}

src/MySqlConnector/MySqlBulkLoader.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,7 @@ internal async ValueTask<int> LoadAsync(IOBehavior ioBehavior, CancellationToken
176176
{
177177
// replace the file name with a sentinel so that we know (when processing the result set) that it's not spoofed by the server
178178
var newFileName = GenerateSourceFileName();
179-
lock (s_lock)
180-
s_sources.Add(newFileName, CreateFileStream(FileName!));
179+
AddSource(newFileName, CreateFileStream(FileName!));
181180
FileName = newFileName;
182181
}
183182
}
@@ -187,8 +186,7 @@ internal async ValueTask<int> LoadAsync(IOBehavior ioBehavior, CancellationToken
187186
throw new InvalidOperationException("Local must be true to use SourceStream, SourceDataTable, or SourceDataReader.");
188187

189188
FileName = GenerateSourceFileName();
190-
lock (s_lock)
191-
s_sources.Add(FileName, Source!);
189+
AddSource(FileName, Source!);
192190
}
193191

194192
var closeConnection = false;
@@ -221,6 +219,12 @@ internal async ValueTask<int> LoadAsync(IOBehavior ioBehavior, CancellationToken
221219
if (closeConnection)
222220
Connection.Close();
223221
}
222+
223+
static void AddSource(string name, object source)
224+
{
225+
lock (s_lock)
226+
s_sources.Add(name, source);
227+
}
224228
}
225229

226230
internal const string SourcePrefix = ":SOURCE:";
@@ -319,6 +323,10 @@ internal static bool TryGetAndRemoveSource(string sourceKey, [NotNullWhen(true)]
319323

320324
private static string GenerateSourceFileName() => SourcePrefix + Guid.NewGuid().ToString("N");
321325

326+
#if NET9_0_OR_GREATER
327+
private static readonly Lock s_lock = new();
328+
#else
322329
private static readonly object s_lock = new();
330+
#endif
323331
private static readonly Dictionary<string, object> s_sources = [];
324332
}

src/MySqlConnector/MySqlConnection.cs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,16 +1189,20 @@ private async Task DoCloseAsync(bool changeState, IOBehavior ioBehavior)
11891189
};
11901190
connection.TakeSessionFrom(this);
11911191

1192-
// put the new, idle, connection into the list of sessions for this transaction (replacing this MySqlConnection)
1193-
lock (s_lock)
1192+
ReplaceConnection(this, connection);
1193+
static void ReplaceConnection(MySqlConnection thisConnection, MySqlConnection connection)
11941194
{
1195-
foreach (var enlistedTransaction in s_transactionConnections[connection.m_enlistedTransaction!.Transaction])
1195+
// put the new, idle, connection into the list of sessions for this transaction (replacing this MySqlConnection)
1196+
lock (s_lock)
11961197
{
1197-
if (enlistedTransaction.Connection == this)
1198+
foreach (var enlistedTransaction in s_transactionConnections[connection.m_enlistedTransaction!.Transaction])
11981199
{
1199-
enlistedTransaction.Connection = connection;
1200-
enlistedTransaction.IsIdle = true;
1201-
break;
1200+
if (enlistedTransaction.Connection == thisConnection)
1201+
{
1202+
enlistedTransaction.Connection = connection;
1203+
enlistedTransaction.IsIdle = true;
1204+
break;
1205+
}
12021206
}
12031207
}
12041208
}
@@ -1256,7 +1260,11 @@ private ConnectionSettings GetConnectionSettings() =>
12561260
private static readonly StateChangeEventArgs s_stateChangeClosedConnecting = new(ConnectionState.Closed, ConnectionState.Connecting);
12571261
private static readonly StateChangeEventArgs s_stateChangeConnectingOpen = new(ConnectionState.Connecting, ConnectionState.Open);
12581262
private static readonly StateChangeEventArgs s_stateChangeOpenClosed = new(ConnectionState.Open, ConnectionState.Closed);
1263+
#if NET9_0_OR_GREATER
1264+
private static readonly Lock s_lock = new();
1265+
#else
12591266
private static readonly object s_lock = new();
1267+
#endif
12601268
private static readonly Dictionary<System.Transactions.Transaction, List<EnlistedTransactionBase>> s_transactionConnections = [];
12611269
private static readonly ReadOnlyMemory<byte>[] s_startTransactionPayloads = new ReadOnlyMemory<byte>[5 * 3 * 2];
12621270

src/MySqlConnector/Utilities/TimerQueue.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,11 @@ public Data(uint id, int time, Action action)
131131
public Action Action { get; }
132132
}
133133

134+
#if NET9_0_OR_GREATER
135+
private readonly Lock m_lock;
136+
#else
134137
private readonly object m_lock;
138+
#endif
135139
private readonly Timer m_timer;
136140
private readonly List<Data> m_timeoutActions;
137141
private uint m_counter;

0 commit comments

Comments
 (0)