9base

revived minimalist port of Plan 9 userland to Unix
git clone git://git.suckless.org/9base
Log | Files | Refs | README | LICENSE

sleep.c (889B)


      1 #include <u.h>
      2 #define NOPLAN9DEFINES
      3 #include <sys/param.h>
      4 #include <sys/time.h>
      5 #include <sched.h>
      6 #include <libc.h>
      7 
      8 #if defined(__NetBSD__) || (defined(__OpenBSD__) && OpenBSD <= 200611)
      9 #if !defined(sched_yield)
     10 #	define sched_yield() \
     11 		do{ struct timespec ts; \
     12 			ts.tv_sec = 0; \
     13 			ts.tv_nsec = 0; \
     14 			nanosleep(&ts, 0); \
     15 		}while(0)
     16 #endif
     17 #endif
     18 
     19 int
     20 p9sleep(long milli)
     21 {
     22 	struct timeval tv;
     23 
     24 	if(milli == 0){
     25 		sched_yield();
     26 		return 0;
     27 	}
     28 
     29 	tv.tv_sec = milli/1000;
     30 	tv.tv_usec = (milli%1000)*1000;
     31 	return select(0, 0, 0, 0, &tv);
     32 }
     33 
     34 long
     35 p9alarm(ulong milli)
     36 {
     37 	struct itimerval itv;
     38 	struct itimerval oitv;
     39 
     40 	itv.it_interval.tv_sec = 0;
     41 	itv.it_interval.tv_usec = 0;
     42 	itv.it_value.tv_sec = milli/1000;
     43 	itv.it_value.tv_usec = (milli%1000)*1000;
     44 	if(setitimer(ITIMER_REAL, &itv, &oitv) < 0)
     45 		return -1;
     46 	return oitv.it_value.tv_sec*1000+oitv.it_value.tv_usec/1000;
     47 }