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 {
short unsigned int port;
time_t added;
time_t connected;
int64_t fast_connected;
unsigned int calls;
char user[MAX_USER_LEN + 1];
char password[MAX_PASSWD_LEN + 1];
......
......@@ -571,16 +571,16 @@ pty_connect(struct bbslist *bbs)
int
pty_close(void)
{
time_t start;
uint64_t start;
char garbage[1024];
int oldmaster;
conn_api.terminate = true;
start = time(NULL);
start = xp_fast_timer64();
kill(child_pid, SIGHUP);
while (waitpid(child_pid, &status, WNOHANG) == 0) {
/* Wait for 10 seconds */
if (time(NULL) - start >= 10)
if (xp_fast_timer64() - start >= 10)
break;
SLEEP(1);
}
......
......@@ -116,9 +116,9 @@ modem_response(char *str, size_t maxlen, int timeout)
{
char ch;
size_t len = 0;
time_t start;
uint64_t start;
start = time(NULL);
start = xp_fast_timer64();
while (1) {
/* Abort with keystroke */
if (kbhit()) {
......@@ -130,7 +130,7 @@ modem_response(char *str, size_t maxlen, int timeout)
return 1;
}
if (time(NULL) - start >= timeout)
if (xp_fast_timer64() - start >= timeout)
return -1;
if (len >= maxlen)
return -1;
......@@ -417,7 +417,7 @@ serial_close(void)
int
modem_close(void)
{
time_t start;
int64_t start;
char garbage[1024];
COM_HANDLE oldcom;
......@@ -432,10 +432,10 @@ modem_close(void)
if (!comLowerDTR(com))
goto CLOSEIT;
start = time(NULL);
start = xp_fast_timer64();
oldcom = com;
com = COM_HANDLE_INVALID;
while (time(NULL) - start <= 10) {
while (xp_fast_timer64() - start <= 10) {
if ((comGetModemStatus(com) & COM_DCD) == 0)
goto CLOSEIT;
SLEEP(1000);
......
......@@ -329,12 +329,11 @@ update_status(struct bbslist *bbs, int speed, int ooii_mode, bool ata_inv)
int oldscroll;
int olddmc = hold_update;
struct text_info txtinfo;
time_t now;
static time_t lastupd = 0;
int64_t now;
static int64_t lastupd = 0;
static int oldspeed = 0;
static int lastmouse = 0;
int newmouse;
double timeond;
int timeon;
char sep;
int oldfont_norm;
......@@ -344,7 +343,7 @@ update_status(struct bbslist *bbs, int speed, int ooii_mode, bool ata_inv)
if (term.nostatus)
return;
now = time(NULL);
now = xp_fast_timer64();
newmouse = ((ms->mode == MM_OFF) ? 1 : 0) | (ms->flags & MS_FLAGS_DISABLED);
if (rip_did_reinit) {
rip_did_reinit = false;
......@@ -378,13 +377,11 @@ update_status(struct bbslist *bbs, int speed, int ooii_mode, bool ata_inv)
lastupd = now;
lastmouse = newmouse;
oldspeed = speed;
timeond = difftime(now, bbs->connected);
if (timeond > 350000)
timeon = now - bbs->fast_connected;
if (timeon > 350000)
timeon = 350000;
else if (timeond < 0)
else if (timeon < 0)
timeon = 0;
else
timeon = timeond;
gettextinfo(&txtinfo);
oldscroll = _wscroll;
hold_update = true;
......@@ -532,7 +529,7 @@ zmodem_check_abort(void *vp)
struct zmodem_cbdata *zcb = (struct zmodem_cbdata *)vp;
zmodem_t *zm = zcb->zm;
static time_t last_check = 0;
time_t now = time(NULL);
int64_t now = xp_fast_timer64();
int key;
if (zm == NULL)
......@@ -1400,8 +1397,8 @@ static BOOL
xmodem_check_abort(void *vp)
{
xmodem_t *xm = (xmodem_t *)vp;
static time_t last_check = 0;
time_t now = time(NULL);
static uint64_t last_check = 0;
uint64_t now = xp_fast_timer64();
int key;
if (xm == NULL)
......
......@@ -1117,6 +1117,57 @@ uint64_t xp_timer64(void)
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 */
bool check_pid(pid_t pid)
{
......
......@@ -385,6 +385,7 @@ DLLEXPORT long xp_random(int);
DLLEXPORT long double xp_timer(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_cpuarch(char *str, size_t);
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