AppletFifo.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Concurrent;
  4. using System.Collections.Generic;
  5. namespace Ryujinx.HLE.HOS.Services.Am.AppletAE
  6. {
  7. internal class AppletFifo<T> : IAppletFifo<T>
  8. {
  9. private ConcurrentQueue<T> _dataQueue;
  10. public event EventHandler DataAvailable;
  11. public bool IsSynchronized
  12. {
  13. get { return ((ICollection)_dataQueue).IsSynchronized; }
  14. }
  15. public object SyncRoot
  16. {
  17. get { return ((ICollection)_dataQueue).SyncRoot; }
  18. }
  19. public int Count
  20. {
  21. get { return _dataQueue.Count; }
  22. }
  23. public AppletFifo()
  24. {
  25. _dataQueue = new ConcurrentQueue<T>();
  26. }
  27. public void Push(T item)
  28. {
  29. _dataQueue.Enqueue(item);
  30. DataAvailable?.Invoke(this, null);
  31. }
  32. public bool TryAdd(T item)
  33. {
  34. try
  35. {
  36. this.Push(item);
  37. return true;
  38. }
  39. catch
  40. {
  41. return false;
  42. }
  43. }
  44. public T Pop()
  45. {
  46. if (_dataQueue.TryDequeue(out T result))
  47. {
  48. return result;
  49. }
  50. throw new InvalidOperationException("FIFO empty.");
  51. }
  52. public bool TryPop(out T result)
  53. {
  54. return _dataQueue.TryDequeue(out result);
  55. }
  56. public bool TryTake(out T item)
  57. {
  58. return this.TryPop(out item);
  59. }
  60. public T Peek()
  61. {
  62. if (_dataQueue.TryPeek(out T result))
  63. {
  64. return result;
  65. }
  66. throw new InvalidOperationException("FIFO empty.");
  67. }
  68. public bool TryPeek(out T result)
  69. {
  70. return _dataQueue.TryPeek(out result);
  71. }
  72. public void Clear()
  73. {
  74. _dataQueue.Clear();
  75. }
  76. public T[] ToArray()
  77. {
  78. return _dataQueue.ToArray();
  79. }
  80. public void CopyTo(T[] array, int arrayIndex)
  81. {
  82. _dataQueue.CopyTo(array, arrayIndex);
  83. }
  84. public void CopyTo(Array array, int index)
  85. {
  86. this.CopyTo((T[])array, index);
  87. }
  88. public IEnumerator<T> GetEnumerator()
  89. {
  90. return _dataQueue.GetEnumerator();
  91. }
  92. IEnumerator IEnumerable.GetEnumerator()
  93. {
  94. return _dataQueue.GetEnumerator();
  95. }
  96. }
  97. }