|
| 1 | +using UnityEngine; |
| 2 | +using System; |
| 3 | + |
| 4 | +namespace Leap.Unity { |
| 5 | + |
| 6 | + public class ProduceConsumeBuffer<T> { |
| 7 | + private T[] _buffer; |
| 8 | + private uint _bufferMask; |
| 9 | + private uint _head, _tail; |
| 10 | + |
| 11 | + /// <summary> |
| 12 | + /// Constructs a new produce consumer buffer of at least a certain capacity. Once the |
| 13 | + /// buffer is created, the capacity cannot be modified. |
| 14 | + /// |
| 15 | + /// If the minimum capacity is a power of two, it will be used as the actual capacity. |
| 16 | + /// If the minimum capacity is not a power of two, the next highest power of two will |
| 17 | + /// be used as the capacity. This behavior is an optimization, Internally this class |
| 18 | + /// uses a bitwise AND operation instead of a slower modulus operation for indexing, |
| 19 | + /// which only is possible if the array length is a power of two. |
| 20 | + /// </summary> |
| 21 | + public ProduceConsumeBuffer(int minCapacity) { |
| 22 | + if (minCapacity <= 0) { |
| 23 | + throw new ArgumentOutOfRangeException("The capacity of the ProduceConsumeBuffer must be positive and non-zero."); |
| 24 | + } |
| 25 | + |
| 26 | + int capacity; |
| 27 | + int closestPowerOfTwo = Mathf.ClosestPowerOfTwo(minCapacity); |
| 28 | + if (closestPowerOfTwo == minCapacity) { |
| 29 | + capacity = minCapacity; |
| 30 | + } else { |
| 31 | + if (closestPowerOfTwo < minCapacity) { |
| 32 | + capacity = closestPowerOfTwo * 2; |
| 33 | + } else { |
| 34 | + capacity = closestPowerOfTwo; |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + _buffer = new T[capacity]; |
| 39 | + _bufferMask = (uint)(capacity - 1); |
| 40 | + _head = 0; |
| 41 | + _tail = 0; |
| 42 | + } |
| 43 | + |
| 44 | + /// <summary> |
| 45 | + /// Returns the maximum number of elements that the buffer can hold. |
| 46 | + /// </summary> |
| 47 | + public int Capacity { |
| 48 | + get { |
| 49 | + return _buffer.Length; |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + /// <summary> |
| 54 | + /// Tries to enqueue a value into the buffer. If the buffer is already full, this |
| 55 | + /// method will perform no action and return false. This method is only safe to |
| 56 | + /// be called from a single producer thread. |
| 57 | + /// </summary> |
| 58 | + public bool TryEnqueue(ref T t) { |
| 59 | + uint nextTail = (_tail + 1) & _bufferMask; |
| 60 | + if (nextTail == _head) return false; |
| 61 | + |
| 62 | + _buffer[_tail] = t; |
| 63 | + _tail = nextTail; |
| 64 | + return true; |
| 65 | + } |
| 66 | + |
| 67 | + /// <summary> |
| 68 | + /// Tries to dequeue a value off of the buffer. If the buffer is empty this method |
| 69 | + /// will perform no action and return false. This method is only safe to be |
| 70 | + /// called from a single consumer thread. |
| 71 | + /// </summary> |
| 72 | + public bool TryDequeue(out T t) { |
| 73 | + if (_tail == _head) { |
| 74 | + t = default(T); |
| 75 | + return false; |
| 76 | + } |
| 77 | + |
| 78 | + t = _buffer[_head]; |
| 79 | + _head = (_head + 1) & _bufferMask; |
| 80 | + return true; |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments