Replace g_time3() with g_get_elapsed_ms()

The function as specified used gettimeofday() which is susceptible
to manual time changes, and is obsoleted in POSIX.1-2008. The
replacement uses clock_gettime(CLOCK_MONOTONIC, ) which is not
susceptible to manual time changes (at least on Linux) and cannot run
backwards.

Also, on systems with 32-bit integers, the value returned by this
function wraps around every 49.7 days. To cope with a wraparound in
a way compliant with the C standard, this value needs to return an
unsigned integer type rather than a signed integer type.
This commit is contained in:
matt335672
2024-12-04 11:13:29 +00:00
parent 90798cdeaa
commit 1f79eb1c01
10 changed files with 69 additions and 58 deletions
+3 -3
View File
@@ -132,7 +132,7 @@ add_timeout(int msoffset, void (*callback)(void *data), void *data)
tui32 now;
LOG_DEVEL(LOG_LEVEL_DEBUG, "add_timeout:");
now = g_time3();
now = g_get_elapsed_ms();
tobj = g_new0(struct timeout_obj, 1);
tobj->mstime = now + msoffset;
tobj->callback = callback;
@@ -167,7 +167,7 @@ get_timeout(int *timeout)
tobj = g_timeout_head;
if (tobj != 0)
{
now = g_time3();
now = g_get_elapsed_ms();
while (tobj != 0)
{
LOG_DEVEL(LOG_LEVEL_DEBUG, " now %u tobj->mstime %u", now, tobj->mstime);
@@ -215,7 +215,7 @@ check_timeout(void)
while (tobj != 0)
{
count++;
now = g_time3();
now = g_get_elapsed_ms();
if (now >= tobj->mstime)
{
tobj->callback(tobj->data);