From c479ef57baaaa28964fc3ec8d80ff99dffa7d49f Mon Sep 17 00:00:00 2001
From: fujuntang <fujuntang@smartai.com>
Date: 星期三, 10 十一月 2021 09:49:29 +0800
Subject: [PATCH] Fix the system hang issue when the app is killed contantly.

---
 src/shm/hashtable.cpp |   56 +++++++++++++++++++++++++++++++++++++-------------------
 1 files changed, 37 insertions(+), 19 deletions(-)

diff --git a/src/shm/hashtable.cpp b/src/shm/hashtable.cpp
index 62d052e..14daaf0 100755
--- a/src/shm/hashtable.cpp
+++ b/src/shm/hashtable.cpp
@@ -2,6 +2,7 @@
 #include "hashtable.h"
 #include "mm.h"
 #include "svsem.h"
+#include "bh_api.h"
 #include "logger_factory.h"
 #include <set>
 #include <functional>
@@ -52,11 +53,17 @@
   }
   else
   {
-
     TAILQ_FOREACH(item, my_tailq_head, joint)
     {
-      if (key == item->key)
+      if ((item != NULL) && (key == item->key)) {
         return item->value;
+      } else {
+        mm_free(my_tailq_head);
+        hashtable->array[code] = NULL;
+        hashtable->queueCount--;
+
+        return NULL;
+      }
     }
   }
   return NULL;
@@ -71,7 +78,12 @@
   tailq_header_t *my_tailq_head = hashtable->array[code] ;
   if ( my_tailq_head == NULL)
   {
+    if (inter_key_get() == 0) {
+      inter_key_set(key);
+    }
+
     my_tailq_head  = (tailq_header_t*) mm_malloc(sizeof(tailq_header_t ));
+
     TAILQ_INIT(my_tailq_head);
     hashtable->array[code] = my_tailq_head;
     goto putnew;
@@ -79,27 +91,30 @@
 
   TAILQ_FOREACH(item, my_tailq_head, joint)
   {
-    if (key ==item->key)
+    if ((item != NULL) && (key == item->key))
     {
       oldvalue = item -> value;
       item->key= key;
-      item -> value = value;
+      item->value = value;
       return oldvalue;
-    }
+    } 
   }
 putnew:
+
+  if (inter_key_get() == 0) {
+    inter_key_set(key);
+  }
   item = (tailq_entry_t *) mm_malloc(sizeof(tailq_entry_t));
   item->key = key;
-  item -> value = value;
+  item->value = value;
   TAILQ_INSERT_TAIL(my_tailq_head, item, joint);
   return NULL;
 }
 
-void *hashtable_remove(hashtable_t *hashtable, int key)
+void hashtable_remove(hashtable_t *hashtable, int key)
 {
   size_t code = hashcode(key);
   tailq_entry_t *item;
-  void *oldvalue;
   int rv;
 
   if( (rv = svsem_uni_wait(hashtable->mutex)) != 0) {
@@ -111,29 +126,31 @@
     if((rv = svsem_post(hashtable->mutex)) != 0) {
       LoggerFactory::getLogger()->error(errno, "hashtable_remove\n");
     }
-    return NULL;
+    return;
   } else {
-    for (item = TAILQ_FIRST(my_tailq_head); item != NULL; item = TAILQ_NEXT(item, joint))
+    for (item = TAILQ_FIRST(my_tailq_head); item != NULL;)
     {
-      if (key == item->key)
-      {
-        oldvalue = item->value;
-        /* Remove the item from the tail queue. */
-        TAILQ_REMOVE(my_tailq_head, item, joint);
+      /* Remove the item from the tail queue. */
+      TAILQ_REMOVE(my_tailq_head, item, joint);
 
-        /* mm_free the item as we don't need it anymore. */
-        mm_free(item);
+      /* mm_free the item as we don't need it anymore. */
+      mm_free(item);
+
+      item = TAILQ_NEXT(item, joint);
+      if (item == NULL) {
+        mm_free(my_tailq_head);
+        hashtable->array[code] = NULL;
         hashtable->queueCount--;
         svsem_post(hashtable->mutex);
-        return oldvalue;
       }
+      return;
     }
 
     if((rv = svsem_post(hashtable->mutex)) != 0) {
       LoggerFactory::getLogger()->error(errno, "hashtable_remove\n");
     }
 
-    return NULL;
+    return;
   }
 }
 
@@ -217,6 +234,7 @@
     key++;
   }
   // 鍗犵敤key
+
   _hashtable_put(hashtable, key, (void *)1);
 
   hashtable->currentKey = key;

--
Gitblit v1.8.0