Skip to content

Commit

Permalink
Added SemaphoreSlimExtensions and removed Nito.AsyncEx dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
axunonb committed Jan 4, 2021
1 parent 107276b commit 39e5514
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 8 deletions.
9 changes: 4 additions & 5 deletions Src/Axuno.TextTemplating/Axuno.TextTemplating.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

The library is a modified version of Volo.Abp.TextTemplating 4.1</Description>
<Copyright>© 2013 - 2021 Volosoft. Open source license with LGPLv3.0</Copyright>
<Version>0.8.5</Version>
<Version>0.8.6</Version>
<Authors>axuno gGmbH</Authors>
<PackageLicenseExpression>LGPL-3.0-only</PackageLicenseExpression>
<PackageTags>text templating netstandard c#</PackageTags>
Expand All @@ -27,10 +27,10 @@ The library is a modified version of Volo.Abp.TextTemplating 4.1</Description>
<PackageProjectUrl>https://github.com/axuno/Axuno.TextTemplating</PackageProjectUrl>
<PackageReleaseNotes>* Updated Nuget Package Scriban 3.3.2
* Implemented fixes of Abp framework from commit https://github.com/abpframework/abp/commit/d495686fd2f6256abdeb0b6eacfd9f73d6f961f0
* Bugfix</PackageReleaseNotes>
* Added SemaphoreSlimExtensions and removed Nito.AsyncEx dependency</PackageReleaseNotes>
<OutputType>Library</OutputType>
<AssemblyVersion>0.8.5.0</AssemblyVersion>
<FileVersion>0.8.5.0</FileVersion>
<AssemblyVersion>0.8.6.0</AssemblyVersion>
<FileVersion>0.8.6.0</FileVersion>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
Expand Down Expand Up @@ -58,7 +58,6 @@ The library is a modified version of Volo.Abp.TextTemplating 4.1</Description>
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Nito.AsyncEx" Version="5.1.0" />
<PackageReference Include="Scriban" Version="3.3.2" />
<PackageReference Include="System.Collections.Immutable" Version="5.0.0" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@
using System.Collections.Concurrent;
using System.Threading;
using System.Threading.Tasks;
using Axuno.TextTemplating;
using Axuno.TextTemplating.VirtualFiles;
using Axuno.VirtualFileSystem;
using Microsoft.Extensions.FileProviders;
using Nito.AsyncEx;

namespace Axuno.TextTemplating.VirtualFiles
{
Expand Down
117 changes: 117 additions & 0 deletions Src/Axuno.TextTemplating/VirtualFiles/SemaphoreSlimExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
using System;
using System.Threading;
using System.Threading.Tasks;

namespace Axuno.TextTemplating.VirtualFiles
{
public static class SemaphoreSlimExtensions
{
public static async Task<IDisposable> LockAsync(this SemaphoreSlim semaphoreSlim)
{
await semaphoreSlim.WaitAsync();
return GetDispose(semaphoreSlim);
}

public static async Task<IDisposable> LockAsync(this SemaphoreSlim semaphoreSlim, CancellationToken cancellationToken)
{
await semaphoreSlim.WaitAsync(cancellationToken);
return GetDispose(semaphoreSlim);
}

public static async Task<IDisposable> LockAsync(this SemaphoreSlim semaphoreSlim, int millisecondsTimeout)
{
await semaphoreSlim.WaitAsync(millisecondsTimeout);
return GetDispose(semaphoreSlim);
}

public static async Task<IDisposable> LockAsync(this SemaphoreSlim semaphoreSlim, int millisecondsTimeout, CancellationToken cancellationToken)
{
await semaphoreSlim.WaitAsync(millisecondsTimeout, cancellationToken);
return GetDispose(semaphoreSlim);
}

public static async Task<IDisposable> LockAsync(this SemaphoreSlim semaphoreSlim, TimeSpan timeout)
{
await semaphoreSlim.WaitAsync(timeout);
return GetDispose(semaphoreSlim);
}

public static async Task<IDisposable> LockAsync(this SemaphoreSlim semaphoreSlim, TimeSpan timeout, CancellationToken cancellationToken)
{
await semaphoreSlim.WaitAsync(timeout, cancellationToken);
return GetDispose(semaphoreSlim);
}

public static IDisposable Lock(this SemaphoreSlim semaphoreSlim)
{
semaphoreSlim.Wait();
return GetDispose(semaphoreSlim);
}

public static IDisposable Lock(this SemaphoreSlim semaphoreSlim, CancellationToken cancellationToken)
{
semaphoreSlim.Wait(cancellationToken);
return GetDispose(semaphoreSlim);
}

public static IDisposable Lock(this SemaphoreSlim semaphoreSlim, int millisecondsTimeout)
{
semaphoreSlim.Wait(millisecondsTimeout);
return GetDispose(semaphoreSlim);
}

public static IDisposable Lock(this SemaphoreSlim semaphoreSlim, int millisecondsTimeout, CancellationToken cancellationToken)
{
semaphoreSlim.Wait(millisecondsTimeout, cancellationToken);
return GetDispose(semaphoreSlim);
}

public static IDisposable Lock(this SemaphoreSlim semaphoreSlim, TimeSpan timeout)
{
semaphoreSlim.Wait(timeout);
return GetDispose(semaphoreSlim);
}

public static IDisposable Lock(this SemaphoreSlim semaphoreSlim, TimeSpan timeout, CancellationToken cancellationToken)
{
semaphoreSlim.Wait(timeout, cancellationToken);
return GetDispose(semaphoreSlim);
}

private static IDisposable GetDispose(this SemaphoreSlim semaphoreSlim)
{
return new DisposeAction(() =>
{
semaphoreSlim.Release();
});
}

/// <summary>
/// This class can be used to provide an action when
/// Dispose method is called.
/// </summary>
public class DisposeAction : IDisposable
{
private readonly Action _action;

/// <summary>
/// Creates a new <see cref="DisposeAction"/> object.
/// </summary>
/// <param name="action">Action to be executed when this object is disposed.</param>
public DisposeAction(Action action)
{
Check.NotNull(action, nameof(action));

_action = action;
}

/// <summary>
/// Invokes the <see cref="Action"/>.
/// </summary>
public void Dispose()
{
_action();
}
}
}
}

0 comments on commit 39e5514

Please sign in to comment.