2018-09-19 01:36:43 +02:00
|
|
|
using System.Collections.Concurrent;
|
|
|
|
using System.Threading;
|
|
|
|
|
2018-12-18 06:33:36 +01:00
|
|
|
namespace Ryujinx.HLE.HOS.Kernel.Threading
|
2018-09-19 01:36:43 +02:00
|
|
|
{
|
|
|
|
class HleCoreManager
|
|
|
|
{
|
2018-11-28 23:18:09 +01:00
|
|
|
private class PausableThread
|
|
|
|
{
|
2018-12-05 01:52:39 +01:00
|
|
|
public ManualResetEvent Event { get; private set; }
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
|
|
public bool IsExiting { get; set; }
|
|
|
|
|
|
|
|
public PausableThread()
|
|
|
|
{
|
|
|
|
Event = new ManualResetEvent(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
private ConcurrentDictionary<Thread, PausableThread> _threads;
|
2018-09-19 01:36:43 +02:00
|
|
|
|
|
|
|
public HleCoreManager()
|
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
_threads = new ConcurrentDictionary<Thread, PausableThread>();
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
public void Set(Thread thread)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
GetThread(thread).Event.Set();
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
public void Reset(Thread thread)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
GetThread(thread).Event.Reset();
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
public void Wait(Thread thread)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
PausableThread pausableThread = GetThread(thread);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (!pausableThread.IsExiting)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
pausableThread.Event.WaitOne();
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
public void Exit(Thread thread)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
GetThread(thread).IsExiting = true;
|
2018-09-19 01:36:43 +02:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
private PausableThread GetThread(Thread thread)
|
2018-09-19 01:36:43 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
return _threads.GetOrAdd(thread, (key) => new PausableThread());
|
2018-09-19 01:36:43 +02:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
public void RemoveThread(Thread thread)
|
2018-09-19 01:36:43 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
if (_threads.TryRemove(thread, out PausableThread pausableThread))
|
2018-09-19 01:36:43 +02:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
pausableThread.Event.Set();
|
|
|
|
pausableThread.Event.Dispose();
|
2018-09-19 01:36:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|