Commit 5f68068c authored by Ian Craggs's avatar Ian Craggs

Fix for #284 time change into past

parent a66faf8c
......@@ -198,7 +198,7 @@ START_TIME_TYPE MQTTAsync_start_clock(void)
START_TIME_TYPE MQTTAsync_start_clock(void)
{
static struct timespec start;
clock_gettime(CLOCK_REALTIME, &start);
clock_gettime(CLOCK_MONOTONIC, &start);
return start;
}
#else
......@@ -206,7 +206,11 @@ START_TIME_TYPE MQTTAsync_start_clock(void)
START_TIME_TYPE MQTTAsync_start_clock(void)
{
static struct timeval start;
gettimeofday(&start, NULL);
static struct timespec start_ts;
clock_gettime(CLOCK_MONOTONIC, &start_ts);
start.tv_sec = start_ts.tv_sec;
start.tv_usec = start_ts.tv_nsec / 1000;
return start;
}
#endif
......@@ -223,7 +227,7 @@ long MQTTAsync_elapsed(struct timespec start)
{
struct timespec now, res;
clock_gettime(CLOCK_REALTIME, &now);
clock_gettime(CLOCK_MONOTONIC, &now);
ntimersub(now, start, res);
return (res.tv_sec)*1000L + (res.tv_nsec)/1000000L;
}
......@@ -231,8 +235,11 @@ long MQTTAsync_elapsed(struct timespec start)
long MQTTAsync_elapsed(struct timeval start)
{
struct timeval now, res;
static struct timespec now_ts;
gettimeofday(&now, NULL);
clock_gettime(CLOCK_MONOTONIC, &now_ts);
now.tv_sec = now_ts.tv_sec;
now.tv_usec = now_ts.tv_nsec / 1000;
timersub(&now, &start, &res);
return (res.tv_sec)*1000 + (res.tv_usec)/1000;
}
......
......@@ -233,7 +233,7 @@ START_TIME_TYPE MQTTClient_start_clock(void)
START_TIME_TYPE MQTTClient_start_clock(void)
{
static struct timespec start;
clock_gettime(CLOCK_REALTIME, &start);
clock_gettime(CLOCK_MONOTONIC, &start);
return start;
}
#else
......@@ -241,7 +241,11 @@ START_TIME_TYPE MQTTClient_start_clock(void)
START_TIME_TYPE MQTTClient_start_clock(void)
{
static struct timeval start;
gettimeofday(&start, NULL);
static struct timespec start_ts;
clock_gettime(CLOCK_MONOTONIC, &start_ts);
start.tv_sec = start_ts.tv_sec;
start.tv_usec = start_ts.tv_nsec / 1000;
return start;
}
#endif
......@@ -258,7 +262,7 @@ long MQTTClient_elapsed(struct timespec start)
{
struct timespec now, res;
clock_gettime(CLOCK_REALTIME, &now);
clock_gettime(CLOCK_MONOTONIC, &now);
ntimersub(now, start, res);
return (res.tv_sec)*1000L + (res.tv_nsec)/1000000L;
}
......@@ -266,8 +270,11 @@ long MQTTClient_elapsed(struct timespec start)
long MQTTClient_elapsed(struct timeval start)
{
struct timeval now, res;
static struct timespec now_ts;
gettimeofday(&now, NULL);
clock_gettime(CLOCK_MONOTONIC, &now_ts);
now.tv_sec = now_ts.tv_sec;
now.tv_usec = now_ts.tv_nsec / 1000;
timersub(&now, &start, &res);
return (res.tv_sec)*1000 + (res.tv_usec)/1000;
}
......
/*******************************************************************************
* Copyright (c) 2009, 2017 IBM Corp.
* Copyright (c) 2009, 2018 IBM Corp.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
......@@ -16,6 +16,7 @@
* Ian Craggs - bug #415042 - start Linux thread as disconnected
* Ian Craggs - fix for bug #420851
* Ian Craggs - change MacOS semaphore implementation
* Ian Craggs - fix for clock #284
*******************************************************************************/
/**
......@@ -247,6 +248,9 @@ int Thread_wait_sem(sem_type sem, int timeout)
usleep(interval); /* microseconds - .1 of a second */
}
#else
/* We have to use CLOCK_REALTIME rather than MONOTONIC for sem_timedwait interval.
* Does this make it susceptible to system clock changes?
*/
if (clock_gettime(CLOCK_REALTIME, &ts) != -1)
{
ts.tv_sec += timeout;
......@@ -333,11 +337,17 @@ int Thread_destroy_sem(sem_type sem)
cond_type Thread_create_cond(void)
{
cond_type condvar = NULL;
pthread_condattr_t attr;
int rc = 0;
FUNC_ENTRY;
pthread_condattr_init(&attr);
#if !defined(OSX)
pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
#endif
condvar = malloc(sizeof(cond_type_struct));
rc = pthread_cond_init(&condvar->cond, NULL);
rc = pthread_cond_init(&condvar->cond, &attr);
rc = pthread_mutex_init(&condvar->mutex, NULL);
FUNC_EXIT_RC(rc);
......@@ -368,13 +378,14 @@ int Thread_wait_cond(cond_type condvar, int timeout)
FUNC_ENTRY;
int rc = 0;
struct timespec cond_timeout;
struct timeval cur_time;
gettimeofday(&cur_time, NULL);
cond_timeout.tv_sec = cur_time.tv_sec + timeout;
cond_timeout.tv_nsec = cur_time.tv_usec * 1000;
#if defined(OSX)
clock_gettime(CLOCK_REALTIME, &cond_timeout);
#else
clock_gettime(CLOCK_MONOTONIC, &cond_timeout);
#endif
cond_timeout.tv_sec += timeout;
pthread_mutex_lock(&condvar->mutex);
rc = pthread_cond_timedwait(&condvar->cond, &condvar->mutex, &cond_timeout);
pthread_mutex_unlock(&condvar->mutex);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment