diff options
author | Rafael Zalamena <rzalamena@cvs.openbsd.org> | 2015-08-26 00:29:25 +0000 |
---|---|---|
committer | Rafael Zalamena <rzalamena@cvs.openbsd.org> | 2015-08-26 00:29:25 +0000 |
commit | ea7b4737a2274a814a2c9b78bfabad346fda0989 (patch) | |
tree | 02548c337271c65901f9e8027b043352a5e36f12 /games | |
parent | f7129fe43ed48fea7a5ea9d61492418183429676 (diff) |
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@.
Diffstat (limited to 'games')
-rw-r--r-- | games/robots/extern.c | 4 | ||||
-rw-r--r-- | games/robots/main.c | 3 | ||||
-rw-r--r-- | games/robots/move.c | 30 | ||||
-rw-r--r-- | games/robots/robots.h | 4 |
4 files changed, 19 insertions, 22 deletions
diff --git a/games/robots/extern.c b/games/robots/extern.c index 4ae1ea916d9..04bc42a3362 100644 --- a/games/robots/extern.c +++ b/games/robots/extern.c @@ -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 */ diff --git a/games/robots/main.c b/games/robots/main.c index 3f7cf413cdd..7ba98c7cf5b 100644 --- a/games/robots/main.c +++ b/games/robots/main.c @@ -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; diff --git a/games/robots/move.c b/games/robots/move.c index 558fbfb3cf2..fa46520d478 100644 --- a/games/robots/move.c +++ b/games/robots/move.c @@ -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: diff --git a/games/robots/robots.h b/games/robots/robots.h index 67bebb8edcb..cefce8ad2b5 100644 --- a/games/robots/robots.h +++ b/games/robots/robots.h @@ -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[]; |