Unverified Commit 8317b958 authored by Lemon's avatar Lemon Committed by GitHub

Add AsyncLock (#44)

parent fb7fb71a
......@@ -17,14 +17,11 @@
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using Nito.AsyncEx;
using SkyWalking.Config;
using SkyWalking.Logging;
using SkyWalking.Utils;
namespace SkyWalking.Remote
{
......
......@@ -17,7 +17,6 @@
<ProjectReference Include="..\SkyWalking.NetworkProtocol\SkyWalking.NetworkProtocol.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Nito.AsyncEx.Coordination" Version="5.0.0-pre-05" />
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="4.4.1" />
</ItemGroup>
</Project>
\ No newline at end of file
/*
* Licensed to the OpenSkywalking under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
using System;
using System.Threading;
using System.Threading.Tasks;
namespace SkyWalking.Utils
{
internal class AsyncLock
{
private readonly SemaphoreSlim _semaphore = new SemaphoreSlim(1);
private readonly Releaser _releaser;
private readonly Task<Releaser> _releaserTask;
public AsyncLock()
{
_releaser = new Releaser(this);
_releaserTask = Task.FromResult(_releaser);
}
public Task<Releaser> LockAsync(CancellationToken cancellationToken = default(CancellationToken))
{
var wait = _semaphore.WaitAsync(cancellationToken);
return wait.IsCompleted
? _releaserTask
: wait.ContinueWith(
(_, state) => ((AsyncLock) state)._releaser,
this, CancellationToken.None,
TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default);
}
public Releaser Lock()
{
_semaphore.Wait();
return _releaser;
}
public struct Releaser : IDisposable
{
private readonly AsyncLock _toRelease;
internal Releaser(AsyncLock toRelease)
{
_toRelease = toRelease;
}
public void Dispose()
=> _toRelease._semaphore.Release();
}
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment