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