/// <summary> /// /// </summary> /// <param name="task">task</param> /// <param name="delay">A delay in addition to the default duration of the timeout</param> public void Schedule(TimeoutTask task, long delay) { lock (_lock) { if (task._timestamp != 0) { task.Unlink(); task._timestamp = 0; } task._timeout = this; task._expired = false; task._delay = delay; task._timestamp = _now + delay; TimeoutTask last = _head._prev; while (last != _head) { if (last._timestamp <= task._timestamp) { break; } last = last._prev; } last.Link(task); } }
/// <summary> /// Get an expired tasks. /// This is called instead of Tick() to obtain the next /// expired Task, but without calling it's Task#Expire() or /// Task#Expired() methods. /// </summary> /// <returns>the next expired task or null.</returns> public TimeoutTask Expired() { lock (_lock) { long _expiry = _now - _duration; if (_head._next != _head) { TimeoutTask task = _head._next; if (task._timestamp > _expiry) { return(null); } task.Unlink(); task._expired = true; return(task); } return(null); } }
public void Tick(long now) { long _expiry = -1; TimeoutTask task = null; while (true) { try { lock (_lock) { if (_expiry == -1) { if (now != -1) { _now = now; } _expiry = _now - _duration; } task = _head._next; if (task == _head || task._timestamp > _expiry) { break; } task.Unlink(); task._expired = true; task.Expire(); } task.Expired(); } catch (Exception exception) { Log.Warn(Log.EXCEPTION, exception); } } }