using System; using System.Threading; using System.Threading.Tasks; namespace Wabbajack.Common.CSP { public class TakeTaskHandler : Handler> { private readonly bool _blockable; private TaskCompletionSource<(bool, T)> _tcs; public TakeTaskHandler(TaskCompletionSource tcs = null, bool blockable = true) { _blockable = blockable; } public TaskCompletionSource<(bool, T)> TaskCompletionSource { get { if (_tcs == null) { var new_tcs = new TaskCompletionSource<(bool, T)>(); Interlocked.CompareExchange(ref _tcs, new_tcs, null); } return _tcs; } } public bool IsActive => true; public bool IsBlockable => _blockable; public uint LockId => 0; public Task<(bool, T)> Task => TaskCompletionSource.Task; public Action Commit() { return Handle; } private void Handle(bool is_open, T a) { TaskCompletionSource.SetResult((is_open, a)); } } }