Changed locks to use the .NET 9 Lock-Type if possible

This commit is contained in:
DarthAffe 2025-06-10 23:06:34 +02:00
parent af9f75c63e
commit d0388f8c75
12 changed files with 63 additions and 27 deletions

View File

@ -35,7 +35,8 @@ public sealed class DX11ScreenCapture : AbstractScreenCapture<ColorBGRA>
#region Properties & Fields
private readonly object _captureLock = new();
private readonly Lock _captureLock = new();
private readonly Lock _texturesLock = new();
private readonly bool _useNewDuplicationAdapter;
@ -140,7 +141,7 @@ public sealed class DX11ScreenCapture : AbstractScreenCapture<ColorBGRA>
{
if (_context == null) return;
lock (_textures)
lock (_texturesLock)
{
if (!_textures.TryGetValue(captureZone, out ZoneTextures? textures)) return;
@ -258,7 +259,7 @@ public sealed class DX11ScreenCapture : AbstractScreenCapture<ColorBGRA>
{
CaptureZone<ColorBGRA> captureZone = base.RegisterCaptureZone(x, y, width, height, downscaleLevel);
lock (_textures)
lock (_texturesLock)
InitializeCaptureZone(captureZone);
return captureZone;
@ -269,7 +270,7 @@ public sealed class DX11ScreenCapture : AbstractScreenCapture<ColorBGRA>
{
if (!base.UnregisterCaptureZone(captureZone)) return false;
lock (_textures)
lock (_texturesLock)
{
if (_textures.TryGetValue(captureZone, out ZoneTextures? textures))
{
@ -291,7 +292,7 @@ public sealed class DX11ScreenCapture : AbstractScreenCapture<ColorBGRA>
//TODO DarthAffe 01.05.2022: For now just reinitialize the zone in that case, but this could be optimized to only recreate the textures needed.
if ((width != null) || (height != null) || (downscaleLevel != null))
{
lock (_textures)
lock (_texturesLock)
{
if (_textures.TryGetValue(captureZone, out ZoneTextures? textures))
{
@ -383,7 +384,7 @@ public sealed class DX11ScreenCapture : AbstractScreenCapture<ColorBGRA>
base.Restart();
lock (_captureLock)
lock (_textures)
lock (_texturesLock)
{
try
{

View File

@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net8.0</TargetFrameworks>
<TargetFrameworks>net8.0;net9.0</TargetFrameworks>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
@ -54,6 +54,10 @@
<DefineConstants>$(DefineConstants);RELEASE</DefineConstants>
</PropertyGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
<Using Include="ScreenCapture.NET.Compatibility.Net8" />
</ItemGroup>
<ItemGroup>
<None Include="Resources\icon.png">
<Pack>True</Pack>

View File

@ -17,7 +17,7 @@ public sealed class DX9ScreenCapture : AbstractScreenCapture<ColorBGRA>
{
#region Properties & Fields
private readonly object _captureLock = new();
private readonly Lock _captureLock = new();
private readonly IDirect3D9 _direct3D9;
private IDirect3DDevice9? _device;

View File

@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net8.0</TargetFrameworks>
<TargetFrameworks>net8.0;net9.0</TargetFrameworks>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
@ -54,6 +54,10 @@
<DefineConstants>$(DefineConstants);RELEASE</DefineConstants>
</PropertyGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
<Using Include="ScreenCapture.NET.Compatibility.Net8" />
</ItemGroup>
<ItemGroup>
<None Include="Resources\icon.png">
<Pack>True</Pack>

View File

@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net8.0</TargetFrameworks>
<TargetFrameworks>net8.0;net9.0</TargetFrameworks>
<RuntimeIdentifier>linux-x64</RuntimeIdentifier>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
@ -54,6 +54,10 @@
<DefineConstants>$(DefineConstants);RELEASE</DefineConstants>
</PropertyGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
<Using Include="ScreenCapture.NET.Compatibility.Net8" />
</ItemGroup>
<ItemGroup>
<None Include="Resources\icon.png">
<Pack>True</Pack>

View File

@ -1,6 +1,7 @@
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Threading;
using HPPH;
namespace ScreenCapture.NET;
@ -14,7 +15,7 @@ public sealed class X11ScreenCapture : AbstractScreenCapture<ColorBGRA>
{
#region Properties & Fields
private readonly object _captureLock = new();
private readonly Lock _captureLock = new();
private nint _display;
private nint _drawable;
@ -62,13 +63,12 @@ public sealed class X11ScreenCapture : AbstractScreenCapture<ColorBGRA>
protected override void PerformCaptureZoneUpdate(CaptureZone<ColorBGRA> captureZone, Span<byte> buffer)
{
using IDisposable @lock = captureZone.Lock();
{
if (captureZone.DownscaleLevel == 0)
CopyZone(captureZone, buffer);
else
DownscaleZone(captureZone, buffer);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void CopyZone(CaptureZone<ColorBGRA> captureZone, Span<byte> buffer)

View File

@ -0,0 +1,8 @@
#if NET8_0
// ReSharper disable once CheckNamespace
namespace ScreenCapture.NET.Compatibility.Net8;
public sealed class Lock;
#endif

View File

@ -119,7 +119,6 @@ public static class BlackBarDetection
#endregion
#region Image
/// <summary>

View File

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using HPPH;
namespace ScreenCapture.NET;
@ -13,10 +14,12 @@ public abstract class AbstractScreenCapture<TColor> : IScreenCapture
private bool _isDisposed;
protected readonly Lock CaptureZonesLock = new();
/// <summary>
/// Gets a list of <see cref="CaptureZone{TColol}"/> registered on this ScreenCapture.
/// </summary>
protected HashSet<CaptureZone<TColor>> CaptureZones { get; } = new();
protected HashSet<CaptureZone<TColor>> CaptureZones { get; } = [];
/// <inheritdoc />
public Display Display { get; }
@ -63,7 +66,7 @@ public abstract class AbstractScreenCapture<TColor> : IScreenCapture
result = false;
}
lock (CaptureZones)
lock (CaptureZonesLock)
foreach (CaptureZone<TColor> captureZone in CaptureZones.Where(x => x.AutoUpdate || x.IsUpdateRequested))
{
try
@ -113,7 +116,7 @@ public abstract class AbstractScreenCapture<TColor> : IScreenCapture
{
if (_isDisposed) throw new ObjectDisposedException(GetType().FullName);
lock (CaptureZones)
lock (CaptureZonesLock)
{
ValidateCaptureZoneAndThrow(x, y, width, height, downscaleLevel);
@ -195,7 +198,7 @@ public abstract class AbstractScreenCapture<TColor> : IScreenCapture
{
if (_isDisposed) throw new ObjectDisposedException(GetType().FullName);
lock (CaptureZones)
lock (CaptureZonesLock)
{
if (!CaptureZones.Contains(captureZone))
throw new ArgumentException("The capture zone is not registered to this ScreenCapture", nameof(captureZone));

View File

@ -16,7 +16,7 @@ public sealed class CaptureZone<TColor> : ICaptureZone
{
#region Properties & Fields
private readonly object _lock = new();
private readonly Lock _lock = new();
/// <inheritdoc />
public Display Display { get; }
@ -150,7 +150,11 @@ public sealed class CaptureZone<TColor> : ICaptureZone
/// <inheritdoc />
public IDisposable Lock()
{
#if NET8_0
Monitor.Enter(_lock);
#else
_lock.Enter();
#endif
return new UnlockDisposable(_lock);
}
@ -187,14 +191,14 @@ public sealed class CaptureZone<TColor> : ICaptureZone
#region Properties & Fields
private bool _disposed = false;
private readonly object _lock;
private readonly Lock _lock;
#endregion
#region Constructors
// ReSharper disable once ConvertToPrimaryConstructor
public UnlockDisposable(object @lock) => this._lock = @lock;
public UnlockDisposable(Lock @lock) => this._lock = @lock;
~UnlockDisposable() => Dispose();
#endregion
@ -206,7 +210,12 @@ public sealed class CaptureZone<TColor> : ICaptureZone
{
ObjectDisposedException.ThrowIf(_disposed, this);
#if NET8_0
Monitor.Exit(_lock);
#else
_lock.Exit();
#endif
_disposed = true;
GC.SuppressFinalize(this);

View File

@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net8.0</TargetFrameworks>
<TargetFrameworks>net9.0;net8.0</TargetFrameworks>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
@ -53,6 +53,10 @@
<DefineConstants>$(DefineConstants);RELEASE</DefineConstants>
</PropertyGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
<Using Include="ScreenCapture.NET.Compatibility.Net8" />
</ItemGroup>
<ItemGroup>
<None Include="Resources\icon.png">
<Pack>True</Pack>

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net8.0</TargetFrameworks>
<TargetFrameworks>net9.0</TargetFrameworks>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>