From a408aac8082eb8d289a5327071dafe617f74840b Mon Sep 17 00:00:00 2001 From: Hiroki Noda Date: Mon, 24 Dec 2018 06:22:53 +0900 Subject: [PATCH] Use monotonic clock for timeout --- source/vibe/core/file.d | 7 ++++--- source/vibe/core/sync.d | 22 +++++++++++----------- tests/vibe.core.net.1429.d | 7 +++---- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/source/vibe/core/file.d b/source/vibe/core/file.d index ac9cdf0..3f92ec0 100644 --- a/source/vibe/core/file.d +++ b/source/vibe/core/file.d @@ -19,6 +19,7 @@ import core.stdc.stdio; import core.sys.posix.unistd; import core.sys.posix.fcntl; import core.sys.posix.sys.stat; +import core.time; import std.conv : octal; import std.datetime; import std.exception; @@ -652,11 +653,11 @@ struct DirectoryWatcher { // TODO: avoid all those heap allocations! while (!m_context.changes.data.length) m_context.changeEvent.wait(Duration.max, m_context.changeEvent.emitCount); } else { - SysTime now = Clock.currTime(UTC()); - SysTime final_time = now + timeout; + MonoTime now = MonoTime.currTime(); + MonoTime final_time = now + timeout; while (!m_context.changes.data.length) { m_context.changeEvent.wait(final_time - now, m_context.changeEvent.emitCount); - now = Clock.currTime(UTC()); + now = MonoTime.currTime(); if (now >= final_time) break; } if (!m_context.changes.data.length) return false; diff --git a/source/vibe/core/sync.d b/source/vibe/core/sync.d index 9d1689b..20b50d6 100644 --- a/source/vibe/core/sync.d +++ b/source/vibe/core/sync.d @@ -767,20 +767,20 @@ struct LocalManualEvent { private int doWait(bool interruptible)(Duration timeout, int emit_count) { - import std.datetime : Clock, SysTime, UTC; + import core.time : MonoTime; assert(m_waiter !is null, "LocalManualEvent is not initialized - use createManualEvent()"); - SysTime target_timeout, now; + MonoTime target_timeout, now; if (timeout != Duration.max) { - try now = Clock.currTime(UTC()); + try now = MonoTime.currTime(); catch (Exception e) { assert(false, e.msg); } target_timeout = now + timeout; } while (m_waiter.m_emitCount - emit_count <= 0) { m_waiter.wait!interruptible(timeout != Duration.max ? target_timeout - now : Duration.max); - try now = Clock.currTime(UTC()); + try now = MonoTime.currTime(); catch (Exception e) { assert(false, e.msg); } if (now >= target_timeout) break; } @@ -1008,7 +1008,7 @@ struct ManualEvent { private int doWaitShared(bool interruptible)(Duration timeout, int emit_count) shared { - import std.datetime : SysTime, Clock, UTC; + import core.time : MonoTime; () @trusted { logTrace("wait shared %s", cast(void*)&this); } (); @@ -1017,9 +1017,9 @@ struct ManualEvent { assert(ms_threadEvent != EventID.invalid, "Failed to create event!"); } - SysTime target_timeout, now; + MonoTime target_timeout, now; if (timeout != Duration.max) { - try now = Clock.currTime(UTC()); + try now = MonoTime.currTime(); catch (Exception e) { assert(false, e.msg); } target_timeout = now + timeout; } @@ -1032,7 +1032,7 @@ struct ManualEvent { ec = this.emitCount; if (timeout != Duration.max) { - try now = Clock.currTime(UTC()); + try now = MonoTime.currTime(); catch (Exception e) { assert(false, e.msg); } if (now >= target_timeout) break; } @@ -1296,7 +1296,7 @@ private final class ThreadLocalWaiter(bool EVENT_TRIGGERED) { bool wait(bool interruptible)(Duration timeout, EventID evt = EventID.invalid, scope bool delegate() @safe nothrow exit_condition = null) @safe { - import std.datetime : SysTime, Clock, UTC; + import core.time : MonoTime; import vibe.internal.async : Waitable, asyncAwaitAny; TaskWaiter waiter_store; @@ -1310,9 +1310,9 @@ private final class ThreadLocalWaiter(bool EVENT_TRIGGERED) { assert(!waiter.next); } - SysTime target_timeout, now; + MonoTime target_timeout, now; if (timeout != Duration.max) { - try now = Clock.currTime(UTC()); + try now = MonoTime.currTime(); catch (Exception e) { assert(false, e.msg); } target_timeout = now + timeout; } diff --git a/tests/vibe.core.net.1429.d b/tests/vibe.core.net.1429.d index 81be91f..14c7fcc 100644 --- a/tests/vibe.core.net.1429.d +++ b/tests/vibe.core.net.1429.d @@ -8,8 +8,7 @@ module test; import vibe.core.core; import vibe.core.log : logInfo; import vibe.core.net; -import core.time : msecs; -import std.datetime : Clock, UTC; +import core.time : MonoTime, msecs; void main() { @@ -21,12 +20,12 @@ void main() }); runTask({ - auto start = Clock.currTime(UTC()); + auto start = MonoTime.currTime(); try { udp.recv(100.msecs); assert(false, "Timeout did not occur."); } catch (Exception e) { - auto duration = Clock.currTime(UTC()) - start; + auto duration = MonoTime.currTime() - start; assert(duration >= 99.msecs, "Timeout occurred too early"); assert(duration >= 99.msecs && duration < 150.msecs, "Timeout occurred too late."); logInfo("UDP receive timeout test was successful.");