1 /* nap.c Larn is copyrighted 1986 by Noah Morgan. */
2 /* $FreeBSD: src/games/larn/nap.c,v 1.4 1999/11/16 02:57:23 billf Exp $ */
3 /* $DragonFly: src/games/larn/nap.c,v 1.2 2003/06/17 04:25:24 dillon Exp $ */
10 #include <sys/timeb.h>
15 * routine to take a nap for n milliseconds
20 if (x<=0) return; /* eliminate chance for infinite loop */
23 if (x > 999) sleep(x/1000); else napms(x);
30 napms(x) /* do nothing */
36 /* napms - sleep for time milliseconds - uses times() */
37 /* this assumes that times returns a relative time in 60ths of a second */
38 /* this will do horrible things if your times() returns seconds! */
42 long matchclock, times();
45 if (time<=0) time=1; /* eliminate chance for infinite loop */
46 if ((matchclock = times(&stats)) == -1 || matchclock == 0)
47 return; /* error, or BSD style times() */
48 matchclock += (time / 17); /*17 ms/tic is 1000 ms/sec / 60 tics/sec */
50 while(matchclock < times(&stats))
57 /* This must be BSD 4.2! */
59 #define bit(_a) (1<<((_a)-1))
65 /* napms - sleep for time milliseconds - uses setitimer() */
69 struct itimerval timeout;
73 if (time <= 0) return;
75 timerclear(&timeout.it_interval);
76 timeout.it_value.tv_sec = time / 1000;
77 timeout.it_value.tv_usec = (time % 1000) * 1000;
79 oldsig = sigblock(bit(SIGALRM));
80 setitimer(ITIMER_REAL, &timeout, (struct itimerval *)0);
81 oldhandler = signal(SIGALRM, nullf);
83 signal(SIGALRM, oldhandler);
88 /* napms - sleep for time milliseconds - uses ftime() */
93 /* assumed to be BSD UNIX */
96 unsigned short matchmilli;
97 struct timeb *tp = & _gtime;
99 if (time <= 0) return;
101 matchmilli = tp->millitm + time;
102 matchtime = tp->time;
103 while (matchmilli >= 1000)
112 if ((tp->time > matchtime) ||
113 ((tp->time == matchtime) && (tp->millitm >= matchmilli)))
119 static napms(time) int time; {} /* do nothing, forget it */