scsh-0.5/scsh/sleep1.c

39 lines
988 B
C

#include <sys/types.h>
#include <sys/time.h>
#include <unistd.h>
#include <time.h>
#include "../scheme48.h"
/* Sux because it's dependent on 32-bitness. */
#define hi8(i) (((i)>>24) & 0xff)
#define lo24(i) ((i) & 0xffffff)
#define comp8_24(hi, lo) (((hi)<<24) + (lo))
/* Sleep until time hisecs/losecs (return #t),
** or until interrupted (return #f).
**
** We make you pass in an absolute time so that if you have to loop
** making multiple tries to sleep due to interrupts, you don't get
** drift.
**
** Posix sleep() is not too well defined. This one uses select(),
** and is pretty straightforward.
*/
scheme_value sleep_until(int hisecs, int losecs)
{
time_t when = comp8_24(hisecs, losecs);
time_t now = time(0);
int delta = when - now;
if( delta > 0 ) {
fd_set r, w, e;
struct timeval tv = { delta, 0 };
FD_ZERO(&r);
FD_ZERO(&w);
FD_ZERO(&e);
if( select(0, &r, &w, &e, &tv) ) return SCHFALSE; /* Lose */
}
return SCHTRUE; /* Win */
}