Skip to content
Snippets Groups Projects
Commit fa67a06a authored by Deucе's avatar Deucе :ok_hand_tone4:
Browse files

Implement xp_fast_timer64()

Like xp_timer64(), except it gets the worst clock, not the best one.
Mostly useful for places where you're using deltas between time(NULL)
to approximate a time, not for if you need to convert to a date or
if you need any kind of precision.
parent 41224008
Branches
Tags
No related merge requests found
Pipeline #7686 failed
...@@ -122,6 +122,7 @@ struct bbslist { ...@@ -122,6 +122,7 @@ struct bbslist {
short unsigned int port; short unsigned int port;
time_t added; time_t added;
time_t connected; time_t connected;
int64_t fast_connected;
unsigned int calls; unsigned int calls;
char user[MAX_USER_LEN + 1]; char user[MAX_USER_LEN + 1];
char password[MAX_PASSWD_LEN + 1]; char password[MAX_PASSWD_LEN + 1];
......
...@@ -571,16 +571,16 @@ pty_connect(struct bbslist *bbs) ...@@ -571,16 +571,16 @@ pty_connect(struct bbslist *bbs)
int int
pty_close(void) pty_close(void)
{ {
time_t start; uint64_t start;
char garbage[1024]; char garbage[1024];
int oldmaster; int oldmaster;
conn_api.terminate = true; conn_api.terminate = true;
start = time(NULL); start = xp_fast_timer64();
kill(child_pid, SIGHUP); kill(child_pid, SIGHUP);
while (waitpid(child_pid, &status, WNOHANG) == 0) { while (waitpid(child_pid, &status, WNOHANG) == 0) {
/* Wait for 10 seconds */ /* Wait for 10 seconds */
if (time(NULL) - start >= 10) if (xp_fast_timer64() - start >= 10)
break; break;
SLEEP(1); SLEEP(1);
} }
......
...@@ -116,9 +116,9 @@ modem_response(char *str, size_t maxlen, int timeout) ...@@ -116,9 +116,9 @@ modem_response(char *str, size_t maxlen, int timeout)
{ {
char ch; char ch;
size_t len = 0; size_t len = 0;
time_t start; uint64_t start;
start = time(NULL); start = xp_fast_timer64();
while (1) { while (1) {
/* Abort with keystroke */ /* Abort with keystroke */
if (kbhit()) { if (kbhit()) {
...@@ -130,7 +130,7 @@ modem_response(char *str, size_t maxlen, int timeout) ...@@ -130,7 +130,7 @@ modem_response(char *str, size_t maxlen, int timeout)
return 1; return 1;
} }
if (time(NULL) - start >= timeout) if (xp_fast_timer64() - start >= timeout)
return -1; return -1;
if (len >= maxlen) if (len >= maxlen)
return -1; return -1;
...@@ -417,7 +417,7 @@ serial_close(void) ...@@ -417,7 +417,7 @@ serial_close(void)
int int
modem_close(void) modem_close(void)
{ {
time_t start; int64_t start;
char garbage[1024]; char garbage[1024];
COM_HANDLE oldcom; COM_HANDLE oldcom;
...@@ -432,10 +432,10 @@ modem_close(void) ...@@ -432,10 +432,10 @@ modem_close(void)
if (!comLowerDTR(com)) if (!comLowerDTR(com))
goto CLOSEIT; goto CLOSEIT;
start = time(NULL); start = xp_fast_timer64();
oldcom = com; oldcom = com;
com = COM_HANDLE_INVALID; com = COM_HANDLE_INVALID;
while (time(NULL) - start <= 10) { while (xp_fast_timer64() - start <= 10) {
if ((comGetModemStatus(com) & COM_DCD) == 0) if ((comGetModemStatus(com) & COM_DCD) == 0)
goto CLOSEIT; goto CLOSEIT;
SLEEP(1000); SLEEP(1000);
......
...@@ -329,12 +329,11 @@ update_status(struct bbslist *bbs, int speed, int ooii_mode, bool ata_inv) ...@@ -329,12 +329,11 @@ update_status(struct bbslist *bbs, int speed, int ooii_mode, bool ata_inv)
int oldscroll; int oldscroll;
int olddmc = hold_update; int olddmc = hold_update;
struct text_info txtinfo; struct text_info txtinfo;
time_t now; int64_t now;
static time_t lastupd = 0; static int64_t lastupd = 0;
static int oldspeed = 0; static int oldspeed = 0;
static int lastmouse = 0; static int lastmouse = 0;
int newmouse; int newmouse;
double timeond;
int timeon; int timeon;
char sep; char sep;
int oldfont_norm; int oldfont_norm;
...@@ -344,7 +343,7 @@ update_status(struct bbslist *bbs, int speed, int ooii_mode, bool ata_inv) ...@@ -344,7 +343,7 @@ update_status(struct bbslist *bbs, int speed, int ooii_mode, bool ata_inv)
if (term.nostatus) if (term.nostatus)
return; return;
now = time(NULL); now = xp_fast_timer64();
newmouse = ((ms->mode == MM_OFF) ? 1 : 0) | (ms->flags & MS_FLAGS_DISABLED); newmouse = ((ms->mode == MM_OFF) ? 1 : 0) | (ms->flags & MS_FLAGS_DISABLED);
if (rip_did_reinit) { if (rip_did_reinit) {
rip_did_reinit = false; rip_did_reinit = false;
...@@ -378,13 +377,11 @@ update_status(struct bbslist *bbs, int speed, int ooii_mode, bool ata_inv) ...@@ -378,13 +377,11 @@ update_status(struct bbslist *bbs, int speed, int ooii_mode, bool ata_inv)
lastupd = now; lastupd = now;
lastmouse = newmouse; lastmouse = newmouse;
oldspeed = speed; oldspeed = speed;
timeond = difftime(now, bbs->connected); timeon = now - bbs->fast_connected;
if (timeond > 350000) if (timeon > 350000)
timeon = 350000; timeon = 350000;
else if (timeond < 0) else if (timeon < 0)
timeon = 0; timeon = 0;
else
timeon = timeond;
gettextinfo(&txtinfo); gettextinfo(&txtinfo);
oldscroll = _wscroll; oldscroll = _wscroll;
hold_update = true; hold_update = true;
...@@ -532,7 +529,7 @@ zmodem_check_abort(void *vp) ...@@ -532,7 +529,7 @@ zmodem_check_abort(void *vp)
struct zmodem_cbdata *zcb = (struct zmodem_cbdata *)vp; struct zmodem_cbdata *zcb = (struct zmodem_cbdata *)vp;
zmodem_t *zm = zcb->zm; zmodem_t *zm = zcb->zm;
static time_t last_check = 0; static time_t last_check = 0;
time_t now = time(NULL); int64_t now = xp_fast_timer64();
int key; int key;
if (zm == NULL) if (zm == NULL)
...@@ -1400,8 +1397,8 @@ static BOOL ...@@ -1400,8 +1397,8 @@ static BOOL
xmodem_check_abort(void *vp) xmodem_check_abort(void *vp)
{ {
xmodem_t *xm = (xmodem_t *)vp; xmodem_t *xm = (xmodem_t *)vp;
static time_t last_check = 0; static uint64_t last_check = 0;
time_t now = time(NULL); uint64_t now = xp_fast_timer64();
int key; int key;
if (xm == NULL) if (xm == NULL)
......
...@@ -1117,6 +1117,57 @@ uint64_t xp_timer64(void) ...@@ -1117,6 +1117,57 @@ uint64_t xp_timer64(void)
return(ret); return(ret);
} }
/****************************************************************************/
/* Returns the current value of the systems worst timer (in SECONDS) */
/* A value of -1 may indicate an error */
/****************************************************************************/
int64_t xp_fast_timer64(void)
{
int64_t ret;
#if defined(__unix__)
struct timespec ts;
static clockid_t cid = CLOCK_REALTIME;
#ifdef CLOCK_SECOND
if (cid == CLOCK_REALTIME) {
if (clock_getres(CLOCK_SECOND, &ts) == 0)
cid = CLOCK_SECOND;
}
#endif
#ifdef CLOCK_MONOTONIC_COARSE
if (cid == CLOCK_REALTIME) {
if (clock_getres(CLOCK_MONOTONIC_COARSE, &ts) == 0)
cid = CLOCK_MONOTONIC_COARSE;
}
#endif
#ifdef CLOCK_MONOTONIC_FAST
if (cid == CLOCK_REALTIME) {
if (clock_getres(CLOCK_MONOTONIC_FAST, &ts) == 0)
cid = CLOCK_MONOTONIC_FAST;
}
#endif
#ifdef CLOCK_MONOTONIC_RAW
if (cid == CLOCK_REALTIME) {
if (clock_getres(CLOCK_MONOTONIC_RAW, &ts) == 0)
cid = CLOCK_MONOTONIC_RAW;
}
cid = CLOCK_MONOTONIC_RAW;
#endif
if (cid == CLOCK_REALTIME)
cid = CLOCK_MONOTONIC;
if (clock_gettime(cid, &ts) == 0)
ret = ts.tv_sec * 1000;
else
ret = -1;
#elif defined(_WIN32)
ret=GetTickCount64() / 1000;
#else
#error no high-resolution time for this platform
#endif
return(ret);
}
/* Returns true if specified process is running */ /* Returns true if specified process is running */
bool check_pid(pid_t pid) bool check_pid(pid_t pid)
{ {
......
...@@ -385,6 +385,7 @@ DLLEXPORT long xp_random(int); ...@@ -385,6 +385,7 @@ DLLEXPORT long xp_random(int);
DLLEXPORT long double xp_timer(void); DLLEXPORT long double xp_timer(void);
DLLEXPORT uint64_t xp_timer64(void); DLLEXPORT uint64_t xp_timer64(void);
DLLEXPORT int64_t xp_fast_timer64(void);
DLLEXPORT char* os_version(char *str, size_t); DLLEXPORT char* os_version(char *str, size_t);
DLLEXPORT char* os_cpuarch(char *str, size_t); DLLEXPORT char* os_cpuarch(char *str, size_t);
DLLEXPORT char* os_cmdshell(void); DLLEXPORT char* os_cmdshell(void);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment