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 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
2 changes: 1 addition & 1 deletion ReaLTaiizor.sln
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{AC2E7142-86C9-466A-A805-FF099FAAF1E5}"
ProjectSection(SolutionItems) = preProject
.editorconfig = .editorconfig
NuGet.Config = NuGet.Config
global.json = global.json
NuGet.Config = NuGet.Config
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "demo", "demo", "{F2C87832-DD5E-49C3-9EB9-5F6F8421A594}"
Expand Down
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 = LockFactory.Create();
private Callback callback;
protected bool cancelled = false;

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

protected DelayedCall()
{
timerLock = new object();
timerLock = LockFactory.Create();
}

#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
3 changes: 2 additions & 1 deletion src/ReaLTaiizor/Extension/Poison/PoisonBrushes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ namespace ReaLTaiizor.Extension.Poison

public sealed class PoisonBrushes
{
private static readonly Lock syncRoot = LockFactory.Create();
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
3 changes: 2 additions & 1 deletion src/ReaLTaiizor/Extension/Poison/PoisonPens.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ namespace ReaLTaiizor.Extension.Poison

public sealed class PoisonPens
{
private static readonly Lock syncRoot = LockFactory.Create();
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
9 changes: 8 additions & 1 deletion src/ReaLTaiizor/ReaLTaiizor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ Changes are detailed at https://github.com/Taiizor/ReaLTaiizor/releases</Package
<NoWarn>1587,1591</NoWarn>
</PropertyGroup>

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

<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|AnyCPU' OR '$(Configuration)|$(Platform)' == 'GitHub|AnyCPU' OR '$(Configuration)|$(Platform)' == 'Release|AnyCPU'">
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
Expand All @@ -71,6 +77,7 @@ Changes are detailed at https://github.com/Taiizor/ReaLTaiizor/releases</Package
<Version>8.0.0</Version>
<PrivateAssets>All</PrivateAssets>
</PackageReference>
<PackageReference Include="Backport.System.Threading.Lock" Version="3.1.0" />
</ItemGroup>

<ItemGroup Condition="$(TargetFramework.StartsWith('net48'))">
Expand Down Expand Up @@ -307,4 +314,4 @@ Changes are detailed at https://github.com/Taiizor/ReaLTaiizor/releases</Package
</None>
</ItemGroup>

</Project>
</Project>
3 changes: 2 additions & 1 deletion src/ReaLTaiizor/Resolver/Poison/FontResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public Font ResolveFont(string familyName, float emSize, FontStyle fontStyle, Gr
private const string OPEN_SANS_LIGHT = "Open Sans Light";
private const string OPEN_SANS_BOLD = "Open Sans Bold";

private readonly Lock syncRoot = LockFactory.Create();
private readonly PrivateFontCollection fontCollection = new();

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

private FontFamily GetFontFamily(string familyName)
{
lock (fontCollection)
lock (syncRoot)
{
foreach (FontFamily fontFamily in fontCollection.Families)
{
Expand Down
Loading