ref: 054c066680b24a99a56b232a32edfe9c3484f734
parent: 06f0f47a9dde43779913462a8e5ab5ccbf0c514a
author: Sigrid Haflínudóttir <ftrvxmtrx@gmail.com>
date: Sat Feb 22 06:33:15 EST 2020
plan9: nanosec: fall back to nsec() in case cycle is unusable
--- a/plan9.c
+++ b/plan9.c
@@ -361,7 +361,8 @@
/*
* nsec() is wallclock and can be adjusted by timesync
- * so need to use cycles() instead
+ * so need to use cycles() instead, but fall back to
+ * nsec() in case we can't
*
* "fasthz" is how many ticks there are in a second
* can be read from /dev/time
@@ -376,20 +377,28 @@
int f, n, i;
char tmp[128], *e;
- if (fasthz < 1) {
- if ((f = open("/dev/time", OREAD)) < 0)
- sysfatal("failed to open /dev/time");
- if ((n = read(f, tmp, sizeof(tmp)-1)) < 2)
- sysfatal("failed to read /dev/time");
- tmp[n] = 0;
- e = tmp;
- for (i = 0; i < 3; i++)
- strtoll(e, &e, 10);
- fasthz = strtoll(e, nil, 10);
- if (fasthz < 1)
- sysfatal("failed to read or invalid fasthz");
- close(f);
- cycles(&xstart);
+ if (fasthz == ~0ULL)
+ return nsec() - xstart;
+
+ if (fasthz == 0) {
+ fasthz = ~0ULL;
+ if ((f = open("/dev/time", OREAD)) >= 0 && (n = read(f, tmp, sizeof(tmp)-1)) > 2) {
+ tmp[n] = 0;
+ e = tmp;
+ for (i = 0; i < 3; i++)
+ strtoll(e, &e, 10);
+ fasthz = strtoll(e, nil, 10);
+ if (fasthz < 1)
+ fasthz = ~0ULL;
+ close(f);
+ cycles(&xstart);
+ }
+ if (fasthz == ~0ULL) {
+ fprint(2, "couldn't get fasthz, falling back to nsec()\n");
+ fprint(2, "you might want to disable aux/timesync\n");
+ xstart = nsec();
+ return 0;
+ }
}
cycles(&x);
if (x < xstart) { /* wrap around */
@@ -441,9 +450,7 @@
else if (end - n > 25000000LL)
sleep(20);
else if (end - n > 10000000LL)
- sleep((end - n) / 1000000LL);
- else
- yield();
+ sleep(1);
} while (!forward);
framedev = (processnew - processold - 15000000000LL/bpm)/1000000LL;