Added no yield timeout back

This commit is contained in:
Alessandro Proto 2023-01-27 21:58:19 +01:00
parent 511515b9fa
commit 27bfd6d363

View file

@ -13,11 +13,13 @@ namespace Capy64.Runtime;
public class LuaState : IDisposable public class LuaState : IDisposable
{ {
public Lua Thread; public Lua Thread;
private Lua _parent; private readonly Lua _parent;
private ConcurrentQueue<LuaEvent> _queue = new(); private readonly ConcurrentQueue<LuaEvent> _queue = new();
private string[] _eventFilters = Array.Empty<string>(); private string[] _eventFilters = Array.Empty<string>();
private static readonly System.Timers.Timer yieldTimeoutTimer = new(TimeSpan.FromSeconds(3));
private static bool yieldTimedOut = false;
public LuaState() public LuaState()
{ {
@ -34,11 +36,28 @@ public class LuaState : IDisposable
Thread = _parent.NewThread(); Thread = _parent.NewThread();
Thread.SetHook(LH_YieldTimeout, LuaHookMask.Count, 7000);
yieldTimeoutTimer.Elapsed += (sender, ev) =>
{
yieldTimedOut = true;
};
InitPlugins(); InitPlugins();
Thread.SetTop(0); Thread.SetTop(0);
} }
private static void LH_YieldTimeout(IntPtr state, IntPtr ar)
{
var L = Lua.FromIntPtr(state);
if (yieldTimedOut)
{
L.Error("no yield timeout");
Console.WriteLine("tick");
}
}
private void InitPlugins() private void InitPlugins()
{ {
var allPlugins = new List<IPlugin>(Capy64.Instance.NativePlugins); var allPlugins = new List<IPlugin>(Capy64.Instance.NativePlugins);
@ -81,7 +100,7 @@ public class LuaState : IDisposable
{ {
npars = 0; npars = 0;
if (_queue.Count == 0) if (_queue.IsEmpty)
return false; return false;
if (!_queue.TryDequeue(out var ev)) if (!_queue.TryDequeue(out var ev))
@ -109,8 +128,10 @@ public class LuaState : IDisposable
Thread.Pop(npars); Thread.Pop(npars);
return true; return true;
} }
yieldTimeoutTimer.Start();
var status = Thread.Resume(null, npars, out var nresults); var status = Thread.Resume(null, npars, out var nresults);
yieldTimedOut = false;
yieldTimeoutTimer.Stop();
// the Lua script is finished, there's nothing else to resume // the Lua script is finished, there's nothing else to resume
if (status == LuaStatus.OK) if (status == LuaStatus.OK)
@ -155,6 +176,7 @@ public class LuaState : IDisposable
public void Dispose() public void Dispose()
{ {
GC.SuppressFinalize(this);
_queue.Clear(); _queue.Clear();
Thread.Close(); Thread.Close();
_parent.Close(); _parent.Close();