2018-09-18 16:36:43 -07:00
|
|
|
using System.Collections.Concurrent;
|
|
|
|
using System.Threading;
|
|
|
|
|
|
|
|
namespace Ryujinx.HLE.HOS.Kernel
|
|
|
|
{
|
|
|
|
class HleCoreManager
|
|
|
|
{
|
2018-11-28 14:18:09 -08:00
|
|
|
private class PausableThread
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
public ManualResetEvent Event { get; private set; }
|
2018-11-28 14:18:09 -08:00
|
|
|
|
|
|
|
public bool IsExiting { get; set; }
|
|
|
|
|
|
|
|
public PausableThread()
|
|
|
|
{
|
|
|
|
Event = new ManualResetEvent(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
private ConcurrentDictionary<Thread, PausableThread> Threads;
|
2018-09-18 16:36:43 -07:00
|
|
|
|
|
|
|
public HleCoreManager()
|
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
Threads = new ConcurrentDictionary<Thread, PausableThread>();
|
2018-11-28 14:18:09 -08:00
|
|
|
}
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
public void Set(Thread Thread)
|
2018-11-28 14:18:09 -08:00
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
GetThread(Thread).Event.Set();
|
2018-11-28 14:18:09 -08:00
|
|
|
}
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
public void Reset(Thread Thread)
|
2018-11-28 14:18:09 -08:00
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
GetThread(Thread).Event.Reset();
|
2018-11-28 14:18:09 -08:00
|
|
|
}
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
public void Wait(Thread Thread)
|
2018-11-28 14:18:09 -08:00
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
PausableThread PausableThread = GetThread(Thread);
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
if (!PausableThread.IsExiting)
|
2018-11-28 14:18:09 -08:00
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
PausableThread.Event.WaitOne();
|
2018-11-28 14:18:09 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
public void Exit(Thread Thread)
|
2018-11-28 14:18:09 -08:00
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
GetThread(Thread).IsExiting = true;
|
2018-09-18 16:36:43 -07:00
|
|
|
}
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
private PausableThread GetThread(Thread Thread)
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
return Threads.GetOrAdd(Thread, (Key) => new PausableThread());
|
2018-09-18 16:36:43 -07:00
|
|
|
}
|
|
|
|
|
2018-12-04 16:52:39 -08:00
|
|
|
public void RemoveThread(Thread Thread)
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
if (Threads.TryRemove(Thread, out PausableThread PausableThread))
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
2018-12-04 16:52:39 -08:00
|
|
|
PausableThread.Event.Set();
|
|
|
|
PausableThread.Event.Dispose();
|
2018-09-18 16:36:43 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|