feat(timers): Adds timer pooling, fixes timer related bugs, and changes timer api (#667)
### Changes/Fixes:
* Adds timer pooling.
* Allows pool to be configurable in ModernUO.json
* Pool replenishes itself asynchronously if depleted.
* Fixes an issue with barkeeps and town criers
* Fixes an issue with incognito buff icons not being removed
* Fixes an issue with polymorph name mod not being removed
* Fixes several places where timers go on forever even after an object is deleted, keeping a reference (memory leak)
* Eliminates the timer for MiningCart altogether.
* Deletes `AcidSlime` since it is a duplicate of `PoolOfAcid`
* Fixes HonorableExecution and standardizes the code for other Bushido moves.
## Changes to the Timer API:
```cs
public class Timer
{
// Creates a timer that will be returned to the pool once execution stops.
public static void StartTimer(Action callback);
public static void StartTimer(TimeSpan delay, Action callback);
public static void StartTimer(TimeSpan delay, TimeSpan interval, Action callback);
public static void StartTimer(TimeSpan interval, int count, Action callback);
public static void StartTimer(TimeSpan delay, TimeSpan interval, int count, Action callback);
// Creates a timer and returns a token for more control. Requires manual cancellation in order for the timer to be returned to the pool.
// If the token is dereferenced, the timer will be dereferenced too. While not returning a timer to the pool is not considered hazardous, it does defeat the purpose of pooled timers.
public static void StartTimer(Action callback, out TimerExecutionToken token);
public static void StartTimer(TimeSpan delay, Action callback, out TimerExecutionToken token);
public static void StartTimer(TimeSpan delay, TimeSpan interval, Action callback, out TimerExecutionToken token);
public static void StartTimer(TimeSpan interval, int count, Action callback, out TimerExecutionToken token);
public static void StartTimer(TimeSpan delay, TimeSpan interval, int count, Action callback, out TimerExecutionToken token);
// If you aren't sure how to use the API above, or you don't care about performance, then you can use the old RunUO Timer.DelayCall
public static DelayCallTimer DelayCall(Action callback);
public static DelayCallTimer DelayCall(TimeSpan delay, Action callback);
public static DelayCallTimer DelayCall(TimeSpan delay, TimeSpan interval, Action callback);
public static DelayCallTimer DelayCall(TimeSpan interval, int count, Action callback);
public static DelayCallTimer DelayCall(TimeSpan delay, TimeSpan interval, int count, Action callback);
}
public struct TimerExecutionToken
{
public bool Running { get; }
public int Index { get; }
public int RemainingCount { get; }
public DateTime Next { get; }
}
```
## When to use `TimerExecutionToken`?
Use tokens when you want to gain the performance benefit of using a pooled timer, but you need one of the following:
* Access to the next time the timer will tick:`token.Next`
* Access to which interval, how many intervals there are, or how many are remaining: `token.Index`, `token.Count`, and `token.RemainingCount`
* Stop a timer manually.
* Determine if the timer is running: `timer.Running`
* See notes below about requirements for using tokens!
## Notes about using the TimerExecutionToken:
When you opt-in to receive a token, you must call `Cancel()` to return the timer. This can be done inside of the callback, or outside of the callback at any time.
If this is not called and your timer is an infinite interval, then you will create a potential memory leak, or null pointer exception in your callback.
If the timer ends and is stopped, but cancel is not called, then the timer will never return to the pool and stay referenced until the token is deleted or cancel is called. (Memory leak)
## Is this thread safe?
No. The ModernUO timer system is not thread safe at all. If you require a thread safe timer system, contact me and I'll help adapt this system. Keep in mind that there is a massive performance hit to make this thread safe when there are literally no use cases for it.
If you need to synchronize execution, meaning you want to execute code from another thread on the core thread. Let's say you have a discord bot that is pushing commands to the game server. Then use `EventLoopContext.Post(SendOrPostCallback callback, object state);`.
This commit is contained in:
parent
b4de631f96
commit
9b554f69b0
219 changed files with 1897 additions and 2241 deletions
|
|
@ -71,7 +71,7 @@ namespace Server.Engines.ConPVP
|
|||
}
|
||||
}
|
||||
|
||||
Timer.DelayCall(Delete); // delete this after the world loads
|
||||
Timer.StartTimer(Delete); // delete this after the world loads
|
||||
}
|
||||
|
||||
public override void Serialize(IGenericWriter writer)
|
||||
|
|
@ -243,7 +243,7 @@ namespace Server.Engines.ConPVP
|
|||
m_Path.Clear();
|
||||
m_PathIdx = 0;
|
||||
|
||||
Timer.DelayCall(TimeSpan.FromSeconds(0.05), ContinueFlight);
|
||||
Timer.StartTimer(TimeSpan.FromSeconds(0.05), ContinueFlight);
|
||||
}
|
||||
|
||||
private bool CheckCatch(Mobile m, Point3D myLoc)
|
||||
|
|
@ -640,7 +640,7 @@ namespace Server.Engines.ConPVP
|
|||
DoAnim(GetWorldLocation(), m_Path[m_PathIdx - 1], Map);
|
||||
}
|
||||
|
||||
Timer.DelayCall(TimeSpan.FromSeconds(0.1), ContinueFlight);
|
||||
Timer.StartTimer(TimeSpan.FromSeconds(0.1), ContinueFlight);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
@ -864,7 +864,7 @@ namespace Server.Engines.ConPVP
|
|||
// has to be delayed in case some other target canceled us...
|
||||
if (m_Resend)
|
||||
{
|
||||
Timer.DelayCall(ResendBombTarget);
|
||||
Timer.StartTimer(ResendBombTarget);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -1613,24 +1613,13 @@ namespace Server.Engines.ConPVP
|
|||
{
|
||||
private BRBomb m_Bomb;
|
||||
|
||||
private Timer m_FinishTimer;
|
||||
private TimerExecutionToken _finishTimerToken;
|
||||
|
||||
public BRGame(BRController controller, DuelContext context) : base(context) => Controller = controller;
|
||||
|
||||
public BRController Controller { get; }
|
||||
|
||||
public Map Facet
|
||||
{
|
||||
get
|
||||
{
|
||||
if (m_Context.Arena != null)
|
||||
{
|
||||
return m_Context.Arena.Facet;
|
||||
}
|
||||
|
||||
return Controller.Map;
|
||||
}
|
||||
}
|
||||
public Map Facet => m_Context.Arena != null ? m_Context.Arena.Facet : Controller.Map;
|
||||
|
||||
public override bool CantDoAnything(Mobile mob) =>
|
||||
mob.Backpack?.FindItemByType<BRBomb>() != null && GetTeamInfo(mob) != null;
|
||||
|
|
@ -1641,7 +1630,7 @@ namespace Server.Engines.ConPVP
|
|||
{
|
||||
m_Bomb.Visible = false;
|
||||
m_Bomb.MoveToWorld(Controller.BombHome, Controller.Map);
|
||||
Timer.DelayCall(TimeSpan.FromSeconds(Utility.RandomMinMax(5, 15)), UnhideBomb);
|
||||
Timer.StartTimer(TimeSpan.FromSeconds(Utility.RandomMinMax(5, 15)), UnhideBomb);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -1724,7 +1713,7 @@ namespace Server.Engines.ConPVP
|
|||
|
||||
public void DelayBounce(TimeSpan ts, Mobile mob, Container corpse)
|
||||
{
|
||||
Timer.DelayCall(ts, DelayBounce_Callback, mob, corpse);
|
||||
Timer.StartTimer(ts, () => DelayBounce_Callback(mob, corpse));
|
||||
}
|
||||
|
||||
private void DelayBounce_Callback(Mobile mob, Container corpse)
|
||||
|
|
@ -1821,12 +1810,12 @@ namespace Server.Engines.ConPVP
|
|||
);
|
||||
}
|
||||
|
||||
m_FinishTimer?.Stop();
|
||||
_finishTimerToken.Cancel();
|
||||
|
||||
m_Bomb = new BRBomb(this);
|
||||
ReturnBomb();
|
||||
|
||||
m_FinishTimer = Timer.DelayCall(Controller.Duration, Finish_Callback);
|
||||
Timer.StartTimer(Controller.Duration, Finish_Callback, out _finishTimerToken);
|
||||
}
|
||||
|
||||
private void Finish_Callback()
|
||||
|
|
@ -2047,8 +2036,7 @@ namespace Server.Engines.ConPVP
|
|||
ApplyHues(m_Context.Participants[i], -1);
|
||||
}
|
||||
|
||||
m_FinishTimer?.Stop();
|
||||
m_FinishTimer = null;
|
||||
_finishTimerToken.Cancel();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -252,7 +252,7 @@ namespace Server.Engines.ConPVP
|
|||
|
||||
public Mobile m_Returner;
|
||||
public DateTime m_ReturnTime;
|
||||
private Timer m_ReturnTimer;
|
||||
private TimerExecutionToken _returnTimerToken;
|
||||
public CTFTeamInfo m_TeamInfo;
|
||||
|
||||
[Constructible]
|
||||
|
|
@ -370,18 +370,11 @@ namespace Server.Engines.ConPVP
|
|||
}
|
||||
}
|
||||
|
||||
private void StopCountdown()
|
||||
{
|
||||
m_ReturnTimer?.Stop();
|
||||
|
||||
m_ReturnTimer = null;
|
||||
}
|
||||
|
||||
private void BeginCountdown(int returnCount)
|
||||
{
|
||||
StopCountdown();
|
||||
_returnTimerToken.Cancel();
|
||||
|
||||
m_ReturnTimer = Timer.DelayCall(TimeSpan.FromSeconds(1.0), TimeSpan.FromSeconds(1.0), Countdown_OnTick);
|
||||
Timer.StartTimer(TimeSpan.FromSeconds(1.0), TimeSpan.FromSeconds(1.0), Countdown_OnTick, out _returnTimerToken);
|
||||
m_ReturnCount = returnCount;
|
||||
}
|
||||
|
||||
|
|
@ -518,7 +511,7 @@ namespace Server.Engines.ConPVP
|
|||
|
||||
public void SendHome()
|
||||
{
|
||||
StopCountdown();
|
||||
_returnTimerToken.Cancel();
|
||||
|
||||
if (m_TeamInfo == null)
|
||||
{
|
||||
|
|
@ -897,7 +890,7 @@ namespace Server.Engines.ConPVP
|
|||
|
||||
public sealed class CTFGame : EventGame
|
||||
{
|
||||
private Timer m_FinishTimer;
|
||||
private TimerExecutionToken _finishTimerToken;
|
||||
|
||||
public CTFGame(CTFController controller, DuelContext context) : base(context) => Controller = controller;
|
||||
|
||||
|
|
@ -1004,7 +997,7 @@ namespace Server.Engines.ConPVP
|
|||
|
||||
public void DelayBounce(TimeSpan ts, Mobile mob, Container corpse)
|
||||
{
|
||||
Timer.DelayCall(ts, DelayBounce_Callback, mob, corpse);
|
||||
Timer.StartTimer(ts, () => DelayBounce_Callback(mob, corpse));
|
||||
}
|
||||
|
||||
private void DelayBounce_Callback(Mobile mob, Container corpse)
|
||||
|
|
@ -1135,9 +1128,8 @@ namespace Server.Engines.ConPVP
|
|||
ApplyHues(m_Context.Participants[i], Controller.TeamInfo[i % 8].Color);
|
||||
}
|
||||
|
||||
m_FinishTimer?.Stop();
|
||||
|
||||
m_FinishTimer = Timer.DelayCall(Controller.Duration, Finish_Callback);
|
||||
_finishTimerToken.Cancel();
|
||||
Timer.StartTimer(Controller.Duration, Finish_Callback, out _finishTimerToken);
|
||||
}
|
||||
|
||||
private void Finish_Callback()
|
||||
|
|
@ -1364,9 +1356,7 @@ namespace Server.Engines.ConPVP
|
|||
ApplyHues(m_Context.Participants[i], -1);
|
||||
}
|
||||
|
||||
m_FinishTimer?.Stop();
|
||||
|
||||
m_FinishTimer = null;
|
||||
_finishTimerToken.Cancel();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -495,10 +495,9 @@ namespace Server.Engines.ConPVP
|
|||
private int m_CapStage;
|
||||
|
||||
private bool m_Capturable = true;
|
||||
private Timer m_CaptureTimer;
|
||||
|
||||
private Timer m_FinishTimer;
|
||||
private Timer m_UncaptureTimer;
|
||||
private TimerExecutionToken _captureTimerToken;
|
||||
private TimerExecutionToken _finishTimerToken;
|
||||
private TimerExecutionToken _uncaptureTimerToken;
|
||||
|
||||
public DDGame(DDController controller, DuelContext context) : base(context) => Controller = controller;
|
||||
|
||||
|
|
@ -587,7 +586,7 @@ namespace Server.Engines.ConPVP
|
|||
|
||||
public void DelayBounce(TimeSpan ts, Mobile mob, Container corpse)
|
||||
{
|
||||
Timer.DelayCall(ts, DelayBounce_Callback, mob, corpse);
|
||||
Timer.StartTimer(ts, () => DelayBounce_Callback(mob, corpse));
|
||||
}
|
||||
|
||||
private void DelayBounce_Callback(Mobile mob, Container corpse)
|
||||
|
|
@ -668,17 +667,8 @@ namespace Server.Engines.ConPVP
|
|||
{
|
||||
m_Capturable = true;
|
||||
|
||||
if (m_CaptureTimer != null)
|
||||
{
|
||||
m_CaptureTimer.Stop();
|
||||
m_CaptureTimer = null;
|
||||
}
|
||||
|
||||
if (m_UncaptureTimer != null)
|
||||
{
|
||||
m_UncaptureTimer.Stop();
|
||||
m_UncaptureTimer = null;
|
||||
}
|
||||
_captureTimerToken.Cancel();
|
||||
_uncaptureTimerToken.Cancel();
|
||||
|
||||
for (var i = 0; i < Controller.TeamInfo.Length; ++i)
|
||||
{
|
||||
|
|
@ -706,8 +696,8 @@ namespace Server.Engines.ConPVP
|
|||
);
|
||||
}
|
||||
|
||||
m_FinishTimer?.Stop();
|
||||
m_FinishTimer = Timer.DelayCall(Controller.Duration, Finish_Callback);
|
||||
_finishTimerToken.Cancel();
|
||||
Timer.StartTimer(Controller.Duration, Finish_Callback, out _finishTimerToken);
|
||||
}
|
||||
|
||||
private void Finish_Callback()
|
||||
|
|
@ -933,25 +923,15 @@ namespace Server.Engines.ConPVP
|
|||
|
||||
m_Capturable = false;
|
||||
|
||||
if (m_CaptureTimer != null)
|
||||
{
|
||||
m_CaptureTimer.Stop();
|
||||
m_CaptureTimer = null;
|
||||
}
|
||||
|
||||
if (m_UncaptureTimer != null)
|
||||
{
|
||||
m_UncaptureTimer.Stop();
|
||||
m_UncaptureTimer = null;
|
||||
}
|
||||
_captureTimerToken.Cancel();
|
||||
_uncaptureTimerToken.Cancel();
|
||||
|
||||
for (var i = 0; i < m_Context.Participants.Count; ++i)
|
||||
{
|
||||
ApplyHues(m_Context.Participants[i], -1);
|
||||
}
|
||||
|
||||
m_FinishTimer?.Stop();
|
||||
m_FinishTimer = null;
|
||||
_finishTimerToken.Cancel();
|
||||
}
|
||||
|
||||
public void Dominate(DDWayPoint point, Mobile from, DDTeamInfo team)
|
||||
|
|
@ -975,14 +955,13 @@ namespace Server.Engines.ConPVP
|
|||
|
||||
Controller.PointA?.SetNonCaptureHue();
|
||||
Controller.PointB?.SetNonCaptureHue();
|
||||
m_CaptureTimer?.Stop();
|
||||
m_CaptureTimer = null;
|
||||
_captureTimerToken.Cancel();
|
||||
}
|
||||
|
||||
if (!wasDom && isDom)
|
||||
{
|
||||
m_CapStage = 0;
|
||||
m_CaptureTimer = Timer.DelayCall(TimeSpan.Zero, TimeSpan.FromSeconds(1.0), CaptureTick);
|
||||
Timer.StartTimer(TimeSpan.Zero, TimeSpan.FromSeconds(1.0), CaptureTick, out _captureTimerToken);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -993,8 +972,7 @@ namespace Server.Engines.ConPVP
|
|||
if (team == null)
|
||||
{
|
||||
m_Capturable = true;
|
||||
m_CaptureTimer?.Stop();
|
||||
m_CaptureTimer = null;
|
||||
_captureTimerToken.Cancel();
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -1014,8 +992,7 @@ namespace Server.Engines.ConPVP
|
|||
|
||||
m_Capturable = false;
|
||||
m_CapStage = 0;
|
||||
m_CaptureTimer.Stop();
|
||||
m_CaptureTimer = null;
|
||||
_captureTimerToken.Cancel();
|
||||
|
||||
if (Controller.PointA != null)
|
||||
{
|
||||
|
|
@ -1029,8 +1006,7 @@ namespace Server.Engines.ConPVP
|
|||
Controller.PointB.SetUncapturableHue();
|
||||
}
|
||||
|
||||
m_UncaptureTimer = Timer.DelayCall(TimeSpan.FromSeconds(30.0), UncaptureTick);
|
||||
m_UncaptureTimer.Start();
|
||||
Timer.StartTimer(TimeSpan.FromSeconds(30.0), UncaptureTick, out _uncaptureTimerToken);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -1038,17 +1014,8 @@ namespace Server.Engines.ConPVP
|
|||
{
|
||||
m_Capturable = true;
|
||||
|
||||
if (m_CaptureTimer != null)
|
||||
{
|
||||
m_CaptureTimer.Stop();
|
||||
m_CaptureTimer = null;
|
||||
}
|
||||
|
||||
if (m_UncaptureTimer != null)
|
||||
{
|
||||
m_UncaptureTimer.Stop();
|
||||
m_UncaptureTimer = null;
|
||||
}
|
||||
_captureTimerToken.Cancel();
|
||||
_uncaptureTimerToken.Cancel();
|
||||
|
||||
if (Controller.PointA != null)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -25,8 +25,7 @@ namespace Server.Engines.ConPVP
|
|||
Name = "the hill";
|
||||
}
|
||||
|
||||
public HillOfTheKing(Serial s)
|
||||
: base(s)
|
||||
public HillOfTheKing(Serial s) : base(s)
|
||||
{
|
||||
}
|
||||
|
||||
|
|
@ -49,18 +48,7 @@ namespace Server.Engines.ConPVP
|
|||
[CommandProperty(AccessLevel.GameMaster)]
|
||||
public int ScoreInterval { get; set; }
|
||||
|
||||
public int CapturesSoFar
|
||||
{
|
||||
get
|
||||
{
|
||||
if (m_KingTimer != null)
|
||||
{
|
||||
return m_KingTimer.Captures;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
public int CapturesSoFar => m_KingTimer?.Captures ?? 0;
|
||||
|
||||
public override void Deserialize(IGenericReader reader)
|
||||
{
|
||||
|
|
@ -864,7 +852,7 @@ namespace Server.Engines.ConPVP
|
|||
|
||||
public sealed class KHGame : EventGame
|
||||
{
|
||||
private Timer m_FinishTimer;
|
||||
private TimerExecutionToken _finishTimerToken;
|
||||
|
||||
public KHGame(KHController controller, DuelContext context) : base(context) => Controller = controller;
|
||||
|
||||
|
|
@ -969,7 +957,7 @@ namespace Server.Engines.ConPVP
|
|||
|
||||
public void DelayBounce(TimeSpan ts, Mobile mob, Container corpse)
|
||||
{
|
||||
Timer.DelayCall(ts, DelayBounce_Callback, mob, corpse);
|
||||
Timer.StartTimer(ts, () => DelayBounce_Callback(mob, corpse));
|
||||
}
|
||||
|
||||
private void DelayBounce_Callback(Mobile mob, Container corpse)
|
||||
|
|
@ -994,7 +982,7 @@ namespace Server.Engines.ConPVP
|
|||
|
||||
if (corpse?.Deleted == false)
|
||||
{
|
||||
Timer.DelayCall(TimeSpan.FromSeconds(30), corpse.Delete);
|
||||
Timer.StartTimer(TimeSpan.FromSeconds(30), corpse.Delete);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -1067,8 +1055,6 @@ namespace Server.Engines.ConPVP
|
|||
);
|
||||
}
|
||||
|
||||
m_FinishTimer?.Stop();
|
||||
|
||||
for (var i = 0; i < Controller.Hills.Length; i++)
|
||||
{
|
||||
if (Controller.Hills[i] != null)
|
||||
|
|
@ -1085,7 +1071,8 @@ namespace Server.Engines.ConPVP
|
|||
}
|
||||
}
|
||||
|
||||
m_FinishTimer = Timer.DelayCall(Controller.Duration, Finish_Callback);
|
||||
_finishTimerToken.Cancel();
|
||||
Timer.StartTimer(Controller.Duration, Finish_Callback, out _finishTimerToken);
|
||||
}
|
||||
|
||||
private void Finish_Callback()
|
||||
|
|
@ -1310,8 +1297,7 @@ namespace Server.Engines.ConPVP
|
|||
ApplyHues(m_Context.Participants[i], -1);
|
||||
}
|
||||
|
||||
m_FinishTimer?.Stop();
|
||||
m_FinishTimer = null;
|
||||
_finishTimerToken.Cancel();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue