Files
ternfs-XTXMarkets/cpp/core/Time.hpp
Francesco Mazzoli 38f3d54ecd Wait forever, rather than having timeouts
The goal here is to not have constant wakeups due to timeout. Do
not attempt to clean things up nicely before termination -- just
terminate instead. We can setup a proper termination system in
the future, I first want to see if this makes a difference.

Also, change xmon to use pipes for communication, so that it can
wait without timers as well.

Also, `write` directly for logging, so that we know the logs will
make it to the file after the logging call returns (since we now
do not have the chance to flush them afterwards).
2023-12-07 10:11:19 +00:00

120 lines
2.9 KiB
C++

#pragma once
#include <time.h>
#include "Common.hpp"
#include "Bincode.hpp"
struct Duration {
int64_t ns;
constexpr Duration(): ns(0) {}
constexpr Duration(int64_t ns_): ns(ns_) {}
constexpr Duration(const struct timespec& ts): ns(ts.tv_sec*1'000'000'000ull + ts.tv_nsec) {}
bool operator==(Duration rhs) const {
return ns == rhs.ns;
}
bool operator>(Duration rhs) const {
return ns > rhs.ns;
}
bool operator>=(Duration rhs) const {
return ns >= rhs.ns;
}
bool operator<(Duration rhs) const {
return ns < rhs.ns;
}
bool operator<=(Duration rhs) const {
return ns <= rhs.ns;
}
Duration operator+(Duration d) const {
return Duration(ns + d.ns);
}
Duration operator*(int64_t x) const {
return Duration(ns * x);
}
Duration operator-(Duration d) const {
return ns - d.ns;
}
struct timespec timespec() const {
struct timespec ts;
ts.tv_sec = ns / 1'000'000'000ull;
ts.tv_nsec = ns % 1'000'000'000ull;
return ts;
}
// sleeps, returns a non-zero duration if we were interrupted
Duration sleep() const;
// sleeps, retrying if we get EINTR
void sleepRetry() const;
};
constexpr Duration operator "" _ns (unsigned long long t) { return Duration(t); }
constexpr Duration operator "" _us (unsigned long long t) { return Duration(t*1'000); }
constexpr Duration operator "" _ms (unsigned long long t) { return Duration(t*1'000'000); }
constexpr Duration operator "" _sec (unsigned long long t) { return Duration(t*1'000'000'000ull); }
constexpr Duration operator "" _mins (unsigned long long t) { return Duration(t*1'000'000'000ull*60); }
constexpr Duration operator "" _hours(unsigned long long t) { return Duration(t*1'000'000'000ull*60*60); }
std::ostream& operator<<(std::ostream& out, Duration d);
struct EggsTime {
uint64_t ns;
EggsTime(): ns(0) {}
EggsTime(uint64_t ns_): ns(ns_) {}
bool operator==(EggsTime rhs) const {
return ns == rhs.ns;
}
bool operator>(EggsTime rhs) const {
return ns > rhs.ns;
}
bool operator>=(EggsTime rhs) const {
return ns >= rhs.ns;
}
bool operator<=(EggsTime rhs) const {
return ns <= rhs.ns;
}
bool operator<(EggsTime rhs) const {
return ns < rhs.ns;
}
void pack(BincodeBuf& buf) const {
buf.packScalar<uint64_t>(ns);
}
void unpack(BincodeBuf& buf) {
ns = buf.unpackScalar<uint64_t>();
}
EggsTime operator+(Duration d) {
return EggsTime(ns + d.ns);
}
// Two positive times might give one negative
// duration.
#ifdef __clang__
__attribute__((no_sanitize("integer")))
#endif
Duration operator-(EggsTime d) {
return Duration(ns - d.ns);
}
};
std::ostream& operator<<(std::ostream& out, EggsTime t);
EggsTime eggsNow();