From 27bfd6d3631c6540ec237bd26e8424119b7ec0dc Mon Sep 17 00:00:00 2001 From: Alessandro Proto Date: Fri, 27 Jan 2023 21:58:19 +0100 Subject: [PATCH] Added no yield timeout back --- Capy64/Runtime/LuaState.cs | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/Capy64/Runtime/LuaState.cs b/Capy64/Runtime/LuaState.cs index 3160430..531792a 100644 --- a/Capy64/Runtime/LuaState.cs +++ b/Capy64/Runtime/LuaState.cs @@ -13,11 +13,13 @@ namespace Capy64.Runtime; public class LuaState : IDisposable { public Lua Thread; - private Lua _parent; + private readonly Lua _parent; - private ConcurrentQueue _queue = new(); + private readonly ConcurrentQueue _queue = new(); private string[] _eventFilters = Array.Empty(); + private static readonly System.Timers.Timer yieldTimeoutTimer = new(TimeSpan.FromSeconds(3)); + private static bool yieldTimedOut = false; public LuaState() { @@ -34,11 +36,28 @@ public class LuaState : IDisposable Thread = _parent.NewThread(); + Thread.SetHook(LH_YieldTimeout, LuaHookMask.Count, 7000); + yieldTimeoutTimer.Elapsed += (sender, ev) => + { + yieldTimedOut = true; + }; + InitPlugins(); 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() { var allPlugins = new List(Capy64.Instance.NativePlugins); @@ -81,7 +100,7 @@ public class LuaState : IDisposable { npars = 0; - if (_queue.Count == 0) + if (_queue.IsEmpty) return false; if (!_queue.TryDequeue(out var ev)) @@ -109,8 +128,10 @@ public class LuaState : IDisposable Thread.Pop(npars); return true; } - + yieldTimeoutTimer.Start(); var status = Thread.Resume(null, npars, out var nresults); + yieldTimedOut = false; + yieldTimeoutTimer.Stop(); // the Lua script is finished, there's nothing else to resume if (status == LuaStatus.OK) @@ -155,6 +176,7 @@ public class LuaState : IDisposable public void Dispose() { + GC.SuppressFinalize(this); _queue.Clear(); Thread.Close(); _parent.Close();