EventFileDescriptor.cs 3.9 KB

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