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