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 12:23:37 -08:00
|
|
|
public ManualResetEvent Event { get; }
|
2018-11-28 14:18:09 -08:00
|
|
|
|
|
|
|
public bool IsExiting { get; set; }
|
|
|
|
|
|
|
|
public PausableThread()
|
|
|
|
{
|
|
|
|
Event = new ManualResetEvent(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-04 12:23:37 -08:00
|
|
|
private ConcurrentDictionary<Thread, PausableThread> _threads;
|
2018-09-18 16:36:43 -07:00
|
|
|
|
|
|
|
public HleCoreManager()
|
|
|
|
{
|
2018-12-04 12:23:37 -08:00
|
|
|
_threads = new ConcurrentDictionary<Thread, PausableThread>();
|
2018-11-28 14:18:09 -08:00
|
|
|
}
|
|
|
|
|
2018-12-04 12:23:37 -08:00
|
|
|
public void Set(Thread thread)
|
2018-11-28 14:18:09 -08:00
|
|
|
{
|
2018-12-04 12:23:37 -08:00
|
|
|
GetThread(thread).Event.Set();
|
2018-11-28 14:18:09 -08:00
|
|
|
}
|
|
|
|
|
2018-12-04 12:23:37 -08:00
|
|
|
public void Reset(Thread thread)
|
2018-11-28 14:18:09 -08:00
|
|
|
{
|
2018-12-04 12:23:37 -08:00
|
|
|
GetThread(thread).Event.Reset();
|
2018-11-28 14:18:09 -08:00
|
|
|
}
|
|
|
|
|
2018-12-04 12:23:37 -08:00
|
|
|
public void Wait(Thread thread)
|
2018-11-28 14:18:09 -08:00
|
|
|
{
|
2018-12-04 12:23:37 -08:00
|
|
|
PausableThread pausableThread = GetThread(thread);
|
2018-11-28 14:18:09 -08:00
|
|
|
|
2018-12-04 12:23:37 -08:00
|
|
|
if (!pausableThread.IsExiting)
|
2018-11-28 14:18:09 -08:00
|
|
|
{
|
2018-12-04 12:23:37 -08:00
|
|
|
pausableThread.Event.WaitOne();
|
2018-11-28 14:18:09 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-04 12:23:37 -08:00
|
|
|
public void Exit(Thread thread)
|
2018-11-28 14:18:09 -08:00
|
|
|
{
|
2018-12-04 12:23:37 -08:00
|
|
|
GetThread(thread).IsExiting = true;
|
2018-09-18 16:36:43 -07:00
|
|
|
}
|
|
|
|
|
2018-12-04 12:23:37 -08:00
|
|
|
private PausableThread GetThread(Thread thread)
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
2018-12-04 12:23:37 -08:00
|
|
|
return _threads.GetOrAdd(thread, (key) => new PausableThread());
|
2018-09-18 16:36:43 -07:00
|
|
|
}
|
|
|
|
|
2018-12-04 12:23:37 -08:00
|
|
|
public void RemoveThread(Thread thread)
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
2018-12-04 12:23:37 -08:00
|
|
|
if (_threads.TryRemove(thread, out PausableThread pausableThread))
|
2018-09-18 16:36:43 -07:00
|
|
|
{
|
2018-12-04 12:23:37 -08:00
|
|
|
pausableThread.Event.Set();
|
|
|
|
pausableThread.Event.Dispose();
|
2018-09-18 16:36:43 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|