Use nanosleep() instead of a busy wait loop.

This cuts the cpu time requirements in half. Windows has no equivalent
for nanosleep(), so keep the busy loop.
This commit is contained in:
Yamagi Burmeister 2016-08-14 12:50:27 +02:00
parent 98683cbc31
commit 0ba7614739

View file

@ -26,9 +26,8 @@
*/ */
#include <fcntl.h> #include <fcntl.h>
#include <locale.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <time.h>
#include <unistd.h> #include <unistd.h>
#include "../../common/header/common.h" #include "../../common/header/common.h"
@ -40,6 +39,7 @@ main(int argc, char **argv)
int time, oldtime, newtime; int time, oldtime, newtime;
int verLen, i; int verLen, i;
const char* versionString; const char* versionString;
struct timespec t;
/* register signal handler */ /* register signal handler */
registerHandler(); registerHandler();
@ -123,6 +123,7 @@ main(int argc, char **argv)
fcntl(fileno(stdin), F_SETFL, fcntl(fileno(stdin), F_GETFL, NULL) | FNDELAY); fcntl(fileno(stdin), F_SETFL, fcntl(fileno(stdin), F_GETFL, NULL) | FNDELAY);
oldtime = Sys_Milliseconds(); oldtime = Sys_Milliseconds();
t.tv_sec = 0;
/* The legendary Quake II mainloop */ /* The legendary Quake II mainloop */
while (1) while (1)
@ -130,6 +131,10 @@ main(int argc, char **argv)
/* find time spent rendering last frame */ /* find time spent rendering last frame */
do do
{ {
/* Sleep 10 microseconds */
t.tv_nsec = 10000;
nanosleep(&t, NULL);
newtime = Sys_Milliseconds(); newtime = Sys_Milliseconds();
time = newtime - oldtime; time = newtime - oldtime;
} }