[Bugfix][API-759] after multiple module init,  then de-init, last call lynq-network-deinit, coredump 2

Change-Id: Ie0fac63ddc7b068f53c44142a71282dd974ed808
diff --git a/src/lynq/framework/lynq-ril-service/src/ril.cpp b/src/lynq/framework/lynq-ril-service/src/ril.cpp
index 634d800..d0156ca 100755
--- a/src/lynq/framework/lynq-ril-service/src/ril.cpp
+++ b/src/lynq/framework/lynq-ril-service/src/ril.cpp
@@ -6213,7 +6213,7 @@
             uint32_t* pFirstInt = (uint32_t*) const_cast<uint8_t*> (data);
                *pFirstInt=*pFirstInt+((index+1)<<SHM_BUFFER_INDEX_OFFSET)+((dataSize)<<SHM_BUFFER_SIZE_OFFSET);          
             dataSize=sizeof(int32_t)*2;
-            RLOGD("LYNQ_RIL_urcBroadcast use share mem level is %d, index is %d",level,index);
+            RLOGD("LYNQ_RIL_urcBroadcast use share mem level is %d, index is %d,pointer is %p",level,index,p_shm_buffer);
         }        
     }
     sent = sendto(lynq_urc_socket_fd, data, dataSize, 0, (struct sockaddr *)&urc_broadcast_addr, sizeof(urc_broadcast_addr));
diff --git a/src/lynq/lib/liblynq-call/lynq_module_socket.cpp b/src/lynq/lib/liblynq-call/lynq_module_socket.cpp
index a7f1ec7..ff45c02 100755
--- a/src/lynq/lib/liblynq-call/lynq_module_socket.cpp
+++ b/src/lynq/lib/liblynq-call/lynq_module_socket.cpp
@@ -320,6 +320,7 @@
     int res = 0;

     lynq_head_t* phead;

     int level,index,size;

+	uint8_t * shm_buffer;

    

     LYINFLOG("urc recv thread is running");

     while(module_urc_status)

@@ -345,7 +346,9 @@
         }      

         if(urc_data_is_in_shm_data(phead->resp_type,level,index,size))

         {

-            urc_p->setData((uint8_t *)get_shem_buffer(level,index),size); // p.setData((uint8_t *) buffer, buflen);                        

+            shm_buffer = (uint8_t *) get_shem_buffer(level,index);

+            LYINFLOG("shm pointer is %p", shm_buffer); 

+            urc_p->setData(shm_buffer,size); // p.setData((uint8_t *) buffer, buflen);                        

         }

         else if(res>=sizeof(int32_t)*3)

         {

diff --git a/src/lynq/lib/liblynq-network/lynq_module_socket.cpp b/src/lynq/lib/liblynq-network/lynq_module_socket.cpp
index b8c7c99..c3f7334 100755
--- a/src/lynq/lib/liblynq-network/lynq_module_socket.cpp
+++ b/src/lynq/lib/liblynq-network/lynq_module_socket.cpp
@@ -320,6 +320,7 @@
     int res = 0;

     lynq_head_t* phead;

     int level,index,size;

+    uint8_t * shm_buffer;

    

     LYINFLOG("urc recv thread is running");

     while(module_urc_status)

@@ -345,7 +346,9 @@
         }        

         if(urc_data_is_in_shm_data(phead->resp_type,level,index,size))

         {

-            urc_p->setData((uint8_t *)get_shem_buffer(level,index),size); // p.setData((uint8_t *) buffer, buflen);                        

+            shm_buffer = (uint8_t *) get_shem_buffer(level,index);

+            LYINFLOG("shm pointer is %p", shm_buffer); 

+            urc_p->setData(shm_buffer,size); // p.setData((uint8_t *) buffer, buflen);                        

         }

         else if(res>=sizeof(int32_t)*3)

         {

diff --git a/src/lynq/lib/liblynq-shm/lynq_shm.cpp b/src/lynq/lib/liblynq-shm/lynq_shm.cpp
index 3d27575..e06ca96 100755
--- a/src/lynq/lib/liblynq-shm/lynq_shm.cpp
+++ b/src/lynq/lib/liblynq-shm/lynq_shm.cpp
@@ -18,6 +18,8 @@
 

 int shmid=-1; 

 

+static int s_use_count=0;

+

 typedef struct{

     int num_of_buffer;

     int size_of_buffer;

@@ -142,10 +144,10 @@
 {

     if(shmid!=-1)

     {

-        if (shmctl(shmid, IPC_RMID, NULL) == -1) 

+        if (shmdt(s_ril_shm_buf) != 0) 

         {

-            RLOGE("shmctl error.\n");       

-        }

+            RLOGE("shmdt error.\n");      

+        }        

         shmid = -1;

     }

     s_ril_shm_buf = (void*) -1L;

@@ -154,16 +156,39 @@
 

 int ril_init_mem()
 {   

-    if(create_shm()!=0)

-    {
-        return -1;

+    pthread_mutex_lock(&s_shm_mtx);

+    RLOGE("init begin, use count is %d.\n",s_use_count);      

+    if(s_use_count==0)

+    {

+        if(create_shm()!=0)

+        {
+            RLOGE("init end, use count is %d.\n",s_use_count);       

+            pthread_mutex_unlock(&s_shm_mtx);

+            return -1;

+        }    

     }

+    s_use_count++;

+    RLOGE("init end, use count is %d.\n",s_use_count);       

+    pthread_mutex_unlock(&s_shm_mtx);

     return 0;

 }
 
 void ril_deinit_mem()

 {

-    remove_shm();   

+    pthread_mutex_lock(&s_shm_mtx);

+

+    RLOGE("de-init begin, use count is %d.\n",s_use_count);      

+    if(s_use_count==1)

+    {

+        remove_shm();        

+    }

+

+    if(s_use_count>0)

+    {

+        s_use_count--;    

+    }

+    RLOGE("de-init end, use count is %d.\n",s_use_count);      

+    pthread_mutex_unlock(&s_shm_mtx);

     return ;

 }