2018-12-18 06:33:36 +01:00
|
|
|
using Ryujinx.HLE.HOS.Kernel.Common;
|
2019-01-18 23:26:39 +01:00
|
|
|
using Ryujinx.HLE.HOS.Kernel.Process;
|
2020-07-17 06:19:07 +02:00
|
|
|
using System.Threading;
|
2018-12-18 06:33:36 +01:00
|
|
|
|
|
|
|
namespace Ryujinx.HLE.HOS.Kernel.Ipc
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
class KClientPort : KSynchronizationObject
|
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
private int _sessionsCount;
|
2020-05-04 05:41:29 +02:00
|
|
|
private readonly int _maxSessions;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2020-05-04 05:41:29 +02:00
|
|
|
private readonly KPort _parent;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2019-01-18 23:26:39 +01:00
|
|
|
public bool IsLight => _parent.IsLight;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2020-05-04 05:41:29 +02:00
|
|
|
public KClientPort(KernelContext context, KPort parent, int maxSessions) : base(context)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
_maxSessions = maxSessions;
|
|
|
|
_parent = parent;
|
2019-01-18 23:26:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public KernelResult Connect(out KClientSession clientSession)
|
|
|
|
{
|
|
|
|
clientSession = null;
|
|
|
|
|
2020-12-09 23:20:05 +01:00
|
|
|
KProcess currentProcess = KernelStatic.GetCurrentProcess();
|
2019-01-18 23:26:39 +01:00
|
|
|
|
|
|
|
if (currentProcess.ResourceLimit != null &&
|
|
|
|
!currentProcess.ResourceLimit.Reserve(LimitableResource.Session, 1))
|
|
|
|
{
|
|
|
|
return KernelResult.ResLimitExceeded;
|
|
|
|
}
|
|
|
|
|
2020-07-17 06:19:07 +02:00
|
|
|
if (!IncrementSessionsCount())
|
2019-01-18 23:26:39 +01:00
|
|
|
{
|
2020-07-17 06:19:07 +02:00
|
|
|
currentProcess.ResourceLimit?.Release(LimitableResource.Session, 1);
|
2019-01-18 23:26:39 +01:00
|
|
|
|
2020-07-17 06:19:07 +02:00
|
|
|
return KernelResult.SessionCountExceeded;
|
2019-01-18 23:26:39 +01:00
|
|
|
}
|
|
|
|
|
2020-07-17 06:19:07 +02:00
|
|
|
KSession session = new KSession(KernelContext, this);
|
2019-01-18 23:26:39 +01:00
|
|
|
|
|
|
|
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-12-09 23:20:05 +01:00
|
|
|
KProcess currentProcess = KernelStatic.GetCurrentProcess();
|
2019-01-18 23:26:39 +01:00
|
|
|
|
|
|
|
if (currentProcess.ResourceLimit != null &&
|
|
|
|
!currentProcess.ResourceLimit.Reserve(LimitableResource.Session, 1))
|
|
|
|
{
|
|
|
|
return KernelResult.ResLimitExceeded;
|
|
|
|
}
|
|
|
|
|
2020-07-17 06:19:07 +02:00
|
|
|
if (!IncrementSessionsCount())
|
2019-01-18 23:26:39 +01:00
|
|
|
{
|
2020-07-17 06:19:07 +02:00
|
|
|
currentProcess.ResourceLimit?.Release(LimitableResource.Session, 1);
|
2019-01-18 23:26:39 +01:00
|
|
|
|
2020-07-17 06:19:07 +02:00
|
|
|
return KernelResult.SessionCountExceeded;
|
2019-01-18 23:26:39 +01:00
|
|
|
}
|
|
|
|
|
2020-05-04 05:41:29 +02:00
|
|
|
KLightSession session = new KLightSession(KernelContext);
|
2019-01-18 23:26:39 +01: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 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2020-07-17 06:19:07 +02: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-04 05:41:29 +02:00
|
|
|
public new static KernelResult RemoveName(KernelContext context, string name)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2020-05-04 05:41:29 +02:00
|
|
|
KAutoObject foundObj = FindNamedObject(context, name);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (!(foundObj is KClientPort))
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
return KernelResult.NotFound;
|
|
|
|
}
|
|
|
|
|
2020-05-04 05:41:29 +02:00
|
|
|
return KAutoObject.RemoveName(context, name);
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|