Implement callback based timers without relying on a task. Fixes #86.

In the previous implementation, the callback tasks were starving as soon as the last external reference to a non-pending timer was given up.
This commit is contained in:
Sönke Ludwig 2018-10-27 15:48:21 +02:00
parent 358232abd1
commit 78fef30310
2 changed files with 31 additions and 17 deletions

View file

@ -4,7 +4,7 @@ authors "Sönke Ludwig"
copyright "Copyright © 2016-2018, rejectedsoftware e.K." copyright "Copyright © 2016-2018, rejectedsoftware e.K."
license "MIT" license "MIT"
dependency "eventcore" version="~>0.8.32" dependency "eventcore" version="~>0.8.39"
dependency "stdx-allocator" version="~>2.77.0" dependency "stdx-allocator" version="~>2.77.0"
targetName "vibe_core" targetName "vibe_core"

View file

@ -742,7 +742,7 @@ unittest {
See_also: createTimer See_also: createTimer
*/ */
Timer setTimer(Duration timeout, void delegate() nothrow @safe callback, bool periodic = false) Timer setTimer(Duration timeout, Timer.Callback callback, bool periodic = false)
@safe nothrow { @safe nothrow {
auto tm = createTimer(callback); auto tm = createTimer(callback);
tm.rearm(timeout, periodic); tm.rearm(timeout, periodic);
@ -784,16 +784,7 @@ Timer setTimer(Duration timeout, void delegate() callback, bool periodic = false
*/ */
Timer createTimer(void delegate() nothrow @safe callback) Timer createTimer(void delegate() nothrow @safe callback)
@safe nothrow { @safe nothrow {
auto ret = Timer(eventDriver.timers.create()); return Timer(eventDriver.timers.create, callback);
if (callback !is null) {
runTask((void delegate() nothrow @safe cb, Timer tm) {
while (!tm.unique || tm.pending) {
tm.wait();
cb();
}
}, callback, ret);
}
return ret;
} }
@ -1036,13 +1027,20 @@ struct Timer {
debug uint m_magicNumber = 0x4d34f916; debug uint m_magicNumber = 0x4d34f916;
} }
alias Callback = void delegate() @safe nothrow;
@safe: @safe:
private this(TimerID id) private this(TimerID id, Callback callback)
nothrow { nothrow {
assert(id != TimerID.init, "Invalid timer ID."); assert(id != TimerID.init, "Invalid timer ID.");
m_driver = eventDriver; m_driver = eventDriver;
m_id = id; m_id = id;
if (callback) {
m_driver.timers.userData!Callback(m_id) = callback;
m_driver.timers.wait(m_id, timerCallbackHandler);
}
} }
this(this) this(this)
@ -1054,8 +1052,7 @@ struct Timer {
~this() ~this()
nothrow { nothrow {
debug assert(m_magicNumber == 0x4d34f916, "Timer corrupted."); debug assert(m_magicNumber == 0x4d34f916, "Timer corrupted.");
if (m_driver) if (m_driver) releaseHandle!"timers"(m_id, () @trusted { return cast(shared)m_driver; } ());
releaseHandle!"timers"(m_id, () @trusted { return cast(shared)m_driver; } ());
} }
/// True if the timer is yet to fire. /// True if the timer is yet to fire.
@ -1080,16 +1077,33 @@ struct Timer {
void stop() nothrow { if (m_driver) m_driver.timers.stop(m_id); } void stop() nothrow { if (m_driver) m_driver.timers.stop(m_id); }
/** Waits until the timer fires. /** Waits until the timer fires.
Returns:
`true` is returned $(I iff) the timer was fired.
*/ */
void wait() bool wait()
{ {
asyncAwait!(TimerCallback, auto cb = m_driver.timers.userData!Callback(m_id);
assert(cb is null, "Cannot wait on a timer that was created with a callback.");
auto res = asyncAwait!(TimerCallback2,
cb => m_driver.timers.wait(m_id, cb), cb => m_driver.timers.wait(m_id, cb),
cb => m_driver.timers.cancelWait(m_id) cb => m_driver.timers.cancelWait(m_id)
); );
return res[1];
} }
} }
private immutable TimerCallback2 timerCallbackHandler = (TimerID timer, bool fired) {
if (fired) {
auto cb = eventDriver.timers.userData!(Timer.Callback)(timer);
cb();
}
if (!eventDriver.timers.isUnique(timer))
eventDriver.timers.wait(timer, timerCallbackHandler);
};
/** Returns an object that ensures that no task switches happen during its life time. /** Returns an object that ensures that no task switches happen during its life time.