Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved locking performance on .NET 9.0+ #174

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
19 changes: 10 additions & 9 deletions src/ReaLTaiizor/Animate/Poison/DelayedCall.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ internal class DelayedCall : IDisposable
protected static List<DelayedCall> dcList;

protected System.Timers.Timer timer;
protected object timerLock;
protected Lock timerLock;
protected static readonly Lock dcListLock = new();
private Callback callback;
protected bool cancelled = false;

Expand All @@ -30,7 +31,7 @@ static DelayedCall()

protected DelayedCall()
{
timerLock = new object();
timerLock = new();
}

#region Compatibility code
Expand Down Expand Up @@ -179,7 +180,7 @@ protected static void PrepareDCObject(DelayedCall dc, int milliseconds, bool asy

protected static void Register(DelayedCall dc)
{
lock (dcList)
lock (dcListLock)
{
if (!dcList.Contains(dc))
{
Expand All @@ -190,7 +191,7 @@ protected static void Register(DelayedCall dc)

protected static void Unregister(DelayedCall dc)
{
lock (dcList)
lock (dcListLock)
{
dcList.Remove(dc);
}
Expand All @@ -200,7 +201,7 @@ public static int RegisteredCount
{
get
{
lock (dcList)
lock (dcListLock)
{
return dcList.Count;
}
Expand All @@ -211,7 +212,7 @@ public static bool IsAnyWaiting
{
get
{
lock (dcList)
lock (dcListLock)
{
foreach (DelayedCall dc in dcList)
{
Expand All @@ -227,7 +228,7 @@ public static bool IsAnyWaiting

public static void CancelAll()
{
lock (dcList)
lock (dcListLock)
{
foreach (DelayedCall dc in dcList)
{
Expand All @@ -238,7 +239,7 @@ public static void CancelAll()

public static void FireAll()
{
lock (dcList)
lock (dcListLock)
{
foreach (DelayedCall dc in dcList)
{
Expand All @@ -249,7 +250,7 @@ public static void FireAll()

public static void DisposeAll()
{
lock (dcList)
lock (dcListLock)
{
while (dcList.Count > 0)
{
Expand Down
4 changes: 3 additions & 1 deletion src/ReaLTaiizor/Extension/Poison/PoisonBrushes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using ReaLTaiizor.Colors;
using System.Collections.Generic;
using System.Drawing;
using System.Threading;

#endregion

Expand All @@ -12,10 +13,11 @@ namespace ReaLTaiizor.Extension.Poison

public sealed class PoisonBrushes
{
private static readonly Lock syncRoot = new();
private static readonly Dictionary<string, SolidBrush> poisonBrushes = new();
private static SolidBrush GetSaveBrush(string key, Color color)
{
lock (poisonBrushes)
lock (syncRoot)
{
if (!poisonBrushes.ContainsKey(key))
{
Expand Down
4 changes: 3 additions & 1 deletion src/ReaLTaiizor/Extension/Poison/PoisonPens.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using ReaLTaiizor.Colors;
using System.Collections.Generic;
using System.Drawing;
using System.Threading;

#endregion

Expand All @@ -12,10 +13,11 @@ namespace ReaLTaiizor.Extension.Poison

public sealed class PoisonPens
{
private static readonly Lock syncRoot = new();
private static readonly Dictionary<string, Pen> poisonPens = new();
private static Pen GetSavePen(string key, Color color)
{
lock (poisonPens)
lock (syncRoot)
{
if (!poisonPens.ContainsKey(key))
{
Expand Down
4 changes: 4 additions & 0 deletions src/ReaLTaiizor/ReaLTaiizor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ Changes are detailed at https://github.com/Taiizor/ReaLTaiizor/releases</Package
<Reference Include="Microsoft.VisualBasic" />
</ItemGroup>

<ItemGroup Condition="!$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net9.0'))">
<PackageReference Include="Backport.System.Threading.Lock" Version="1.1.6" />
</ItemGroup>

<ItemGroup>
<None Include="..\README.MD">
<Pack>True</Pack>
Expand Down
4 changes: 3 additions & 1 deletion src/ReaLTaiizor/Resolver/Poison/FontResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading;

#endregion

Expand Down Expand Up @@ -34,6 +35,7 @@
private const string OPEN_SANS_LIGHT = "Open Sans Light";
private const string OPEN_SANS_BOLD = "Open Sans Bold";

private readonly Lock syncRoot = new();
private readonly PrivateFontCollection fontCollection = new();

private static bool TryResolve(ref string familyName, ref FontStyle fontStyle)
Expand Down Expand Up @@ -66,7 +68,7 @@

private FontFamily GetFontFamily(string familyName)
{
lock (fontCollection)
lock (syncRoot)
{
foreach (FontFamily fontFamily in fontCollection.Families)
{
Expand All @@ -87,7 +89,7 @@
int bytes = (int)fontStream.Length;
data = Marshal.AllocCoTaskMem(bytes);
byte[] fontdata = new byte[bytes];
fontStream.Read(fontdata, 0, bytes);

Check warning on line 92 in src/ReaLTaiizor/Resolver/Poison/FontResolver.cs

View workflow job for this annotation

GitHub Actions / Build (GitHub, src)

Avoid inexact read with 'System.IO.Stream.Read(byte[], int, int)' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2022)

Check warning on line 92 in src/ReaLTaiizor/Resolver/Poison/FontResolver.cs

View workflow job for this annotation

GitHub Actions / Build (GitHub, src)

Avoid inexact read with 'System.IO.Stream.Read(byte[], int, int)' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2022)

Check warning on line 92 in src/ReaLTaiizor/Resolver/Poison/FontResolver.cs

View workflow job for this annotation

GitHub Actions / Build (GitHub, src/ReaLTaiizor.sln)

Avoid inexact read with 'System.IO.Stream.Read(byte[], int, int)' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2022)

Check warning on line 92 in src/ReaLTaiizor/Resolver/Poison/FontResolver.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp, GitHub, src)

Avoid inexact read with 'System.IO.Stream.Read(byte[], int, int)' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2022)

Check warning on line 92 in src/ReaLTaiizor/Resolver/Poison/FontResolver.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp, GitHub, src)

Avoid inexact read with 'System.IO.Stream.Read(byte[], int, int)' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2022)

Check warning on line 92 in src/ReaLTaiizor/Resolver/Poison/FontResolver.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp, GitHub, src)

Avoid inexact read with 'System.IO.Stream.Read(byte[], int, int)' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2022)

Check warning on line 92 in src/ReaLTaiizor/Resolver/Poison/FontResolver.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp, GitHub, src)

Avoid inexact read with 'System.IO.Stream.Read(byte[], int, int)' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2022)

Check warning on line 92 in src/ReaLTaiizor/Resolver/Poison/FontResolver.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp, GitHub, src)

Avoid inexact read with 'System.IO.Stream.Read(byte[], int, int)' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2022)
Marshal.Copy(fontdata, 0, data, bytes);
fontCollection.AddMemoryFont(data, bytes);
return fontCollection.Families[fontCollection.Families.Length - 1];
Expand Down
Loading