2018-02-04 23:08:20 +00:00
|
|
|
using ChocolArm64;
|
2018-04-21 19:07:16 +00:00
|
|
|
using System;
|
2018-02-04 23:08:20 +00:00
|
|
|
|
2018-02-20 20:09:23 +00:00
|
|
|
namespace Ryujinx.Core.OsHle.Handles
|
2018-02-04 23:08:20 +00:00
|
|
|
{
|
2018-03-19 18:58:46 +00:00
|
|
|
class KThread : KSynchronizationObject
|
2018-02-04 23:08:20 +00:00
|
|
|
{
|
|
|
|
public AThread Thread { get; private set; }
|
|
|
|
|
2018-05-13 03:22:42 +00:00
|
|
|
public int CoreMask { get; set; }
|
|
|
|
|
2018-04-24 18:57:39 +00:00
|
|
|
public long MutexAddress { get; set; }
|
|
|
|
public long CondVarAddress { get; set; }
|
2018-04-21 19:07:16 +00:00
|
|
|
|
2018-05-13 03:22:42 +00:00
|
|
|
private Process Process;
|
|
|
|
|
2018-04-21 19:07:16 +00:00
|
|
|
public KThread NextMutexThread { get; set; }
|
|
|
|
public KThread NextCondVarThread { get; set; }
|
|
|
|
|
2018-04-24 18:57:39 +00:00
|
|
|
public KThread MutexOwner { get; set; }
|
2018-04-21 19:07:16 +00:00
|
|
|
|
|
|
|
public int ActualPriority { get; private set; }
|
|
|
|
public int WantedPriority { get; private set; }
|
|
|
|
|
2018-05-13 03:22:42 +00:00
|
|
|
public int IdealCore { get; private set; }
|
|
|
|
public int ActualCore { get; set; }
|
2018-04-19 02:52:23 +00:00
|
|
|
|
2018-04-21 19:07:16 +00:00
|
|
|
public int WaitHandle { get; set; }
|
2018-02-14 02:43:08 +00:00
|
|
|
|
|
|
|
public int ThreadId => Thread.ThreadId;
|
|
|
|
|
2018-05-13 03:22:42 +00:00
|
|
|
public KThread(
|
|
|
|
AThread Thread,
|
|
|
|
Process Process,
|
|
|
|
int IdealCore,
|
|
|
|
int Priority)
|
2018-02-04 23:08:20 +00:00
|
|
|
{
|
2018-05-13 03:22:42 +00:00
|
|
|
this.Thread = Thread;
|
|
|
|
this.Process = Process;
|
|
|
|
this.IdealCore = IdealCore;
|
|
|
|
|
|
|
|
CoreMask = 1 << IdealCore;
|
2018-04-21 19:07:16 +00:00
|
|
|
|
|
|
|
ActualPriority = WantedPriority = Priority;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SetPriority(int Priority)
|
|
|
|
{
|
|
|
|
WantedPriority = Priority;
|
|
|
|
|
|
|
|
UpdatePriority();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void UpdatePriority()
|
|
|
|
{
|
|
|
|
int OldPriority = ActualPriority;
|
|
|
|
|
|
|
|
int CurrPriority = WantedPriority;
|
|
|
|
|
|
|
|
if (NextMutexThread != null && CurrPriority > NextMutexThread.WantedPriority)
|
|
|
|
{
|
|
|
|
CurrPriority = NextMutexThread.WantedPriority;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (CurrPriority != OldPriority)
|
|
|
|
{
|
|
|
|
ActualPriority = CurrPriority;
|
|
|
|
|
2018-05-13 03:22:42 +00:00
|
|
|
UpdateWaitLists();
|
2018-04-21 19:07:16 +00:00
|
|
|
|
|
|
|
MutexOwner?.UpdatePriority();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-13 03:22:42 +00:00
|
|
|
private void UpdateWaitLists()
|
|
|
|
{
|
|
|
|
UpdateMutexList();
|
|
|
|
UpdateCondVarList();
|
|
|
|
|
|
|
|
Process.Scheduler.Resort(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void UpdateMutexList()
|
2018-04-21 19:07:16 +00:00
|
|
|
{
|
|
|
|
KThread OwnerThread = MutexOwner;
|
|
|
|
|
2018-05-13 03:22:42 +00:00
|
|
|
if (OwnerThread == null)
|
2018-04-21 19:07:16 +00:00
|
|
|
{
|
2018-05-13 03:22:42 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//The MutexOwner field should only be non-null when the thread is
|
|
|
|
//waiting for the lock, and the lock belongs to another thread.
|
|
|
|
if (OwnerThread == this)
|
|
|
|
{
|
|
|
|
throw new InvalidOperationException();
|
|
|
|
}
|
|
|
|
|
|
|
|
lock (OwnerThread)
|
|
|
|
{
|
|
|
|
//Remove itself from the list.
|
|
|
|
KThread CurrThread = OwnerThread;
|
|
|
|
|
|
|
|
while (CurrThread.NextMutexThread != null)
|
2018-04-21 19:07:16 +00:00
|
|
|
{
|
2018-05-13 03:22:42 +00:00
|
|
|
if (CurrThread.NextMutexThread == this)
|
|
|
|
{
|
|
|
|
CurrThread.NextMutexThread = NextMutexThread;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
CurrThread = CurrThread.NextMutexThread;
|
2018-04-21 19:07:16 +00:00
|
|
|
}
|
|
|
|
|
2018-05-13 03:22:42 +00:00
|
|
|
//Re-add taking new priority into account.
|
|
|
|
CurrThread = OwnerThread;
|
2018-04-21 19:07:16 +00:00
|
|
|
|
2018-05-13 03:22:42 +00:00
|
|
|
while (CurrThread.NextMutexThread != null)
|
|
|
|
{
|
|
|
|
if (CurrThread.NextMutexThread.ActualPriority > ActualPriority)
|
2018-04-21 19:07:16 +00:00
|
|
|
{
|
2018-05-13 03:22:42 +00:00
|
|
|
break;
|
|
|
|
}
|
2018-04-21 19:07:16 +00:00
|
|
|
|
2018-05-13 03:22:42 +00:00
|
|
|
CurrThread = CurrThread.NextMutexThread;
|
|
|
|
}
|
2018-04-21 19:07:16 +00:00
|
|
|
|
2018-05-13 03:22:42 +00:00
|
|
|
NextMutexThread = CurrThread.NextMutexThread;
|
2018-04-21 19:07:16 +00:00
|
|
|
|
2018-05-13 03:22:42 +00:00
|
|
|
CurrThread.NextMutexThread = this;
|
|
|
|
}
|
|
|
|
}
|
2018-04-21 19:07:16 +00:00
|
|
|
|
2018-05-13 03:22:42 +00:00
|
|
|
private void UpdateCondVarList()
|
|
|
|
{
|
|
|
|
lock (Process.ThreadArbiterListLock)
|
|
|
|
{
|
|
|
|
if (Process.ThreadArbiterListHead == null)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Remove itself from the list.
|
|
|
|
bool Found;
|
|
|
|
|
|
|
|
KThread CurrThread = Process.ThreadArbiterListHead;
|
|
|
|
|
|
|
|
if (Found = (Process.ThreadArbiterListHead == this))
|
|
|
|
{
|
|
|
|
Process.ThreadArbiterListHead = Process.ThreadArbiterListHead.NextCondVarThread;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
while (CurrThread.NextCondVarThread != null)
|
2018-04-21 19:07:16 +00:00
|
|
|
{
|
2018-05-13 03:22:42 +00:00
|
|
|
if (CurrThread.NextCondVarThread == this)
|
2018-04-21 19:07:16 +00:00
|
|
|
{
|
2018-05-13 03:22:42 +00:00
|
|
|
CurrThread.NextCondVarThread = NextCondVarThread;
|
|
|
|
|
|
|
|
Found = true;
|
|
|
|
|
2018-04-21 19:07:16 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-05-13 03:22:42 +00:00
|
|
|
CurrThread = CurrThread.NextCondVarThread;
|
2018-04-21 19:07:16 +00:00
|
|
|
}
|
2018-05-13 03:22:42 +00:00
|
|
|
}
|
2018-04-21 19:07:16 +00:00
|
|
|
|
2018-05-13 03:22:42 +00:00
|
|
|
if (!Found)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Re-add taking new priority into account.
|
|
|
|
if (Process.ThreadArbiterListHead == null ||
|
|
|
|
Process.ThreadArbiterListHead.ActualPriority > ActualPriority)
|
|
|
|
{
|
|
|
|
NextCondVarThread = Process.ThreadArbiterListHead;
|
|
|
|
|
|
|
|
Process.ThreadArbiterListHead = this;
|
2018-04-21 19:07:16 +00:00
|
|
|
|
2018-05-13 03:22:42 +00:00
|
|
|
return;
|
2018-04-21 19:07:16 +00:00
|
|
|
}
|
2018-05-13 03:22:42 +00:00
|
|
|
|
|
|
|
CurrThread = Process.ThreadArbiterListHead;
|
|
|
|
|
|
|
|
while (CurrThread.NextCondVarThread != null)
|
|
|
|
{
|
|
|
|
if (CurrThread.NextCondVarThread.ActualPriority > ActualPriority)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
CurrThread = CurrThread.NextCondVarThread;
|
|
|
|
}
|
|
|
|
|
|
|
|
NextCondVarThread = CurrThread.NextCondVarThread;
|
|
|
|
|
|
|
|
CurrThread.NextCondVarThread = this;
|
2018-04-21 19:07:16 +00:00
|
|
|
}
|
2018-02-04 23:08:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|