[Bugfix][T108][bug-view-2473] sl - call ql_nw_get_voice_reg_status many times, will block

    Only Configure: No
    Affected branch: Na
    Affected module: nw
    Is it affected : only ASR
    Self-test: yes
    Doc Update: no

Change-Id: I43b988009c1ddef83c5287d465ffa715c37d3808
diff --git a/mbtk/libmbtk_lib/ril/v2/mbtk_ril_api.c b/mbtk/libmbtk_lib/ril/v2/mbtk_ril_api.c
index 7d6218e..bb36c76 100755
--- a/mbtk/libmbtk_lib/ril/v2/mbtk_ril_api.c
+++ b/mbtk/libmbtk_lib/ril/v2/mbtk_ril_api.c
@@ -136,35 +136,36 @@
 
 static void ril_thread_wait(ril_cli_thread_info_t* cli_thread)
 {
+    pthread_mutex_lock(&cli_thread->mutex);
     if(!cli_thread->is_waitting) {
         LOGD("Thread(%s) waitting...", cli_thread->name);
         cli_thread->is_waitting = TRUE;
-        pthread_mutex_lock(&cli_thread->mutex);
         pthread_cond_wait(&cli_thread->cond, &cli_thread->mutex);
-        pthread_mutex_unlock(&cli_thread->mutex);
         cli_thread->is_waitting = FALSE;
         LOGD("Thread(%s) running...", cli_thread->name);
     } else {
         LOGD("RSP befor SEND, not waitting...");
+        timer_cancel(cli_thread);
         cli_thread->is_waitting = FALSE;
     }
+    pthread_mutex_unlock(&cli_thread->mutex);
 }
 
 
 static void ril_thread_continue(ril_cli_thread_info_t* cli_thread)
 {
+
     if(cli_thread) { // Found cli thread in list.
+        pthread_mutex_lock(&cli_thread->mutex);
         if(cli_thread->is_waitting) {
             LOGD("Thread(%s) will continue...", cli_thread->name);
-            pthread_mutex_lock(&cli_thread->mutex);
             pthread_cond_signal(&cli_thread->cond);
-            pthread_mutex_unlock(&cli_thread->mutex);
-
             cli_thread->msg_index = RIL_MSG_INDEX_INVALID;
         } else {
             LOGD("Thread(%s) not continue...", cli_thread->name);
             cli_thread->is_waitting = TRUE;
         }
+        pthread_mutex_unlock(&cli_thread->mutex);
     } else {
         LOGE("No found cli thread[Cannot occur].");
     }
@@ -183,15 +184,18 @@
         }
     }
 
+
     if(cli_thread) { // Found cli thread in list.
+        pthread_mutex_lock(&ril_cli.send_mutex);
         LOGD("Timer handler : %s, msg_index - %d", cli_thread->name, cli_thread->msg_index);
 
         if(cli_thread->msg_index == RIL_MSG_INDEX_INVALID) {
             LOGE("Invalid message index.");
+            pthread_mutex_unlock(&ril_cli.send_mutex);
             return;
         }
 
-        pthread_mutex_lock(&ril_cli.send_mutex);
+
         ril_msg_info_t* msg = NULL;
         list_first(ril_cli.msg_list);
         while ((msg = (ril_msg_info_t*) list_next(ril_cli.msg_list)))
@@ -485,6 +489,7 @@
                              bool is_async,
                              int timeout)
 {
+    LOGI("s %d p %d REQ(%s). l %d  a %d t %d",sim_id,port, id2str(id),req_len,is_async,timeout);
     #ifdef LYNQ_DSDS_SUPPORT
     ril_msg_pack_info_t* pack = ril_msg_pack_creat(sim_id, port, RIL_MSG_TYPE_REQ, id, RIL_MSG_INDEX_AUTO, req, req_len);
     #else
@@ -492,12 +497,14 @@
     #endif
     if(pack == NULL)
     {
+        LOGE("pack is NULL"); 
         return MBTK_RIL_ERR_MEMORY;
     }
 
     ril_msg_info_t *msg_info = (ril_msg_info_t*)malloc(sizeof(ril_msg_info_t));
     if(msg_info == NULL)
     {
+        LOGE("msg_info is NULL");
         ril_msg_pack_free(pack);
         return MBTK_RIL_ERR_MEMORY;
     }
@@ -505,9 +512,21 @@
     int rsp_data_len = 0;
     int rsp_err = MBTK_RIL_ERR_SUCCESS;
 
-    pthread_mutex_lock(&ril_cli.send_mutex);
+
     // Add to msg list.
     msg_info->pid = pthread_self();
+    
+    ril_cli_thread_info_t* cli_thread = thread_find_by_pid(msg_info->pid);
+    
+    if(cli_thread==NULL)
+    {
+        LOGE("cli_thread is NULL");
+        ril_msg_pack_free(pack);
+        free(msg_info);
+        return MBTK_RIL_ERR_MEMORY;
+    }
+    
+    pthread_mutex_lock(&ril_cli.send_mutex);
     msg_info->pack = pack;
     msg_info->rsp_data_len = &rsp_data_len;
     msg_info->rsp_err = &rsp_err;
@@ -518,12 +537,11 @@
     }
     msg_info->is_async = is_async;
     list_add(ril_cli.msg_list, msg_info);
-    pthread_mutex_unlock(&ril_cli.send_mutex);
 
-    ril_cli_thread_info_t* cli_thread = thread_find_by_pid(msg_info->pid);
+    
     // Send pack to server success.
-    if(ril_pack_send(ril_cli.cli_fd, pack) >= RIL_SOCK_PACK_LEN_MIN) {
-        if(!is_async) { // Waitting for response for sync request.
+
+     if(!is_async) { // Waitting for response for sync request.
             if(timeout == 0) {
                 timeout = RIL_TIMEOUT_DEF;
             }
@@ -531,17 +549,30 @@
                 cli_thread->msg_index = pack->msg_index;
                 timer_start(cli_thread, timeout);
             }
-
-            ril_thread_wait(cli_thread);
+     }
+     pthread_mutex_unlock(&ril_cli.send_mutex);
+     
+     if(ril_pack_send(ril_cli.cli_fd, pack) >= RIL_SOCK_PACK_LEN_MIN)
+     {
+         if(!is_async)
+        {
+          ril_thread_wait(cli_thread);
         }
-
-        LOGD("REQ(%s) success.", id2str(id));
+        
+        LOGI("REQ(%s) success.", id2str(id));
         return rsp_err;
-    } else {
+    } else {    
+        pthread_mutex_lock(&ril_cli.send_mutex);
+        if(!is_async)
+        {
+            timer_cancel(cli_thread);
+        }
         if(list_remove(ril_cli.msg_list, msg_info)) {
             ril_msg_pack_free(msg_info->pack);
             free(msg_info);
         }
+        LOGE("REQ(%s) send fail.", id2str(id));
+        pthread_mutex_unlock(&ril_cli.send_mutex);
         return MBTK_RIL_ERR_SEND_PACK;
     }
 }