mirror of
https://github.com/openbsd/src.git
synced 2025-01-10 06:47:55 -08:00
Improve robots(6) by using timespec*() functions, replacing gettimeofday()
with clock_gettime(MONOTONIC) to avoid clock changes and replacing poll() with ppoll() to deal better with timespec. ok guenther@.
This commit is contained in:
parent
1d4f5a7d2d
commit
e5f400f567
@ -1,4 +1,4 @@
|
||||
/* $OpenBSD: extern.c,v 1.6 2014/11/03 22:14:54 deraadt Exp $ */
|
||||
/* $OpenBSD: extern.c,v 1.7 2015/08/26 00:29:24 rzalamena Exp $ */
|
||||
/* $NetBSD: extern.c,v 1.3 1995/04/22 10:08:49 cgd Exp $ */
|
||||
|
||||
/*
|
||||
@ -62,7 +62,7 @@ int Score; /* Current score */
|
||||
int Start_level = 1; /* Level on which to start */
|
||||
int Wait_bonus; /* bonus for waiting */
|
||||
|
||||
struct timeval tv; /* how long to wait; could be an option */
|
||||
struct timespec tv; /* how long to wait; could be an option */
|
||||
|
||||
COORD Max; /* Max area robots take up */
|
||||
COORD Min; /* Min area robots take up */
|
||||
|
@ -1,4 +1,4 @@
|
||||
/* $OpenBSD: main.c,v 1.19 2014/11/03 22:14:54 deraadt Exp $ */
|
||||
/* $OpenBSD: main.c,v 1.20 2015/08/26 00:29:24 rzalamena Exp $ */
|
||||
/* $NetBSD: main.c,v 1.5 1995/04/22 10:08:54 cgd Exp $ */
|
||||
|
||||
/*
|
||||
@ -70,7 +70,6 @@ main(int ac, char *av[])
|
||||
Real_time = TRUE;
|
||||
/* Could be a command-line option */
|
||||
tv.tv_sec = 3;
|
||||
tv.tv_usec = 0;
|
||||
break;
|
||||
case 'a':
|
||||
Start_level = 4;
|
||||
|
@ -1,4 +1,4 @@
|
||||
/* $OpenBSD: move.c,v 1.10 2014/11/03 22:14:54 deraadt Exp $ */
|
||||
/* $OpenBSD: move.c,v 1.11 2015/08/26 00:29:24 rzalamena Exp $ */
|
||||
/* $NetBSD: move.c,v 1.4 1995/04/22 10:08:58 cgd Exp $ */
|
||||
|
||||
/*
|
||||
@ -43,8 +43,7 @@ get_move(void)
|
||||
{
|
||||
int c;
|
||||
int retval;
|
||||
struct timeval t, tod;
|
||||
struct timezone tz;
|
||||
struct timespec t, tn;
|
||||
#ifdef FANCY
|
||||
int lastmove;
|
||||
#endif
|
||||
@ -61,9 +60,8 @@ get_move(void)
|
||||
}
|
||||
#endif
|
||||
if (Real_time) {
|
||||
t.tv_sec = tv.tv_sec;
|
||||
t.tv_usec = tv.tv_usec;
|
||||
(void)gettimeofday(&tod, &tz);
|
||||
t = tv;
|
||||
clock_gettime(CLOCK_MONOTONIC, &tn);
|
||||
}
|
||||
for (;;) {
|
||||
if (Teleport && must_telep())
|
||||
@ -94,8 +92,7 @@ over:
|
||||
|
||||
pfd[0].fd = STDIN_FILENO;
|
||||
pfd[0].events = POLLIN;
|
||||
retval = poll(pfd, 1,
|
||||
t.tv_sec * 1000 + t.tv_usec / 1000);
|
||||
retval = ppoll(pfd, 1, &t, NULL);
|
||||
if (retval > 0)
|
||||
c = getchar();
|
||||
else /* Don't move if timed out or error */
|
||||
@ -203,15 +200,16 @@ teleport:
|
||||
break;
|
||||
}
|
||||
if (Real_time) {
|
||||
(void)gettimeofday(&t, &tz);
|
||||
t.tv_sec = tod.tv_sec + tv.tv_sec - t.tv_sec;
|
||||
t.tv_usec = tod.tv_usec + tv.tv_usec - t.tv_usec;
|
||||
if (t.tv_usec < 0) {
|
||||
t.tv_sec--;
|
||||
t.tv_usec += 1000000; /* Now it must be > 0 */
|
||||
}
|
||||
if (t.tv_sec < 0)
|
||||
/* Update current time. */
|
||||
clock_gettime(CLOCK_MONOTONIC, &t);
|
||||
|
||||
/* Check whether tv time has passed. */
|
||||
timespecadd(&tn, &tv, &tn);
|
||||
if (timespeccmp(&tn, &t, <))
|
||||
goto ret;
|
||||
|
||||
/* Keep the difference otherwise. */
|
||||
timespecsub(&tn, &t, &t);
|
||||
}
|
||||
}
|
||||
ret:
|
||||
|
@ -1,4 +1,4 @@
|
||||
/* $OpenBSD: robots.h,v 1.8 2014/11/16 04:49:48 guenther Exp $ */
|
||||
/* $OpenBSD: robots.h,v 1.9 2015/08/26 00:29:24 rzalamena Exp $ */
|
||||
/* $NetBSD: robots.h,v 1.5 1995/04/24 12:24:54 cgd Exp $ */
|
||||
|
||||
/*
|
||||
@ -107,7 +107,7 @@ extern char Cnt_move, Field[Y_FIELDSIZE][X_FIELDSIZE], *Next_move,
|
||||
extern int Count, Level, Num_robots, Num_scores, Score,
|
||||
Start_level, Wait_bonus;
|
||||
|
||||
extern struct timeval tv;
|
||||
extern struct timespec tv;
|
||||
|
||||
extern COORD Max, Min, My_pos, Robots[];
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user