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

Lock optimisation through semaphore pooling #54

Open
wants to merge 1 commit into
base: master
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 3 additions & 22 deletions FolderSyncNet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@
<Reference Include="AsyncEnumerable, Version=4.0.2.0, Culture=neutral, PublicKeyToken=0426b068161bd1d1, processorArchitecture=MSIL">
<HintPath>packages\AsyncEnumerator.4.0.2\lib\net461\AsyncEnumerable.dll</HintPath>
</Reference>
<Reference Include="AsyncKeyedLock, Version=6.4.2.0, Culture=neutral, PublicKeyToken=c6dde91429ba0f2f, processorArchitecture=MSIL">
<HintPath>packages\AsyncKeyedLock.6.4.2\lib\netstandard2.0\AsyncKeyedLock.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Bcl.AsyncInterfaces, Version=7.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>packages\Microsoft.Bcl.AsyncInterfaces.7.0.0\lib\net462\Microsoft.Bcl.AsyncInterfaces.dll</HintPath>
</Reference>
Expand Down Expand Up @@ -329,31 +332,9 @@
<None Include="app.manifest">
<SubType>Designer</SubType>
</None>
<None Include="appsettings.debug6.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="appsettings.debug2.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="appsettings.debug7.json" />
<None Include="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="appsettings.prev.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="appsettings.debug5.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="appsettings.example.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="appsettings.debug3.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="appsettings.ipcam.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="FolderSync.bat">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
Expand Down
2 changes: 1 addition & 1 deletion InitialScan.cs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ private static IAsyncEnumerable<FileInfo> ProcessSubDirs(FolderSyncNetSource.Dir
var historyFileInfosDict = new Dictionary<string, CachedFileInfo>();
//var historyFileInfosTask = Task.CompletedTask;

AsyncLockQueueDictionary<string>.LockDictReleaser destOrHistoryDirCacheLock = null;
IDisposable destOrHistoryDirCacheLock = null;
//AsyncLockQueueDictionary<string>.LockDictReleaser historyDirCacheLock = null;

FileInfo[] fileInfos = null;
Expand Down
129 changes: 16 additions & 113 deletions Synchronisation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,141 +7,43 @@
//

#define ASYNC
using AsyncKeyedLock;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using Nito.AsyncEx;

namespace FolderSync
{
public class AsyncLockQueueDictionary<KeyT>
where KeyT : IComparable<KeyT>, IEquatable<KeyT>
{
private static readonly bool IsStringDictionary = typeof(KeyT) == typeof(string);

private readonly object DictionaryAccessMutex = new object();
//private readonly SemaphoreSlim DictionaryAccessMutex = new SemaphoreSlim(1, 1);
private readonly Dictionary<KeyT, AsyncLockWithWaiterCount> LockQueueDictionary = new Dictionary<KeyT, AsyncLockWithWaiterCount>();

public sealed class AsyncLockWithWaiterCount
private readonly AsyncKeyedLocker<KeyT> LockQueueDictionary = new AsyncKeyedLocker<KeyT>(o =>
{
public readonly AsyncLock LockEntry;
#pragma warning disable S1104 //Warning S1104 Make this field 'private' and encapsulate it in a 'public' property.
public int WaiterCount;
#pragma warning restore S1104

public AsyncLockWithWaiterCount()
{
this.LockEntry = new AsyncLock();
this.WaiterCount = 1;
}
}
o.PoolSize = 20;
o.PoolInitialFill = 1;
});

public sealed class LockDictReleaser : IDisposable //TODO: implement IAsyncDisposable in .NET 5.0
{
private readonly KeyT Name;
private readonly AsyncLockWithWaiterCount LockEntry;
#if !NOASYNC
private readonly IDisposable LockHandle;
#endif
private readonly AsyncLockQueueDictionary<KeyT> AsyncLockQueueDictionary;

#if NOASYNC
internal LockDictReleaser(KeyT name, AsyncLockWithWaiterCount lockEntry, AsyncLockQueueDictionary<KeyT> asyncLockQueueDictionary)
#else
internal LockDictReleaser(KeyT name, AsyncLockWithWaiterCount lockEntry, IDisposable lockHandle, AsyncLockQueueDictionary<KeyT> asyncLockQueueDictionary)
#endif
{
this.Name = name;
this.LockEntry = lockEntry;
#if !NOASYNC
this.LockHandle = lockHandle;
#endif
this.AsyncLockQueueDictionary = asyncLockQueueDictionary;
}

public void Dispose()
{
#if NOASYNC
this.AsyncLockQueueDictionary.ReleaseLock(this.Name, this.LockEntry);
#else
this.AsyncLockQueueDictionary.ReleaseLock(this.Name, this.LockEntry, this.LockHandle);
#endif
}
}
private static readonly bool IsStringDictionary = typeof(KeyT) == typeof(string);

#if NOASYNC
private void ReleaseLock(KeyT name, AsyncLockWithWaiterCount lockEntry)
#else
private void ReleaseLock(KeyT name, AsyncLockWithWaiterCount lockEntry, IDisposable lockHandle)
#endif
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public async ValueTask<IDisposable> LockAsync(KeyT name)
{
#if NOASYNC
Monitor.Exit(lockEntry.LockEntry);
#else
lockHandle.Dispose();
#endif

lock (DictionaryAccessMutex)
//DictionaryAccessMutex.Wait();
//try
{
lockEntry.WaiterCount--;
Debug.Assert(lockEntry.WaiterCount >= 0);

if (lockEntry.WaiterCount == 0) //NB!
{
LockQueueDictionary.Remove(name);
}
}
//finally
//{
// DictionaryAccessMutex.Release();
//}
return await LockQueueDictionary.LockAsync(name).ConfigureAwait(false);
}

public async Task<LockDictReleaser> LockAsync(KeyT name, CancellationToken cancellationToken = default(CancellationToken))
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public async ValueTask<IDisposable> LockAsync(KeyT name, CancellationToken cancellationToken)
{
AsyncLockWithWaiterCount lockEntry;
#pragma warning disable PH_S023 //Message PH_S023 Using the blocking synchronization mechanics of a monitor inside an async method is discouraged; use SemaphoreSlim instead.
lock (DictionaryAccessMutex)
#pragma warning restore PH_S023
//await DictionaryAccessMutex.WaitAsync(cancellationToken);
//try
{
if (!LockQueueDictionary.TryGetValue(name, out lockEntry))
{
lockEntry = new AsyncLockWithWaiterCount();
LockQueueDictionary.Add(name, lockEntry);
}
else
{
lockEntry.WaiterCount++; //NB! must be done inside the lock and BEFORE waiting for the lock
}
}
//finally
//{
// DictionaryAccessMutex.Release();
//}

#if NOASYNC
#pragma warning disable PH_P006 //warning PH_P006 Favor the use of the lock-statement instead of the use of Monitor.Enter when no timeouts are needed.
Monitor.Enter(lockEntry.LockEntry);
#pragma warning restore PH_P006
return new LockDictReleaser(name, lockEntry, this);
#else
var lockHandle = await lockEntry.LockEntry.LockAsync(cancellationToken);
return new LockDictReleaser(name, lockEntry, lockHandle, this);
#endif
return await LockQueueDictionary.LockAsync(name, cancellationToken).ConfigureAwait(false);
}

public sealed class MultiLockDictReleaser : IDisposable //TODO: implement IAsyncDisposable in .NET 5.0
{
private readonly LockDictReleaser[] Releasers;
private readonly IDisposable[] Releasers;

public MultiLockDictReleaser(params LockDictReleaser[] releasers)
public MultiLockDictReleaser(params IDisposable[] releasers)
{
this.Releasers = releasers;
}
Expand All @@ -156,6 +58,7 @@ public void Dispose()
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public async Task<MultiLockDictReleaser> LockAsync(KeyT name1, KeyT name2, CancellationToken cancellationToken = default(CancellationToken))
{
var names = new List<KeyT>()
Expand Down
1 change: 1 addition & 0 deletions packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<package id="AlphaVSS" version="2.0.0" targetFramework="net48" />
<package id="AlphaVSS.Native.NetFx" version="2.0.0" targetFramework="net48" />
<package id="AsyncEnumerator" version="4.0.2" targetFramework="net48" />
<package id="AsyncKeyedLock" version="6.4.2" targetFramework="net48" />
<package id="Microsoft.Bcl.AsyncInterfaces" version="7.0.0" targetFramework="net48" />
<package id="Microsoft.Extensions.Configuration" version="7.0.0" targetFramework="net48" />
<package id="Microsoft.Extensions.Configuration.Abstractions" version="7.0.0" targetFramework="net48" />
Expand Down