📄 ssleep.c
字号:
static void WinGiveUpTimeSlice(void)
{
union REGS inregs, outregs;
static const int irq = MULTIPLEX;
inregs.x.ax = 0x1680;
int86(irq, &inregs, &outregs);
if (outregs.h.al != 0) {
printmsg(0,"Problem giving up timeslice: %u\n", outregs.h.al);
panic();
}
} /* WinGiveUpTimeSlice */
/*--------------------------------------------------------------------*/
/* R u n n i n g U n d e r D e s q v i e w */
/* */
/* Returns TRUE if running under that OTHER DOS multi-tasker. */
/*--------------------------------------------------------------------*/
static int RunningUnderDesqview(void)
{
static int result = 2;
union REGS inregs, outregs;
if (result != 2) /* First call? */
return result; /* No --> Return saved result */
inregs.x.ax = 0x2B01; /* Dos Set Date function */
inregs.x.cx = 0x4445; /* CX DX = 'DESQ' */
inregs.x.dx = 0x5351;
intdos(&inregs, &outregs);
if (outregs.h.al == 0xff) {
result = 0;
} else {
printmsg(4, "RunningUnderDesqview: Running under DesqView (AX=0x%x)",
(int) outregs.x.ax);
result = 1;
}
return result;
} /* RunningUnderDesqview */
/*--------------------------------------------------------------------*/
/* D V G i v e U p T i m e S l i c e */
/* */
/* Surrender the CPU under DesqView */
/*--------------------------------------------------------------------*/
static void DVGiveUpTimeSlice(void)
{
#ifdef __TURBOC__
asm {
#else
_asm \
{
#endif
push ax
mov ax, 101AH
/* Switch over to Desqview's stack */
int 15H
mov ax, 1000H
/* Give up the timeslice */
int 15H
mov ax, 1025H
/* Switch stack back to application */
int 15H
pop ax
}
} /* DVGiveUpTimeSlice */
#endif /* _Windows */
#endif /* WIN32 */
#endif
/*--------------------------------------------------------------------*/
/* ssleep() - wait n seconds */
/* */
/* Simply delay until n seconds have passed. */
/*--------------------------------------------------------------------*/
void ssleep(time_t interval)
{
time_t start = time((time_t *)NULL);
time_t left = interval;
/*--------------------------------------------------------------------*/
/* Break the spin into chunks ddelay can handle */
/*--------------------------------------------------------------------*/
while ( (left*1000L) > (long) INT_MAX )
{
ddelay( 5000 ); /* Five seconds per pass */
left = max(interval - (time( NULL ) - start),0);
} /* while */
/*--------------------------------------------------------------------*/
/* Final delay for the time remaining */
/*--------------------------------------------------------------------*/
ddelay( (int) (left * 1000L) );
} /*ssleep*/
/*--------------------------------------------------------------------*/
/* d d e l a y */
/* */
/* Delay for an interval of milliseconds */
/*--------------------------------------------------------------------*/
void ddelay (KEWSHORT interval )
{
#if defined(FAMILYAPI) || defined(__OS2__)
USHORT result;
#elif !defined(_Windows)
struct timeb t;
time_t seconds;
unsigned last;
time_t milliseconds = interval;
#endif
#ifndef _Windows
/*--------------------------------------------------------------------*/
/* Check for user aborts via the ESC (escape) key */
/*--------------------------------------------------------------------*/
if (bflag[F_ESCAPE]) /* Special Ctrl-C processing avail? */
{
boolean beep = TRUE;
while (safepeek()) /* Yes --> While character in buffer */
{
if (safein() == '\033') /* Look for ESC */
raise( SIGINT ); /* Yes --> eject via std exit */
else if ( beep )
{
putchar('\a'); /* No --> Complain to user */
beep = FALSE; /* But be nice about it ...
only once per pass through here */
} /* else if ( beep ) */
} /* while */
} /* if (bflag[F_ESCAPE]) */
#endif /* _Windows */
/*--------------------------------------------------------------------*/
/* Now do the wait */
/*--------------------------------------------------------------------*/
/*--------------------------------------------------------------------*/
/* Windows/NT wait */
/*--------------------------------------------------------------------*/
#ifdef WIN32
Sleep(interval);
/*--------------------------------------------------------------------*/
/* Windows wait */
/*--------------------------------------------------------------------*/
#elif defined(_Windows)
WindowsDelay(interval);
/*--------------------------------------------------------------------*/
/* OS/2 wait */
/*--------------------------------------------------------------------*/
#elif defined(FAMILYAPI) || defined(__OS2__)
result = DosSleep(interval);
if (result)
printOS2error( "DosSleep", result );
#else
/*--------------------------------------------------------------------*/
/* MS-DOS wait */
/*--------------------------------------------------------------------*/
#ifdef __TURBOC__
enable();
#else
_enable();
#endif
/*--------------------------------------------------------------------*/
/* Handle the special case of 0 delay, which is simply a */
/* request to give up our timeslice */
/*--------------------------------------------------------------------*/
if (milliseconds == 0) /* Make it compatible with DosSleep */
{
if (RunningUnderWindows())
WinGiveUpTimeSlice( );
else if (RunningUnderDesqview())
DVGiveUpTimeSlice();
return;
} /* if */
ftime(&t); /* Get a starting time */
last = t.millitm; /* Save milliseconds portion */
seconds = t.time; /* Save seconds as well */
while( milliseconds > 0) /* Begin the spin loop */
{
if (RunningUnderWindows())
WinGiveUpTimeSlice();
else if (RunningUnderDesqview())
DVGiveUpTimeSlice();
else {
#ifdef __TURBOC__
delay( (short) milliseconds );
#else
int volatile count; /* Don't let compiler optimize this */
for ( count = 0; count < 2400; count ++);
/* We spin so that interrupts are
enabled for most of the loop */
#endif
} /* else */
ftime(&t); /* Take a new time check */
if (t.time == seconds) /* Same second as last pass? */
milliseconds -= (t.millitm - last); /* Yes --> mSecond delta*/
else
milliseconds -= 1000 * (int) (t.time - seconds)
- (last - t.millitm);
/* No --> Handle wrap of mSeconds */
last = t.millitm; /* Update last tick indicator */
seconds = t.time; /* Update this as well; only needed if
it changed (see above), but it
kills time (which is our job) */
} /* while */
#endif /* FAMILYAPI */
} /* ddelay */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -