home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Usenet 1994 January
/
usenetsourcesnewsgroupsinfomagicjanuary1994.iso
/
sources
/
x
/
volume6
/
xplumb
/
part01
/
timer.C
< prev
Wrap
C/C++ Source or Header
|
1990-04-11
|
3KB
|
141 lines
/*
* Copyright 1990 Digital Equipment Corporation
*
* Permission to use, copy, modify, and distribute this software and its
* documentation for any purpose and without fee is hereby granted,
* provided that the above copyright notice appear in all copies and that
* both that copyright notice and this permission notice appear in
* supporting documentation, and that the name of Digital Equipment
* Corporation not be used in advertising or publicity pertaining to
* distribution of the software without specific, written prior
* permission. Digital Equipment Corporation makes no representations
* about the suitability of this software for any purpose. It is
* provided "as is" without express or implied warranty.
*
* DIGITAL EQUIPMENT CORPORATION DISCLAIMS ALL WARRANTIES WITH REGARD TO
* THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS, IN NO EVENT SHALL DIGITAL EQUIPMENT CORPORATION BE LIABLE FOR
* ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
* OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* Author: Terry Weissman
* weissman@wsl.dec.com
*/
#include <sys/time.h>
#include <sys/types.h>
#include <sys/timeb.h>
extern void ftime(struct timeb *);
extern int select(int, int *, int *, int *, struct timeval *);
#ifndef NULL
#define NULL 0
#endif NULL
static int starttime;
static int paused = 0;
static int pausetime;
struct InfoRec {
int when;
void (*func)(void *);
void *closure;
struct InfoRec *next;
};
typedef struct InfoRec *Info;
static Info first = NULL;
static inline int GetCurrentTime() {
struct timeb t;
ftime(&t);
return ((t.time - starttime) * 1000 + t.millitm);
}
void TimerAddTimeout(int msec, void (*func)(void *), void *closure) {
Info temp = new InfoRec();
temp->when = msec + GetCurrentTime();
temp->func = func;
temp->closure = closure;
Info *t;
t = &first;
while (*t && (*t)->when <= temp->when) t = &((*t)->next);
temp->next = *t;
*t = temp;
}
struct timeval *TimerGetInterval() {
if (!first || paused) return NULL;
static struct timeval result;
int msec = first->when - GetCurrentTime();
if (msec < 0) msec = 0;
result.tv_sec = msec / 1000;
result.tv_usec = (msec % 1000) * 1000;
return &result;
}
void TimerHandleTimeout() {
Info temp;
if (first) {
temp = first;
first = first->next;
(*temp->func)(temp->closure);
delete temp;
}
}
void TimerClearAll() {
Info temp;
while (first) {
temp = first;
first = first->next;
delete temp;
}
}
void TimerPause() {
if (!paused) {
paused = 1;
pausetime = GetCurrentTime();
}
}
void TimerContinue() {
paused = 0;
pausetime -= GetCurrentTime();
Info temp;
for (temp = first ; temp ; temp = temp->next) {
temp->when += pausetime;
}
}
void TimerSleep(int msec) {
struct timeval t;
t.tv_sec = msec / 1000;
t.tv_usec = (msec % 1000) * 1000;
(void) select(1, NULL, NULL, NULL, &t);
}
void TimerInit() {
struct timeb t;
ftime(&t);
starttime = t.time;
}