3 Emulation of BSD usleep()
5 Note that this is not a complete emulation in that
6 it destroys any pre-existing setitimer(), but it's
7 good enough for programs which don't have interval
8 timers running across calls on usleep(). It does
9 save and restore an existing SIGALRM handler.
17 #include <sys/signal.h>
21 typedef void (*signalFUNC)(int);
22 #define signal(a, b) sigset(a, (signalFUNC) b)
23 #define signalFUNCreturn (signalFUNC)
26 #ifndef signalFUNCreturn
27 #define signalFUNCreturn
32 int flush_fd = -1; /* File descriptor for auto-flush */
35 volatile static int waiting;
37 static void getalrm(int i)
42 void sf_usleep(unsigned t)
44 static struct itimerval it;
47 oldsig = signalFUNCreturn signal(SIGALRM, getalrm);
49 #define FLUSHTIME 100000 /* Flush interval in microseconds */
51 while (t > FLUSHTIME) {
52 it.it_value.tv_sec = 0;
53 it.it_value.tv_usec = FLUSHTIME;
55 tcflush(flush_fd, TCIFLUSH);
57 if (setitimer(ITIMER_REAL, &it, NULL))
62 (void) signal(SIGALRM, getalrm);
67 it.it_value.tv_sec = t / 1000000;
68 it.it_value.tv_usec = t % 1000000;
70 if (setitimer(ITIMER_REAL, &it, NULL))
75 signal(SIGALRM, oldsig);