ref: e228d9b24953a72ca8e10c0b8024f3ac9ce1fcb4
dir: /src/plan9/sys.c/
#include "flisp.h"
#include "timefuncs.h"
#include <tos.h>
#define Nsec 1000000000ULL
double D_PNAN, D_PINF;
double
sec_realtime(void)
{
uvlong t = nsec();
uvlong ns = t % Nsec;
uvlong s = (t - ns) / Nsec;
return (double)s + (double)ns/1.0e9;
}
/*
* nsec() is wallclock and can be adjusted by timesync
* so need to use cycles() instead, but fall back to
* nsec() in case we can't
*/
u64int
nanosec_monotonic(void)
{
static u64int fasthz, xstart;
u64int x;
if(fasthz == ~0ULL)
return nsec() - xstart;
if(fasthz == 0){
if(_tos->cyclefreq){
fasthz = _tos->cyclefreq;
cycles(&xstart);
} else {
fasthz = ~0ULL;
xstart = nsec();
}
return 0;
}
cycles(&x);
x -= xstart;
u64int q = x / fasthz;
u64int r = x % fasthz;
return q*Nsec + r*Nsec/fasthz;
}
void
timestring(double s, char *buf, int sz)
{
Tm tm;
snprint(buf, sz, "%τ", tmfmt(tmtime(&tm, s, tzload("local")), nil));
}
double
parsetime(const char *s)
{
Tm tm;
if(tmparse(&tm, "?WWW, ?MM ?DD hh:mm:ss ?Z YYYY", s, tzload("local"), nil) == nil)
return -1;
return tmnorm(&tm);
}
void
sleep_ms(int ms)
{
if(ms != 0)
sleep(ms);
}
int
ftruncate(int f, off_t sz)
{
Dir d;
nulldir(&d);
d.length = sz;
return dirfwstat(f, &d) > 0 ? 0 : -1;
}
extern uchar bootcode[];
extern ulong bootlen;
void
main(int argc, char **argv)
{
argv0 = argv[0];
setfcr(FPPDBL|FPRNR|FPOVFL);
tmfmtinstall();
D_PNAN = strtod("+NaN", nil);
D_PINF = strtod("+Inf", nil);
flmain(bootcode, bootlen, argc, argv);
}