2019-11-09 06:37:05 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Runtime.CompilerServices;
|
|
|
|
|
using System.Text;
|
2019-11-10 22:15:52 +00:00
|
|
|
|
using System.Threading;
|
2019-11-09 06:37:05 +00:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace Wabbajack.Common.CSP
|
|
|
|
|
{
|
2019-11-09 21:29:55 +00:00
|
|
|
|
public class TakeTaskHandler<T> : Handler<Action<bool, T>>
|
2019-11-09 06:37:05 +00:00
|
|
|
|
{
|
|
|
|
|
private readonly bool _blockable;
|
2019-11-09 14:49:00 +00:00
|
|
|
|
private TaskCompletionSource<(bool, T)> _tcs;
|
2019-11-09 06:37:05 +00:00
|
|
|
|
|
|
|
|
|
public TakeTaskHandler(TaskCompletionSource<T> tcs = null, bool blockable = true)
|
|
|
|
|
{
|
|
|
|
|
_blockable = blockable;
|
2019-11-09 14:49:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public TaskCompletionSource<(bool, T)> TaskCompletionSource
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_tcs == null)
|
2019-11-10 22:15:52 +00:00
|
|
|
|
{
|
|
|
|
|
var new_tcs = new TaskCompletionSource<(bool, T)>();
|
|
|
|
|
Interlocked.CompareExchange(ref _tcs, new_tcs, null);
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-09 14:49:00 +00:00
|
|
|
|
return _tcs;
|
|
|
|
|
}
|
2019-11-09 06:37:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public bool IsActive => true;
|
|
|
|
|
public bool IsBlockable => _blockable;
|
|
|
|
|
public uint LockId => 0;
|
2019-11-09 14:49:00 +00:00
|
|
|
|
public Task<(bool, T)> Task => TaskCompletionSource.Task;
|
2019-11-09 21:29:55 +00:00
|
|
|
|
public Action<bool, T> Commit()
|
2019-11-09 06:37:05 +00:00
|
|
|
|
{
|
|
|
|
|
return Handle;
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-09 21:29:55 +00:00
|
|
|
|
private void Handle(bool is_open, T a)
|
2019-11-09 06:37:05 +00:00
|
|
|
|
{
|
2019-11-09 21:29:55 +00:00
|
|
|
|
TaskCompletionSource.SetResult((is_open, a));
|
2019-11-09 06:37:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|