ref: 6777c495b3f5ba30a7b2442e176433ebebef7ab7
dir: /src/plan9/sys.c/
#include "sl.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;
}
void *
sl_segalloc(usize sz)
{
return segattach(SG_CEXEC, "memory", nil, sz);
}
void
sl_segfree(void *s, usize sz)
{
USED(sz);
if(s != nil)
segdetach(s);
}
void
sl_segused(void *s, usize sz, usize used)
{
USED(s); USED(sz); USED(used);
}
/*
* 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, soffset 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);
slmain(bootcode, bootlen, argc, argv);
}