added missing documentation

This commit is contained in:
Christian Köllner 2020-04-20 21:36:06 +02:00
parent 8f74ad79c0
commit 16f95e8f8f

View File

@ -7,6 +7,11 @@ using System.Threading.Tasks;
namespace Capnp.Util namespace Capnp.Util
{ {
/// <summary>
/// A task-like object which enforces that all await operations from the same thread leave in the exact order they were issued.
/// Note that an ordinary .NET Task does not fulfill this requirement if completed by a thread which is different from the
/// awaiting thread.
/// </summary>
public class StrictlyOrderedAwaitTask: INotifyCompletion public class StrictlyOrderedAwaitTask: INotifyCompletion
{ {
class Cover { } class Cover { }
@ -18,12 +23,20 @@ namespace Capnp.Util
readonly Task _awaitedTask; readonly Task _awaitedTask;
object? _state; object? _state;
/// <summary>
/// Constructs an instance
/// </summary>
/// <param name="awaitedTask">Task on which the order shall be enforced</param>
public StrictlyOrderedAwaitTask(Task awaitedTask) public StrictlyOrderedAwaitTask(Task awaitedTask)
{ {
_awaitedTask = awaitedTask; _awaitedTask = awaitedTask;
_state = s_cover; _state = s_cover;
} }
/// <summary>
/// await pattern implementation
/// </summary>
/// <returns>An object suitable for the await pattern</returns>
public StrictlyOrderedAwaitTask GetAwaiter() public StrictlyOrderedAwaitTask GetAwaiter()
{ {
return this; return this;
@ -55,6 +68,9 @@ namespace Capnp.Util
} }
} }
/// <summary>
/// Part of await pattern implementation. Do not use directly.
/// </summary>
public void OnCompleted(Action continuation) public void OnCompleted(Action continuation)
{ {
bool first = false; bool first = false;
@ -92,36 +108,82 @@ namespace Capnp.Util
} }
} }
/// <summary>
/// Whether the underlying task did complete and it is safe to skip continuation registration.
/// </summary>
public bool IsCompleted => _awaitedTask.IsCompleted && _state == s_seal; public bool IsCompleted => _awaitedTask.IsCompleted && _state == s_seal;
/// <summary>
/// Part of await pattern implementation. Do not use directly.
/// </summary>
public void GetResult() => _awaitedTask.GetAwaiter().GetResult(); public void GetResult() => _awaitedTask.GetAwaiter().GetResult();
/// <summary>
/// Task on which the order shall be enforced.
/// </summary>
public Task WrappedTask => _awaitedTask; public Task WrappedTask => _awaitedTask;
} }
/// <summary>
/// A task-like object which enforces that all await operations from the same thread leave in the exact order they were issued.
/// Note that an ordinary .NET Task does not fulfill this requirement if completed by a thread which is different from the
/// awaiting thread.
/// </summary>
public class StrictlyOrderedAwaitTask<T> : StrictlyOrderedAwaitTask public class StrictlyOrderedAwaitTask<T> : StrictlyOrderedAwaitTask
{ {
/// <summary>
/// Constructs an instance
/// </summary>
/// <param name="awaitedTask">Task on which the order shall be enforced</param>
public StrictlyOrderedAwaitTask(Task<T> awaitedTask): base(awaitedTask) public StrictlyOrderedAwaitTask(Task<T> awaitedTask): base(awaitedTask)
{ {
} }
/// <summary>
/// Task on which the order shall be enforced.
/// </summary>
public new Task<T> WrappedTask => (Task<T>)base.WrappedTask; public new Task<T> WrappedTask => (Task<T>)base.WrappedTask;
/// <summary>
/// await pattern implementation
/// </summary>
/// <returns>An object suitable for the await pattern</returns>
public new StrictlyOrderedAwaitTask<T> GetAwaiter() => this; public new StrictlyOrderedAwaitTask<T> GetAwaiter() => this;
/// <summary>
/// Part of await pattern implementation. Do not use directly.
/// </summary>
public new T GetResult() => WrappedTask.GetAwaiter().GetResult(); public new T GetResult() => WrappedTask.GetAwaiter().GetResult();
/// <summary>
/// Redirects to the wrapped Task's result.
/// </summary>
public T Result => WrappedTask.Result; public T Result => WrappedTask.Result;
} }
/// <summary>
/// Extension methods to simplify the use of <see cref="StrictlyOrderedAwaitTask"/>
/// </summary>
public static class StrictlyOrderedTaskExtensions public static class StrictlyOrderedTaskExtensions
{ {
/// <summary>
/// Converts the task to a task-like object which enforces that all await operations from the same thread leave in the exact order they were issued.
/// </summary>
/// <typeparam name="T">The type of the result produced by the Task</typeparam>
/// <param name="task">Task to wrap</param>
/// <returns>awaitable object</returns>
public static StrictlyOrderedAwaitTask<T> EnforceAwaitOrder<T>(this Task<T> task) public static StrictlyOrderedAwaitTask<T> EnforceAwaitOrder<T>(this Task<T> task)
{ {
return new StrictlyOrderedAwaitTask<T>(task); return new StrictlyOrderedAwaitTask<T>(task);
} }
/// <summary>
/// Converts the task to a task-like object which enforces that all await operations from the same thread leave in the exact order they were issued.
/// </summary>
/// <param name="task">Task to wrap</param>
/// <returns>awaitable object</returns>
public static StrictlyOrderedAwaitTask EnforceAwaitOrder(this Task task) public static StrictlyOrderedAwaitTask EnforceAwaitOrder(this Task task)
{ {
return new StrictlyOrderedAwaitTask(task); return new StrictlyOrderedAwaitTask(task);