提交 97989132 编写于 作者: I Ian Craggs

Protect running variable #1214

上级 29ed338a
...@@ -800,10 +800,10 @@ static thread_return_type WINAPI MQTTClient_run(void* n) ...@@ -800,10 +800,10 @@ static thread_return_type WINAPI MQTTClient_run(void* n)
long timeout = 10L; /* first time in we have a small timeout. Gets things started more quickly */ long timeout = 10L; /* first time in we have a small timeout. Gets things started more quickly */
FUNC_ENTRY; FUNC_ENTRY;
running = 1;
run_id = Thread_getid();
Thread_lock_mutex(mqttclient_mutex); Thread_lock_mutex(mqttclient_mutex);
run_id = Thread_getid();
running = 1;
while (!tostop) while (!tostop)
{ {
int rc = SOCKET_ERROR; int rc = SOCKET_ERROR;
...@@ -1183,13 +1183,26 @@ static MQTTResponse MQTTClient_connectURIVersion(MQTTClient handle, MQTTClient_c ...@@ -1183,13 +1183,26 @@ static MQTTResponse MQTTClient_connectURIVersion(MQTTClient handle, MQTTClient_c
resp.reasonCode = SOCKET_ERROR; resp.reasonCode = SOCKET_ERROR;
if (m->ma && !running) if (m->ma && !running)
{ {
int count = 0;
Thread_start(MQTTClient_run, handle); Thread_start(MQTTClient_run, handle);
if (MQTTTime_elapsed(start) >= millisecsTimeout) if (MQTTTime_elapsed(start) >= millisecsTimeout)
{ {
rc = SOCKET_ERROR; rc = SOCKET_ERROR;
goto exit; goto exit;
} }
MQTTTime_sleep(100L);
while (!running && ++count < 5)
{
Thread_unlock_mutex(mqttclient_mutex);
MQTTTime_sleep(100L);
Thread_lock_mutex(mqttclient_mutex);
}
if (!running)
{
rc = SOCKET_ERROR;
goto exit;
}
} }
Log(TRACE_MIN, -1, "Connecting to serverURI %s with MQTT version %d", serverURI, MQTTVersion); Log(TRACE_MIN, -1, "Connecting to serverURI %s with MQTT version %d", serverURI, MQTTVersion);
...@@ -2585,6 +2598,7 @@ static MQTTPacket* MQTTClient_waitfor(MQTTClient handle, int packet_type, int* r ...@@ -2585,6 +2598,7 @@ static MQTTPacket* MQTTClient_waitfor(MQTTClient handle, int packet_type, int* r
MQTTPacket* pack = NULL; MQTTPacket* pack = NULL;
MQTTClients* m = handle; MQTTClients* m = handle;
START_TIME_TYPE start = MQTTTime_start_clock(); START_TIME_TYPE start = MQTTTime_start_clock();
int is_running = 0; /* local copy of running */
FUNC_ENTRY; FUNC_ENTRY;
if (((MQTTClients*)handle) == NULL || timeout <= 0L) if (((MQTTClients*)handle) == NULL || timeout <= 0L)
...@@ -2593,7 +2607,11 @@ static MQTTPacket* MQTTClient_waitfor(MQTTClient handle, int packet_type, int* r ...@@ -2593,7 +2607,11 @@ static MQTTPacket* MQTTClient_waitfor(MQTTClient handle, int packet_type, int* r
goto exit; goto exit;
} }
if (running) Thread_lock_mutex(mqttclient_mutex);
is_running = running;
Thread_unlock_mutex(mqttclient_mutex);
if (is_running)
{ {
if (packet_type == CONNECT) if (packet_type == CONNECT)
{ {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册