ryujinx/Ryujinx.HLE/HOS/Services/Sockets/Bsd/Impl/EventFileDescriptorPollManager.cs
Mary-nyan 1865ea87e5
bsd: Fix eventfd broken logic (#3647)
* bsd: Fix eventfd broken logic

This commit fix eventfd logic being broken.

The following changes were made:
- EventFd IPC definition had argument inverted
- EventFd events weren't fired correctly
- Poll logic was wrong and unfinished for eventfd
- Reintroduce workaround from #3385 but in a safer way, and spawn 4
  threads.

* ipc: Rework a bit for multithreads

* Clean up debug logs

* Make server thread yield when managed lock isn't availaible

* Fix replyTargetHandle not being added in the proper locking scope

* Simplify some scopes

* Address gdkchan's comments

* Revert IPC workaround for now

* Reintroduce the EventFileDescriptor workaround
2022-11-27 20:18:05 +00:00

114 lines
3.4 KiB
C#

using Ryujinx.Common.Logging;
using System.Collections.Generic;
using System.Threading;
namespace Ryujinx.HLE.HOS.Services.Sockets.Bsd
{
class EventFileDescriptorPollManager : IPollManager
{
private static EventFileDescriptorPollManager _instance;
public static EventFileDescriptorPollManager Instance
{
get
{
if (_instance == null)
{
_instance = new EventFileDescriptorPollManager();
}
return _instance;
}
}
public bool IsCompatible(PollEvent evnt)
{
return evnt.FileDescriptor is EventFileDescriptor;
}
public LinuxError Poll(List<PollEvent> events, int timeoutMilliseconds, out int updatedCount)
{
updatedCount = 0;
List<ManualResetEvent> waiters = new List<ManualResetEvent>();
for (int i = 0; i < events.Count; i++)
{
PollEvent evnt = events[i];
EventFileDescriptor socket = (EventFileDescriptor)evnt.FileDescriptor;
bool isValidEvent = false;
if (evnt.Data.InputEvents.HasFlag(PollEventTypeMask.Input) ||
evnt.Data.InputEvents.HasFlag(PollEventTypeMask.UrgentInput))
{
waiters.Add(socket.ReadEvent);
isValidEvent = true;
}
if (evnt.Data.InputEvents.HasFlag(PollEventTypeMask.Output))
{
waiters.Add(socket.WriteEvent);
isValidEvent = true;
}
if (!isValidEvent)
{
Logger.Warning?.Print(LogClass.ServiceBsd, $"Unsupported Poll input event type: {evnt.Data.InputEvents}");
return LinuxError.EINVAL;
}
}
int index = WaitHandle.WaitAny(waiters.ToArray(), timeoutMilliseconds);
if (index != WaitHandle.WaitTimeout)
{
for (int i = 0; i < events.Count; i++)
{
PollEventTypeMask outputEvents = 0;
PollEvent evnt = events[i];
EventFileDescriptor socket = (EventFileDescriptor)evnt.FileDescriptor;
if (socket.ReadEvent.WaitOne(0))
{
if (evnt.Data.InputEvents.HasFlag(PollEventTypeMask.Input))
{
outputEvents |= PollEventTypeMask.Input;
}
if (evnt.Data.InputEvents.HasFlag(PollEventTypeMask.UrgentInput))
{
outputEvents |= PollEventTypeMask.UrgentInput;
}
}
if ((evnt.Data.InputEvents.HasFlag(PollEventTypeMask.Output))
&& socket.WriteEvent.WaitOne(0))
{
outputEvents |= PollEventTypeMask.Output;
}
if (outputEvents != 0)
{
evnt.Data.OutputEvents = outputEvents;
updatedCount++;
}
}
}
else
{
return LinuxError.ETIMEDOUT;
}
return LinuxError.SUCCESS;
}
}
}