diff --git a/OpenSim/Region/ScriptEngine/Shared/Api/Implementation/Plugins/ScriptTimer.cs b/OpenSim/Region/ScriptEngine/Shared/Api/Implementation/Plugins/ScriptTimer.cs index c09f3f13b3..420dffca93 100644 --- a/OpenSim/Region/ScriptEngine/Shared/Api/Implementation/Plugins/ScriptTimer.cs +++ b/OpenSim/Region/ScriptEngine/Shared/Api/Implementation/Plugins/ScriptTimer.cs @@ -73,6 +73,7 @@ namespace OpenSim.Region.ScriptEngine.Shared.Api.Plugins } private Dictionary Timers = new Dictionary(); + private List TimersCache = null; private object TimerListLock = new object(); public void SetTimerEvent(uint m_localID, UUID m_itemID, double sec) @@ -83,22 +84,27 @@ namespace OpenSim.Region.ScriptEngine.Shared.Api.Plugins return; } - // Add to timer - TimerInfo ts = new TimerInfo(); - ts.localID = m_localID; - ts.itemID = m_itemID; - ts.interval = Convert.ToInt64(sec * 10000000); // How many 100 nanoseconds (ticks) should we wait - // 2193386136332921 ticks - // 219338613 seconds - - //ts.next = DateTime.Now.ToUniversalTime().AddSeconds(ts.interval); - ts.next = DateTime.Now.Ticks + ts.interval; - string key = MakeTimerKey(m_localID, m_itemID); lock (TimerListLock) { - // Adds if timer doesn't exist, otherwise replaces with new timer - Timers[key] = ts; + Timers.TryGetValue(key, out TimerInfo ts); + if(ts == null) + { + ts = new TimerInfo() + { + localID = m_localID, + itemID = m_itemID, + interval = (long)(sec * 10000000), + next = DateTime.Now.Ticks + ts.interval + }; + Timers[key] = ts; + TimersCache = null; + } + else + { + ts.interval = (long)(sec * 10000000); + ts.next = DateTime.Now.Ticks + ts.interval; + } } } @@ -112,27 +118,28 @@ namespace OpenSim.Region.ScriptEngine.Shared.Api.Plugins { m_CmdManager.m_ScriptEngine.CancelScriptEvent(ts.itemID, "timer"); Timers.Remove(key); + TimersCache = null; } } } public void CheckTimerEvents() { - // Nothing to do here? - if (Timers.Count == 0) - return; - List tvals; lock (TimerListLock) { - // Go through all timers - tvals = new List(Timers.Values); + if (Timers.Count == 0) + return; + if (TimersCache == null) + TimersCache = new List(Timers.Values); + tvals = TimersCache; } + long now = DateTime.Now.Ticks; foreach (TimerInfo ts in tvals) { // Time has passed? - if (ts.next < DateTime.Now.Ticks) + if (ts.next <= now) { //m_log.Debug("Time has passed: Now: " + DateTime.Now.Ticks + ", Passed: " + ts.next); // Add it to queue @@ -140,9 +147,7 @@ namespace OpenSim.Region.ScriptEngine.Shared.Api.Plugins new EventParams("timer", new Object[0], new DetectParams[0])); // set next interval - - //ts.next = DateTime.Now.ToUniversalTime().AddSeconds(ts.interval); - ts.next = DateTime.Now.Ticks + ts.interval; + ts.next = now + ts.interval; } } } @@ -151,23 +156,29 @@ namespace OpenSim.Region.ScriptEngine.Shared.Api.Plugins { List data = new List(); + List tvals; lock (TimerListLock) { - Dictionary.ValueCollection tvals = Timers.Values; - foreach (TimerInfo ts in tvals) + if (Timers.Count == 0) + return new object[0]; + if (TimersCache == null) + TimersCache = new List(Timers.Values); + tvals = TimersCache; + } + + foreach (TimerInfo ts in tvals) + { + if (ts.itemID == itemID) { - if (ts.itemID == itemID) - { - data.Add(ts.interval); - data.Add(ts.next-DateTime.Now.Ticks); - } + data.Add(ts.interval); + data.Add(ts.next-DateTime.Now.Ticks); } } + return data.ToArray(); } - public void CreateFromData(uint localID, UUID itemID, UUID objectID, - Object[] data) + public void CreateFromData(uint localID, UUID itemID, UUID objectID, Object[] data) { int idx = 0; @@ -180,10 +191,12 @@ namespace OpenSim.Region.ScriptEngine.Shared.Api.Plugins ts.interval = (long)data[idx]; ts.next = DateTime.Now.Ticks + (long)data[idx+1]; idx += 2; + string tskey = MakeTimerKey(localID, itemID); lock (TimerListLock) { - Timers.Add(MakeTimerKey(localID, itemID), ts); + Timers.Add(tskey, ts); + TimersCache = null; } } } @@ -191,13 +204,19 @@ namespace OpenSim.Region.ScriptEngine.Shared.Api.Plugins public List GetTimersInfo() { List retList = new List(); - + List tvals; lock (TimerListLock) { - foreach (TimerInfo i in Timers.Values) - retList.Add(i.Clone()); + if (Timers.Count == 0) + return retList; + if (TimersCache == null) + TimersCache = new List(Timers.Values); + tvals = TimersCache; } + foreach (TimerInfo i in tvals) + retList.Add(i.Clone()); + return retList; } }