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

Change-Id: Ie0fac63ddc7b068f53c44142a71282dd974ed808
diff --git a/lib/liblynq-shm/lynq_shm.cpp b/lib/liblynq-shm/lynq_shm.cpp
index 3d27575..e06ca96 100755
--- a/lib/liblynq-shm/lynq_shm.cpp
+++ b/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 ;

 }