EventFileDescriptor.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. using Ryujinx.HLE.HOS.Services.Sockets.Bsd.Types;
  2. using System;
  3. using System.Runtime.InteropServices;
  4. using System.Threading;
  5. namespace Ryujinx.HLE.HOS.Services.Sockets.Bsd.Impl
  6. {
  7. class EventFileDescriptor : IFileDescriptor
  8. {
  9. private ulong _value;
  10. private readonly EventFdFlags _flags;
  11. private object _lock = new object();
  12. public bool Blocking { get => !_flags.HasFlag(EventFdFlags.NonBlocking); set => throw new NotSupportedException(); }
  13. public ManualResetEvent WriteEvent { get; }
  14. public ManualResetEvent ReadEvent { get; }
  15. public EventFileDescriptor(ulong value, EventFdFlags flags)
  16. {
  17. // FIXME: We should support blocking operations.
  18. // Right now they can't be supported because it would cause the
  19. // service to lock up as we only have one thread processing requests.
  20. flags |= EventFdFlags.NonBlocking;
  21. _value = value;
  22. _flags = flags;
  23. WriteEvent = new ManualResetEvent(false);
  24. ReadEvent = new ManualResetEvent(false);
  25. UpdateEventStates();
  26. }
  27. public int Refcount { get; set; }
  28. public void Dispose()
  29. {
  30. WriteEvent.Dispose();
  31. ReadEvent.Dispose();
  32. }
  33. private void ResetEventStates()
  34. {
  35. WriteEvent.Reset();
  36. ReadEvent.Reset();
  37. }
  38. private void UpdateEventStates()
  39. {
  40. if (_value > 0)
  41. {
  42. ReadEvent.Set();
  43. }
  44. if (_value != uint.MaxValue - 1)
  45. {
  46. WriteEvent.Set();
  47. }
  48. }
  49. public LinuxError Read(out int readSize, Span<byte> buffer)
  50. {
  51. if (buffer.Length < sizeof(ulong))
  52. {
  53. readSize = 0;
  54. return LinuxError.EINVAL;
  55. }
  56. lock (_lock)
  57. {
  58. ResetEventStates();
  59. ref ulong count = ref MemoryMarshal.Cast<byte, ulong>(buffer)[0];
  60. if (_value == 0)
  61. {
  62. if (Blocking)
  63. {
  64. while (_value == 0)
  65. {
  66. Monitor.Wait(_lock);
  67. }
  68. }
  69. else
  70. {
  71. readSize = 0;
  72. UpdateEventStates();
  73. return LinuxError.EAGAIN;
  74. }
  75. }
  76. readSize = sizeof(ulong);
  77. if (_flags.HasFlag(EventFdFlags.Semaphore))
  78. {
  79. --_value;
  80. count = 1;
  81. }
  82. else
  83. {
  84. count = _value;
  85. _value = 0;
  86. }
  87. UpdateEventStates();
  88. return LinuxError.SUCCESS;
  89. }
  90. }
  91. public LinuxError Write(out int writeSize, ReadOnlySpan<byte> buffer)
  92. {
  93. if (!MemoryMarshal.TryRead(buffer, out ulong count) || count == ulong.MaxValue)
  94. {
  95. writeSize = 0;
  96. return LinuxError.EINVAL;
  97. }
  98. lock (_lock)
  99. {
  100. ResetEventStates();
  101. if (_value > _value + count)
  102. {
  103. if (Blocking)
  104. {
  105. Monitor.Wait(_lock);
  106. }
  107. else
  108. {
  109. writeSize = 0;
  110. UpdateEventStates();
  111. return LinuxError.EAGAIN;
  112. }
  113. }
  114. writeSize = sizeof(ulong);
  115. _value += count;
  116. Monitor.Pulse(_lock);
  117. UpdateEventStates();
  118. return LinuxError.SUCCESS;
  119. }
  120. }
  121. }
  122. }