2018-12-17 21:33:36 -08:00
|
|
|
using Ryujinx.HLE.HOS.Kernel.Common;
|
2019-01-18 14:26:39 -08:00
|
|
|
using Ryujinx.HLE.HOS.Kernel.Process;
|
|
|
|
using Ryujinx.HLE.HOS.Services;
|
2020-07-16 21:19:07 -07:00
|
|
|
using System.Threading;
|
2018-12-17 21:33:36 -08:00
|
|
|
|
|
|
|
namespace Ryujinx.HLE.HOS.Kernel.Ipc
|
2018-11-28 14:18:09 -08:00
|
|
|
{
|
|
|
|
class KClientPort : KSynchronizationObject
|
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
private int _sessionsCount;
|
2020-05-03 20:41:29 -07:00
|
|
|
private readonly int _maxSessions;
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2020-05-03 20:41:29 -07:00
|
|
|
private readonly KPort _parent;
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2019-01-18 14:26:39 -08:00
|
|
|
public bool IsLight => _parent.IsLight;
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2019-07-01 19:39:22 -07:00
|
|
|
// TODO: Remove that, we need it for now to allow HLE
|
|
|
|
// SM implementation to work with the new IPC system.
|
2019-01-18 14:26:39 -08:00
|
|
|
public IpcService Service { get; set; }
|
|
|
|
|
2020-05-03 20:41:29 -07:00
|
|
|
public KClientPort(KernelContext context, KPort parent, int maxSessions) : base(context)
|
2018-11-28 14:18:09 -08:00
|
|
|
{
|
2018-12-06 03:16:24 -08:00
|
|
|
_maxSessions = maxSessions;
|
|
|
|
_parent = parent;
|
2019-01-18 14:26:39 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
public KernelResult Connect(out KClientSession clientSession)
|
|
|
|
{
|
|
|
|
clientSession = null;
|
|
|
|
|
2020-05-03 20:41:29 -07:00
|
|
|
KProcess currentProcess = KernelContext.Scheduler.GetCurrentProcess();
|
2019-01-18 14:26:39 -08:00
|
|
|
|
|
|
|
if (currentProcess.ResourceLimit != null &&
|
|
|
|
!currentProcess.ResourceLimit.Reserve(LimitableResource.Session, 1))
|
|
|
|
{
|
|
|
|
return KernelResult.ResLimitExceeded;
|
|
|
|
}
|
|
|
|
|
2020-07-16 21:19:07 -07:00
|
|
|
if (!IncrementSessionsCount())
|
2019-01-18 14:26:39 -08:00
|
|
|
{
|
2020-07-16 21:19:07 -07:00
|
|
|
currentProcess.ResourceLimit?.Release(LimitableResource.Session, 1);
|
2019-01-18 14:26:39 -08:00
|
|
|
|
2020-07-16 21:19:07 -07:00
|
|
|
return KernelResult.SessionCountExceeded;
|
2019-01-18 14:26:39 -08:00
|
|
|
}
|
|
|
|
|
2020-07-16 21:19:07 -07:00
|
|
|
KSession session = new KSession(KernelContext, this);
|
2019-01-18 14:26:39 -08:00
|
|
|
|
|
|
|
if (Service != null)
|
|
|
|
{
|
|
|
|
session.ClientSession.Service = Service;
|
|
|
|
}
|
|
|
|
|
|
|
|
KernelResult result = _parent.EnqueueIncomingSession(session.ServerSession);
|
|
|
|
|
|
|
|
if (result != KernelResult.Success)
|
|
|
|
{
|
|
|
|
session.ClientSession.DecrementReferenceCount();
|
|
|
|
session.ServerSession.DecrementReferenceCount();
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
clientSession = session.ClientSession;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public KernelResult ConnectLight(out KLightClientSession clientSession)
|
|
|
|
{
|
|
|
|
clientSession = null;
|
|
|
|
|
2020-05-03 20:41:29 -07:00
|
|
|
KProcess currentProcess = KernelContext.Scheduler.GetCurrentProcess();
|
2019-01-18 14:26:39 -08:00
|
|
|
|
|
|
|
if (currentProcess.ResourceLimit != null &&
|
|
|
|
!currentProcess.ResourceLimit.Reserve(LimitableResource.Session, 1))
|
|
|
|
{
|
|
|
|
return KernelResult.ResLimitExceeded;
|
|
|
|
}
|
|
|
|
|
2020-07-16 21:19:07 -07:00
|
|
|
if (!IncrementSessionsCount())
|
2019-01-18 14:26:39 -08:00
|
|
|
{
|
2020-07-16 21:19:07 -07:00
|
|
|
currentProcess.ResourceLimit?.Release(LimitableResource.Session, 1);
|
2019-01-18 14:26:39 -08:00
|
|
|
|
2020-07-16 21:19:07 -07:00
|
|
|
return KernelResult.SessionCountExceeded;
|
2019-01-18 14:26:39 -08:00
|
|
|
}
|
|
|
|
|
2020-05-03 20:41:29 -07:00
|
|
|
KLightSession session = new KLightSession(KernelContext);
|
2019-01-18 14:26:39 -08:00
|
|
|
|
|
|
|
KernelResult result = _parent.EnqueueIncomingLightSession(session.ServerSession);
|
|
|
|
|
|
|
|
if (result != KernelResult.Success)
|
|
|
|
{
|
|
|
|
session.ClientSession.DecrementReferenceCount();
|
|
|
|
session.ServerSession.DecrementReferenceCount();
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
clientSession = session.ClientSession;
|
|
|
|
|
|
|
|
return result;
|
2018-11-28 14:18:09 -08:00
|
|
|
}
|
|
|
|
|
2020-07-16 21:19:07 -07:00
|
|
|
private bool IncrementSessionsCount()
|
|
|
|
{
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
int currentCount = _sessionsCount;
|
|
|
|
|
|
|
|
if (currentCount < _maxSessions)
|
|
|
|
{
|
|
|
|
if (Interlocked.CompareExchange(ref _sessionsCount, currentCount + 1, currentCount) == currentCount)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Disconnect()
|
|
|
|
{
|
|
|
|
KernelContext.CriticalSection.Enter();
|
|
|
|
|
|
|
|
SignalIfMaximumReached(Interlocked.Decrement(ref _sessionsCount));
|
|
|
|
|
|
|
|
KernelContext.CriticalSection.Leave();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void SignalIfMaximumReached(int value)
|
|
|
|
{
|
|
|
|
if (value == _maxSessions)
|
|
|
|
{
|
|
|
|
Signal();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-03 20:41:29 -07:00
|
|
|
public new static KernelResult RemoveName(KernelContext context, string name)
|
2018-11-28 14:18:09 -08:00
|
|
|
{
|
2020-05-03 20:41:29 -07:00
|
|
|
KAutoObject foundObj = FindNamedObject(context, name);
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2018-12-06 03:16:24 -08:00
|
|
|
if (!(foundObj is KClientPort))
|
2018-11-28 14:18:09 -08:00
|
|
|
{
|
|
|
|
return KernelResult.NotFound;
|
|
|
|
}
|
|
|
|
|
2020-05-03 20:41:29 -07:00
|
|
|
return KAutoObject.RemoveName(context, name);
|
2018-11-28 14:18:09 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|