Commit 0bd7b221 authored by Kenton Varda's avatar Kenton Varda

Replace all uses of std::chrono with KJ clock APIs.

parent 36b91bdb
......@@ -28,7 +28,6 @@
#include <errno.h>
#include <inttypes.h>
#include <limits>
#include <chrono>
#include <pthread.h>
#include <map>
#include <sys/wait.h>
......@@ -44,14 +43,6 @@
namespace kj {
// =======================================================================================
// Timer code common to multiple implementations
TimePoint UnixEventPort::readClock() {
return origin<TimePoint>() + std::chrono::duration_cast<std::chrono::nanoseconds>(
std::chrono::steady_clock::now().time_since_epoch()).count() * NANOSECONDS;
}
// =======================================================================================
// Signal code common to multiple implementations
......@@ -284,7 +275,8 @@ void UnixEventPort::gotSignal(const siginfo_t& siginfo) {
// epoll FdObserver implementation
UnixEventPort::UnixEventPort()
: timerImpl(readClock()),
: clock(systemPreciseMonotonicClock()),
timerImpl(clock.now()),
epollFd(-1),
signalFd(-1),
eventFd(-1) {
......@@ -413,7 +405,7 @@ Promise<void> UnixEventPort::FdObserver::whenWriteDisconnected() {
bool UnixEventPort::wait() {
return doEpollWait(
timerImpl.timeoutToNextEvent(readClock(), MILLISECONDS, int(maxValue))
timerImpl.timeoutToNextEvent(clock.now(), MILLISECONDS, int(maxValue))
.map([](uint64_t t) -> int { return t; })
.orDefault(-1));
}
......@@ -602,7 +594,7 @@ bool UnixEventPort::doEpollWait(int timeout) {
}
}
timerImpl.advanceTo(readClock());
timerImpl.advanceTo(clock.now());
return woken;
}
......@@ -616,7 +608,8 @@ bool UnixEventPort::doEpollWait(int timeout) {
#endif
UnixEventPort::UnixEventPort()
: timerImpl(readClock()) {
: clock(systemPreciseMonotonicClock()),
timerImpl(clock.now()) {
static_assert(sizeof(threadId) >= sizeof(pthread_t),
"pthread_t is larger than a long long on your platform. Please port.");
*reinterpret_cast<pthread_t*>(&threadId) = pthread_self();
......@@ -854,7 +847,7 @@ bool UnixEventPort::wait() {
sigprocmask(SIG_UNBLOCK, &newMask, &origMask);
pollContext.run(
timerImpl.timeoutToNextEvent(readClock(), MILLISECONDS, int(maxValue))
timerImpl.timeoutToNextEvent(clock.now(), MILLISECONDS, int(maxValue))
.map([](uint64_t t) -> int { return t; })
.orDefault(-1));
......@@ -863,7 +856,7 @@ bool UnixEventPort::wait() {
// Queue events.
pollContext.processResults();
timerImpl.advanceTo(readClock());
timerImpl.advanceTo(clock.now());
return false;
}
......@@ -925,7 +918,7 @@ bool UnixEventPort::poll() {
pollContext.run(0);
pollContext.processResults();
}
timerImpl.advanceTo(readClock());
timerImpl.advanceTo(clock.now());
return woken;
}
......
......@@ -142,12 +142,12 @@ private:
class SignalPromiseAdapter;
class ChildExitPromiseAdapter;
const MonotonicClock& clock;
TimerImpl timerImpl;
SignalPromiseAdapter* signalHead = nullptr;
SignalPromiseAdapter** signalTail = &signalHead;
TimePoint readClock();
void gotSignal(const siginfo_t& siginfo);
friend class TimerPromiseAdapter;
......
......@@ -28,7 +28,7 @@
#include "async-win32.h"
#include "debug.h"
#include <atomic>
#include <chrono>
#include "time.h"
#include "refcount.h"
#include <ntsecapi.h> // NTSTATUS
#include <ntstatus.h> // STATUS_SUCCESS
......@@ -38,7 +38,8 @@
namespace kj {
Win32IocpEventPort::Win32IocpEventPort()
: iocp(newIocpHandle()), thread(openCurrentThread()), timerImpl(readClock()) {}
: clock(systemPreciseMonotonicClock()),
iocp(newIocpHandle()), thread(openCurrentThread()), timerImpl(clock.now()) {}
Win32IocpEventPort::~Win32IocpEventPort() noexcept(false) {}
......@@ -157,17 +158,12 @@ Own<Win32EventPort::SignalObserver> Win32IocpEventPort::observeSignalState(HANDL
return waitThreads.observeSignalState(handle);
}
TimePoint Win32IocpEventPort::readClock() {
return origin<TimePoint>() + std::chrono::duration_cast<std::chrono::nanoseconds>(
std::chrono::steady_clock::now().time_since_epoch()).count() * NANOSECONDS;
}
bool Win32IocpEventPort::wait() {
waitIocp(timerImpl.timeoutToNextEvent(readClock(), MILLISECONDS, INFINITE - 1)
waitIocp(timerImpl.timeoutToNextEvent(clock.now(), MILLISECONDS, INFINITE - 1)
.map([](uint64_t t) -> DWORD { return t; })
.orDefault(INFINITE));
timerImpl.advanceTo(readClock());
timerImpl.advanceTo(clock.now());
return receivedWake();
}
......
......@@ -209,6 +209,8 @@ private:
class IoOperationImpl;
class IoObserverImpl;
const MonotonicClock& clock;
AutoCloseHandle iocp;
AutoCloseHandle thread;
Win32WaitObjectThreadPool waitThreads;
......@@ -216,8 +218,6 @@ private:
mutable std::atomic<bool> sentWake {false};
bool isAllowApc = false;
static TimePoint readClock();
void waitIocp(DWORD timeoutMs);
// Wait on the I/O completion port for up to timeoutMs and pump events. Does not advance the
// timer; caller must do that.
......
......@@ -30,7 +30,6 @@
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <chrono>
#include "time.h"
#ifndef _WIN32
#include <sys/mman.h>
......@@ -185,8 +184,7 @@ private:
};
TimePoint readClock() {
return origin<TimePoint>() + std::chrono::duration_cast<std::chrono::nanoseconds>(
std::chrono::steady_clock::now().time_since_epoch()).count() * NANOSECONDS;
return systemPreciseMonotonicClock().now();
}
} // namespace
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment