فهرست منبع

修改 platform_thread.c 和mqttclient.c,确保线程启动前完成初始化

lwk 5 روز پیش
والد
کامیت
ed5f5fa0a0
3فایلهای تغییر یافته به همراه28 افزوده شده و 8 حذف شده
  1. 6 3
      mqttclient/mqttclient.c
  2. 1 0
      mqttclient/mqttclient.h
  3. 21 5
      platform/linux/platform_thread.c

+ 6 - 3
mqttclient/mqttclient.c

@@ -933,11 +933,14 @@ static void mqtt_yield_thread(void *arg)
     client_state_t state;
     mqtt_client_t *c = (mqtt_client_t *)arg;
     platform_thread_t *thread_to_be_destoried = NULL;
-    
+
     state = mqtt_get_client_state(c);
-        if (CLIENT_STATE_CONNECTED !=  state) {
-            MQTT_LOG_W("%s:%d %s()..., mqtt is not connected to the server...",  __FILE__, __LINE__, __FUNCTION__);
+    if (CLIENT_STATE_CONNECTED != state) {
+        MQTT_LOG_W("%s:%d %s()..., mqtt is not connected to the server...",  __FILE__, __LINE__, __FUNCTION__);
+        // 修复竞态条件:添加 NULL 检查,防止在 c->mqtt_thread 赋值之前访问
+        if (NULL != c->mqtt_thread) {
             platform_thread_stop(c->mqtt_thread);    /* mqtt is not connected to the server, stop thread */
+        }
     }
 
     while (1) {

+ 1 - 0
mqttclient/mqttclient.h

@@ -126,6 +126,7 @@ typedef struct mqtt_client {
     platform_timer_t            mqtt_last_received;
     reconnect_handler_t         mqtt_reconnect_handler;
     interceptor_handler_t       mqtt_interceptor_handler;
+    void*                       mqtt_conn_context;
 } mqtt_client_t;
 
 

+ 21 - 5
platform/linux/platform_thread.c

@@ -21,15 +21,23 @@ platform_thread_t *platform_thread_init( const char *name,
 
     thread_entry = (void *(*)(void*))entry;
     thread = platform_memory_alloc(sizeof(platform_thread_t));
-    
-    res = pthread_create(&thread->thread, NULL, thread_entry, param);
-    if(res != 0) {
-        platform_memory_free(thread);
+
+    if (NULL == thread) {
+        return NULL;
     }
 
+    // 修复竞态条件:在创建线程之前先初始化 mutex 和 cond
+    // 否则新线程可能在这些成员初始化之前就开始访问它们
     thread->mutex = (pthread_mutex_t)PTHREAD_MUTEX_INITIALIZER;
     thread->cond = (pthread_cond_t)PTHREAD_COND_INITIALIZER;
 
+    // 现在才创建线程,此时 mutex 和 cond 已经初始化完成
+    res = pthread_create(&thread->thread, NULL, thread_entry, param);
+    if(res != 0) {
+        platform_memory_free(thread);
+        return NULL;
+    }
+
     return thread;
 }
 
@@ -40,6 +48,10 @@ void platform_thread_startup(platform_thread_t* thread)
 
 void platform_thread_stop(platform_thread_t* thread)
 {
+    // 添加 NULL 检查,防止空指针解引用
+    if (NULL == thread) {
+        return;
+    }
     pthread_mutex_lock(&(thread->mutex));
     pthread_cond_wait(&(thread->cond), &(thread->mutex));
     pthread_mutex_unlock(&(thread->mutex));
@@ -47,7 +59,11 @@ void platform_thread_stop(platform_thread_t* thread)
 
 void platform_thread_start(platform_thread_t* thread)
 {
-    pthread_mutex_lock(&(thread->mutex)); 
+    // 添加 NULL 检查,防止空指针解引用
+    if (NULL == thread) {
+        return;
+    }
+    pthread_mutex_lock(&(thread->mutex));
     pthread_cond_signal(&(thread->cond));
     pthread_mutex_unlock(&(thread->mutex));
 }