| | | 1 | | using System.Runtime.CompilerServices; |
| | | 2 | | |
| | | 3 | | namespace CounterpointCollective.Collections |
| | | 4 | | { |
| | | 5 | | /// <summary> |
| | | 6 | | /// Represents a node in a <see cref="CoalescingQueue"/> for an item of type <typeparamref name="TValue"/>. |
| | | 7 | | /// </summary> |
| | | 8 | | /// <typeparam name="TValue">The type of the item managed by this queue node.</typeparam> |
| | | 9 | | public readonly ref struct CoalescingQueueNode<TContainer, TValue> where TContainer : struct |
| | | 10 | | { |
| | | 11 | | private readonly ref QueueEntry<TContainer> _entry; |
| | | 12 | | private readonly RawValueProvider<TContainer, TValue> _valueProvider; |
| | | 13 | | |
| | | 14 | | internal CoalescingQueueNode(StructQueue<QueueEntry<TContainer>>.Handle inner, RawValueProvider<TContainer, TVal |
| | | 15 | | { |
| | 1199834 | 16 | | _valueProvider = valueProvider; |
| | 1199834 | 17 | | _entry = ref inner.GetRef(); |
| | 1199834 | 18 | | } |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// Causes the value to be dispatched after <see cref="Dispose"/> is called. |
| | | 22 | | /// </summary> |
| | | 23 | | public void MarkForDispatch() |
| | 2099211 | 24 | | => _entry.MarkedForDispatch = true; |
| | | 25 | | |
| | | 26 | | /// <returns> |
| | | 27 | | /// An instance of <typeparamref name="TValue"/> representing the node's value. |
| | | 28 | | /// </returns> |
| | 2000007 | 29 | | public ref TValue Value => ref _valueProvider(ref _entry); |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Marks this node as completed. |
| | | 33 | | /// </summary> |
| | | 34 | | /// <remarks> |
| | | 35 | | /// If the node's value has been claimed, calling <see cref="Dispose"/> will eventually dispatch it. |
| | | 36 | | /// If the value has not been claimed, nothing is dispatched. |
| | | 37 | | /// </remarks> |
| | | 38 | | public void Dispose() |
| | | 39 | | { |
| | | 40 | | //Save the reference. In case this gets recycled by the dispatcher before we hit the onCompleted call. |
| | 1199833 | 41 | | var handle = _entry.Handle; |
| | 1199833 | 42 | | var v = _entry.UnRef(); |
| | 1199833 | 43 | | if (v == 0) |
| | | 44 | | { |
| | 1199831 | 45 | | handle.OnNodeCompleted(); |
| | | 46 | | } |
| | 2 | 47 | | else if (v < 0) |
| | | 48 | | { |
| | | 49 | | #pragma warning disable CA1065 // Do not raise exceptions in unexpected locations |
| | 0 | 50 | | throw new InvalidOperationException("Call Dispose exactly once on a CoalescingQueueNode"); |
| | | 51 | | #pragma warning restore CA1065 // Do not raise exceptions in unexpected locations |
| | | 52 | | } |
| | 2 | 53 | | } |
| | | 54 | | } |
| | | 55 | | |
| | | 56 | | |
| | | 57 | | public struct HeapSafeCoalescingQueueNode<TContainer, TValue> where TContainer : struct |
| | | 58 | | { |
| | | 59 | | private readonly StructQueue<QueueEntry<TContainer>>.Handle _handle; |
| | | 60 | | private readonly RawValueProvider<TContainer, TValue> _valueProvider; |
| | | 61 | | |
| | | 62 | | internal HeapSafeCoalescingQueueNode(StructQueue<QueueEntry<TContainer>>.Handle handle, RawValueProvider<TContai |
| | | 63 | | { |
| | | 64 | | _handle = handle; |
| | | 65 | | _valueProvider = valueProvider; |
| | | 66 | | } |
| | | 67 | | |
| | | 68 | | public CoalescingQueueNode<TContainer, TValue> Open() |
| | | 69 | | => new(_handle, _valueProvider); |
| | | 70 | | } |
| | | 71 | | |
| | | 72 | | } |