home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Atari FTP
/
ATARI_FTP_0693.zip
/
ATARI_FTP_0693
/
Mint
/
mntlib32.zoo
/
sleep.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-02-26
|
2KB
|
105 lines
/* sleep -- sleep for a specified number of seconds */
/* usleep -- sleep for a specified number of microSecond */
/* written by Eric R. Smith and placed in the public domain */
/* extensively rehacked by entropy for mint >= 0.95, still public domain */
#include <time.h>
#include <mintbind.h>
#include <signal.h>
#include <unistd.h>
/* _clock() has a rez of CLOCKS_PER_SEC ticks/sec */
#define USEC_PER_TICK (1000000L / ((unsigned long)CLOCKS_PER_SEC))
#define USEC_TO_CLOCK_TICKS(us) ((us) / USEC_PER_TICK )
static void __CDECL alarm_catch __PROTO((long signum));
static void __CDECL
alarm_catch(signum)
long signum;
{
return;
}
unsigned int
sleep(n)
unsigned int n;
{
unsigned long stop;
long old_alarm_func;
long old_sigmask;
long alarm_sec;
long remain;
extern int __mint;
if (__mint) {
if (__mint < 95) {
while (n > 32) {
(void)Fselect(32000, 0L, 0L, 0L);
n -= 32;
}
(void)Fselect(1000*n, 0L, 0L, 0L);
return 0;
}
if (n == 0)
return 0;
alarm_sec = (long) alarm(0);
old_sigmask = Psigblock(~0L);
old_alarm_func = Psignal(SIGALRM, (long)alarm_catch);
(void) Psigblock(~0L);
stop = _clock() + n * CLOCKS_PER_SEC;
if (alarm_sec && (alarm_sec < n))
(void) alarm((unsigned) alarm_sec);
else
(void) alarm(n);
Psigpause(old_sigmask);
(void) alarm(0);
(void) Psignal(SIGALRM, (long) alarm_catch);
(void) Syield();
(void) Psigblock(~0L);
remain = ((long) stop - (long) _clock())
/ (long) CLOCKS_PER_SEC;
if (alarm_sec) {
alarm_sec -= ((long) n - remain);
if (alarm_sec <= 0)
(void) Pkill(Pgetpid(), SIGALRM);
else
(void) alarm((unsigned) alarm_sec);
}
(void) Psignal(SIGALRM, (long) old_alarm_func);
(void) Psigsetmask(old_sigmask);
(void) Syield();
return ((remain > 0) ? (unsigned) remain : 0);
}
else {
stop = _clock() + n * CLOCKS_PER_SEC;
while (_clock() < stop)
;
}
return 0;
}
/*
* Sleep for usec microSeconds
* the actual suspension time can be arbitrarily longer
*
*/
void
usleep(usec)
unsigned long usec;
{
unsigned long stop;
extern int __mint;
if (__mint) {
if (usec >= 1000)
(void)Fselect((unsigned)(usec/1000), 0L, 0L, 0L);
}
else {
stop = _clock() + USEC_TO_CLOCK_TICKS(usec);
while (_clock() < stop)
;
}
}