Merge pull request #88 from vibe-d/extended_timer_callback

Introduce TimerCallback2.
This commit is contained in:
Sönke Ludwig 2018-10-27 14:11:39 +02:00 committed by GitHub
commit 9b75a3b7d9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 6 deletions

View file

@ -564,7 +564,12 @@ interface EventDriverTimers {
void stop(TimerID timer);
bool isPending(TimerID timer);
bool isPeriodic(TimerID timer);
void wait(TimerID timer, TimerCallback callback);
final void wait(TimerID timer, TimerCallback callback) {
wait(timer, (tm, fired) {
if (fired) callback(tm);
});
}
void wait(TimerID timer, TimerCallback2 callback);
void cancelWait(TimerID timer);
/** Increments the reference count of the given resource.
@ -668,6 +673,7 @@ alias FileIOCallback = void delegate(FileFD, IOStatus, size_t);
alias EventCallback = void delegate(EventID);
alias SignalCallback = void delegate(SignalListenID, SignalStatus, int);
alias TimerCallback = void delegate(TimerID);
alias TimerCallback2 = void delegate(TimerID, bool fired);
alias FileChangesCallback = void delegate(WatcherID, in ref FileChange change);
@system alias DataInitializer = void function(void*) @nogc;

View file

@ -79,7 +79,7 @@ final class LoopTimeoutTimerDriver : EventDriverTimers {
foreach (tm; processed_timers) {
auto cb = tm.callback;
tm.callback = null;
if (cb) cb(tm.id);
if (cb) cb(tm.id, true);
}
return processed_timers.length > 0;
@ -91,8 +91,8 @@ final class LoopTimeoutTimerDriver : EventDriverTimers {
TimerSlot* tm;
try tm = ms_allocator.make!TimerSlot;
catch (Exception e) return TimerID.invalid;
GC.addRange(tm, TimerSlot.sizeof, typeid(TimerSlot));
assert(tm !is null);
GC.addRange(tm, TimerSlot.sizeof, typeid(TimerSlot));
tm.id = id;
tm.refCount = 1;
tm.timeout = long.max;
@ -113,8 +113,13 @@ final class LoopTimeoutTimerDriver : EventDriverTimers {
final override void stop(TimerID timer)
@trusted {
import std.algorithm.mutation : swap;
auto tm = m_timers[timer];
if (!tm.pending) return;
TimerCallback2 cb;
swap(cb, tm.callback);
if (cb) cb(timer, false);
tm.pending = false;
m_timerQueue.remove(tm);
}
@ -129,11 +134,12 @@ final class LoopTimeoutTimerDriver : EventDriverTimers {
return m_timers[descriptor].repeatDuration > 0;
}
final override void wait(TimerID timer, TimerCallback callback)
final override void wait(TimerID timer, TimerCallback2 callback)
{
assert(!m_timers[timer].callback, "Calling wait() no a timer that is already waiting.");
assert(!m_timers[timer].callback, "Calling wait() on a timer that is already waiting.");
m_timers[timer].callback = callback;
}
alias wait = EventDriverTimers.wait;
final override void cancelWait(TimerID timer)
{
@ -214,7 +220,7 @@ struct TimerSlot {
bool pending;
long timeout; // stdtime
long repeatDuration;
TimerCallback callback; // TODO: use a list with small-value optimization
TimerCallback2 callback; // TODO: use a list with small-value optimization
DataInitializer userDataDestructor;
ubyte[16*size_t.sizeof] userData;

View file

@ -57,6 +57,21 @@ void main()
eventDriver.timers.set(tm, 1200.msecs, 0.msecs);
// test if stop() produces a callback with fire==false
bool got_event = false;
auto tm2 = eventDriver.timers.create();
eventDriver.timers.wait(tm2, (t, fired) { assert(!fired); got_event = true; });
eventDriver.timers.set(tm2, 100.msecs, 0.msecs);
eventDriver.timers.stop(tm2);
assert(got_event);
// test that cancelWait() does not produce a callback
eventDriver.timers.wait(tm2, (t, fired) { assert(false); });
eventDriver.timers.set(tm2, 100.msecs, 0.msecs);
eventDriver.timers.cancelWait(tm2);
eventDriver.timers.stop(tm2);
ExitReason er;
do er = eventDriver.core.processEvents(Duration.max);
while (er == ExitReason.idle);