mirror of https://github.com/eclipse/paho.mqtt.c
Avoid slow connect in MQTTAsync #1039
This commit is contained in:
parent
b7455f3330
commit
b35a2dc237
|
|
@ -1753,6 +1753,8 @@ exit:
|
|||
|
||||
thread_return_type WINAPI MQTTAsync_sendThread(void* n)
|
||||
{
|
||||
int timeout = 10; /* first time in we have a small timeout. Gets things started more quickly */
|
||||
|
||||
FUNC_ENTRY;
|
||||
MQTTAsync_lock_mutex(mqttasync_mutex);
|
||||
sendThread_state = RUNNING;
|
||||
|
|
@ -1775,13 +1777,13 @@ thread_return_type WINAPI MQTTAsync_sendThread(void* n)
|
|||
MQTTAsync_unlock_mutex(mqttcommand_mutex);
|
||||
}
|
||||
#if !defined(_WIN32) && !defined(_WIN64)
|
||||
if ((rc = Thread_wait_cond(send_cond, 1)) != 0 && rc != ETIMEDOUT)
|
||||
if ((rc = Thread_wait_cond(send_cond, timeout)) != 0 && rc != ETIMEDOUT)
|
||||
Log(LOG_ERROR, -1, "Error %d waiting for condition variable", rc);
|
||||
#else
|
||||
if ((rc = Thread_wait_sem(send_sem, 1000)) != 0 && rc != ETIMEDOUT)
|
||||
if ((rc = Thread_wait_sem(send_sem, timeout)) != 0 && rc != ETIMEDOUT)
|
||||
Log(LOG_ERROR, -1, "Error %d waiting for semaphore", rc);
|
||||
#endif
|
||||
|
||||
timeout = 1000; /* 1 second for follow on waits */
|
||||
MQTTAsync_checkTimeouts();
|
||||
}
|
||||
sendThread_state = STOPPING;
|
||||
|
|
|
|||
22
src/Thread.c
22
src/Thread.c
|
|
@ -403,25 +403,37 @@ int Thread_signal_cond(cond_type condvar)
|
|||
}
|
||||
|
||||
/**
|
||||
* Wait with a timeout (seconds) for condition variable
|
||||
* Wait with a timeout (ms) for condition variable
|
||||
* @return 0 for success, ETIMEDOUT otherwise
|
||||
*/
|
||||
int Thread_wait_cond(cond_type condvar, int timeout)
|
||||
int Thread_wait_cond(cond_type condvar, int timeout_ms)
|
||||
{
|
||||
int rc = 0;
|
||||
struct timespec cond_timeout;
|
||||
struct timespec interval;
|
||||
|
||||
FUNC_ENTRY;
|
||||
interval.tv_sec = timeout_ms / 1000;
|
||||
interval.tv_nsec = (timeout_ms % 1000) * 1000000L;
|
||||
|
||||
#if defined(__APPLE__) && __MAC_OS_X_VERSION_MIN_REQUIRED < 101200 /* for older versions of MacOS */
|
||||
struct timeval cur_time;
|
||||
gettimeofday(&cur_time, NULL);
|
||||
cond_timeout.tv_sec = cur_time.tv_sec + timeout;
|
||||
cond_timeout.tv_sec = cur_time.tv_sec;
|
||||
cond_timeout.tv_nsec = cur_time.tv_usec * 1000;
|
||||
#else
|
||||
clock_gettime(CLOCK_REALTIME, &cond_timeout);
|
||||
|
||||
cond_timeout.tv_sec += timeout;
|
||||
#endif
|
||||
|
||||
cond_timeout.tv_sec += interval.tv_sec;
|
||||
cond_timeout.tv_nsec += (timeout_ms % 1000) * 1000000L;
|
||||
|
||||
if (cond_timeout.tv_nsec >= 1000000000L)
|
||||
{
|
||||
cond_timeout.tv_sec++;
|
||||
cond_timeout.tv_nsec += (cond_timeout.tv_nsec - 1000000000L);
|
||||
}
|
||||
|
||||
pthread_mutex_lock(&condvar->mutex);
|
||||
rc = pthread_cond_timedwait(&condvar->cond, &condvar->mutex, &cond_timeout);
|
||||
pthread_mutex_unlock(&condvar->mutex);
|
||||
|
|
|
|||
Loading…
Reference in New Issue