Skip to content

Commit

Permalink
fix timer gc
Browse files Browse the repository at this point in the history
  • Loading branch information
pangweiwei committed Jan 14, 2018
1 parent 2c67255 commit 8e5814e
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 23 deletions.
43 changes: 22 additions & 21 deletions Assets/Plugins/Slua_Managed/Timer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ namespace SLua

public class LuaTimer : LuaObject
{
const int TimerMaxInit = 16;
class Timer
{
internal int sn;
Expand All @@ -35,13 +36,13 @@ class Timer
internal Func<int, bool> handler;
internal bool delete;
internal IntPtr L;
internal LinkedList<Timer> container;
internal List<Timer> container;
}
class Wheel
{
internal static int dial_scale = 256;
internal int head;
internal LinkedList<Timer>[] vecDial;
internal List<Timer>[] vecDial;
internal int dialSize;
internal int timeRange;
internal Wheel nextWheel;
Expand All @@ -50,20 +51,20 @@ internal Wheel(int dialSize)
this.dialSize = dialSize;
this.timeRange = dialSize * dial_scale;
this.head = 0;
this.vecDial = new LinkedList<Timer>[dial_scale];
this.vecDial = new List<Timer>[dial_scale];
for (int i = 0; i < dial_scale; ++i)
{
this.vecDial[i] = new LinkedList<Timer>();
this.vecDial[i] = new List<Timer>(TimerMaxInit);
}
}
internal LinkedList<Timer> nextDial()
internal List<Timer> nextDial()
{
return vecDial[head++];
}
internal void add(int delay, Timer tm)
{
var container = vecDial[(head + (delay - (dialSize - jiffies_msec)) / dialSize) % dial_scale];
container.AddLast(tm);
container.Add(tm);
tm.container = container;
}
}
Expand All @@ -74,8 +75,8 @@ internal void add(int delay, Timer tm)
static float pileSecs;
static float nowTime;
static Dictionary<int, Timer> mapSnTimer;
static LinkedList<Timer> executeTimers;
static List<Timer> executeTimers;

static int intpow(int n, int m)
{
int ret = 1;
Expand Down Expand Up @@ -126,7 +127,8 @@ internal static void tick(float deltaTime)
{
if (executeTimers == null)
return;


//UnityEngine.Profiling.Profiler.BeginSample ("timer tick");
nowTime += deltaTime;
pileSecs += deltaTime;
int cycle = 0;
Expand All @@ -138,12 +140,10 @@ internal static void tick(float deltaTime)
for (int i = 0; i < cycle; ++i)
{
var timers = wheels[0].nextDial();
LinkedListNode<Timer> node = timers.First;
for (int j = 0; j < timers.Count; ++j)
{
var tm = node.Value;
executeTimers.AddLast(tm);
node = node.Next;
var tm = timers[j];
executeTimers.Add(tm);
}
timers.Clear();

Expand All @@ -156,10 +156,9 @@ internal static void tick(float deltaTime)
if (wheel.nextWheel != null)
{
var tms = wheel.nextWheel.nextDial();
LinkedListNode<Timer> tmsNode = tms.First;
for (int k = 0; k < tms.Count; ++k)
{
var tm = tmsNode.Value;
var tm = tms[k];
if (tm.delete)
{
mapSnTimer.Remove(tm.sn);
Expand All @@ -168,7 +167,6 @@ internal static void tick(float deltaTime)
{
innerAdd(tm.deadline, tm);
}
tmsNode = tmsNode.Next;
}
tms.Clear();
}
Expand All @@ -179,11 +177,12 @@ internal static void tick(float deltaTime)
}
}
}

while (executeTimers.Count > 0)
// run timer callback
for (int n=0;n<executeTimers.Count;n++)
{
var tm = executeTimers.First.Value;
executeTimers.Remove(tm);
var tm = executeTimers[n];
// if callback return true or any value!=false,
// re-add timer to new wheel
if (!tm.delete && tm.handler(tm.sn) && tm.cycle > 0)
{
innerAdd(now() + tm.cycle, tm);
Expand All @@ -193,6 +192,8 @@ internal static void tick(float deltaTime)
mapSnTimer.Remove(tm.sn);
}
}
executeTimers.Clear();
//UnityEngine.Profiling.Profiler.EndSample ();
}
static bool inited = false;
static void init()
Expand All @@ -210,7 +211,7 @@ static void init()
}
}
mapSnTimer = new Dictionary<int, Timer>();
executeTimers = new LinkedList<Timer>();
executeTimers = new List<Timer>(TimerMaxInit);
}

static int fetchSn()
Expand Down
4 changes: 4 additions & 0 deletions Assets/Slua/Editor/LuaConsole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,11 @@ void ResizeScrollView()
GUILayout.Space(4);

Event e = Event.current;
#if UNITY_2017_3_OR_NEWER
if (e.type == EventType.MouseDown && dragSpliterRect.Contains(e.mousePosition))
#else
if (e.type == EventType.mouseDown && dragSpliterRect.Contains(e.mousePosition))
#endif
{
e.Use();
inputAreaResizing = true;
Expand Down
8 changes: 8 additions & 0 deletions Assets/Slua/Resources/main.txt
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,14 @@ function main()
cube.transform.localRotation = Quaternion.Euler(0,0,math.sin(Time.time)*90)
return true
end)

-- test timer
for i=1,20 do
LuaTimer.Add(0,i*1000,function()
print("timer callback",i*1000)
return true
end)
end

print(UnityEngine.PrimitiveType.Cube,type(UnityEngine.PrimitiveType.Cube))

Expand Down
6 changes: 4 additions & 2 deletions Assets/Slua/example/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ void Start()

void log(string cond, string trace, LogType lt)
{
logText.text += (cond + "\n");

string txt = logText.text + (cond + "\n");
if(txt.Length>1000)
txt = txt.Substring (txt.Length - 1000);
logText.text = txt;
}

void tick(int p)
Expand Down

0 comments on commit 8e5814e

Please sign in to comment.