From 973692652774a5ffe98478ee287b40af529d0b39 Mon Sep 17 00:00:00 2001
From: wangzhengquan <wangzhengquan85@126.com>
Date: 星期三, 13 一月 2021 14:14:58 +0800
Subject: [PATCH] update

---
 src/shm/hashtable.cpp |  204 ++++++++++++++++++++++++++++++++++++--------------
 1 files changed, 146 insertions(+), 58 deletions(-)

diff --git a/src/shm/hashtable.cpp b/src/shm/hashtable.cpp
index 8455040..5ee06ba 100755
--- a/src/shm/hashtable.cpp
+++ b/src/shm/hashtable.cpp
@@ -2,6 +2,7 @@
 #include "hashtable.h"
 #include "mm.h"
 #include "sem_util.h"
+#include "logger_factory.h"
 #include <set>
 #include <functional>
 
@@ -27,7 +28,7 @@
 
 void hashtable_init(hashtable_t *hashtable )
 {
-  
+
   memset(hashtable, 0, sizeof(hashtable_t));
   hashtable->mutex = SemUtil::get(IPC_PRIVATE, 1);
   hashtable->wlock = SemUtil::get(IPC_PRIVATE, 1);
@@ -102,8 +103,11 @@
   size_t code = hashcode(key);
   tailq_entry_t *item;
   void *oldvalue;
+  int rv;
 
-  SemUtil::dec(hashtable->wlock);
+  if( (rv = SemUtil::dec(hashtable->wlock)) != 0) {
+    LoggerFactory::getLogger()->error(rv, "hashtable_remove\n");
+  }
   tailq_header_t *my_tailq_head = hashtable->array[code] ;
   if ( my_tailq_head == NULL)
   {
@@ -127,7 +131,10 @@
       }
     }
   }
-  SemUtil::inc(hashtable->wlock);
+  if((rv = SemUtil::inc(hashtable->wlock)) != 0) {
+    LoggerFactory::getLogger()->error(rv, "hashtable_remove\n");
+  }
+
   return NULL;
 
 }
@@ -139,7 +146,11 @@
 void hashtable_removeall(hashtable_t *hashtable)
 {
   tailq_entry_t *item;
-  SemUtil::dec(hashtable->wlock);
+  int rv;
+  rv = SemUtil::dec(hashtable->wlock);
+  if(rv != 0) {
+    LoggerFactory::getLogger()->error(rv, "hashtable_removeall\n");
+  }
   for (int i = 0; i < MAPSIZE; i++)
   {
     tailq_header_t *my_tailq_head = hashtable->array[i] ;
@@ -155,7 +166,10 @@
     mm_free(my_tailq_head);
     hashtable->array[i] = NULL;
   }
-  SemUtil::inc(hashtable->wlock);
+  rv = SemUtil::inc(hashtable->wlock);
+  if(rv != 0) {
+    LoggerFactory::getLogger()->error(rv, "hashtable_removeall\n");
+  }
 }
 
 /**
@@ -182,64 +196,110 @@
 
 static size_t hashcode(int key)
 {
-  
+
   return key % MAPSIZE;
   /*printf("hashfun = %ld\n", code);*/
 }
 
 void *hashtable_get(hashtable_t *hashtable, int key) {
-  
-   SemUtil::dec(hashtable->mutex);
-   hashtable->readcnt++;
-   if (hashtable->readcnt == 1) {
+
+  int rv;
+  rv = SemUtil::dec(hashtable->mutex);
+  if(rv != 0) {
+    LoggerFactory::getLogger()->error(rv, "hashtable_get\n");
+  }
+  hashtable->readcnt++;
+  if (hashtable->readcnt == 1) {
     //鑾峰彇璇诲啓閿�
-    SemUtil::dec(hashtable->wlock);
-// err_msg(0, "hashtable_get dec %d %d\n", --hashtable->tmp);
-   }
-   SemUtil::inc(hashtable->mutex);
-   // ================
+    rv = SemUtil::dec(hashtable->wlock);
+    if(rv != 0) {
+      LoggerFactory::getLogger()->error(rv, "hashtable_get\n");
+    }
+  }
+  rv = SemUtil::inc(hashtable->mutex);
+  if(rv != 0) {
+    LoggerFactory::getLogger()->error(rv, "hashtable_get\n");
+  }
+  // ================
 
-   void * res = _hashtable_get(hashtable, key);
+  void * res = _hashtable_get(hashtable, key);
 
-   // ==================
+  // ==================
 
-   SemUtil::dec(hashtable->mutex);
-   hashtable->readcnt--;
-   if(hashtable->readcnt == 0) {
+  rv = SemUtil::dec(hashtable->mutex);
+  if(rv != 0) {
+    LoggerFactory::getLogger()->error(rv, "hashtable_get\n");
+  }
+  hashtable->readcnt--;
+  if(hashtable->readcnt == 0) {
     //閲婃斁璇诲啓閿�
-    SemUtil::inc(hashtable->wlock);
-// err_msg(0, "hashtable_get inc %d\n", ++hashtable->tmp);
-  //閫氱煡鍐�
-    SemUtil::set(hashtable->cond, 1);
-   }
-   SemUtil::inc(hashtable->mutex);
-   return res;
+    rv = SemUtil::inc(hashtable->wlock);
+    if(rv != 0) {
+      LoggerFactory::getLogger()->error(rv, "hashtable_get\n");
+    }
+    //閫氱煡鍐�
+    rv = SemUtil::set(hashtable->cond, 1);
+    if(rv != 0) {
+      LoggerFactory::getLogger()->error(rv, "hashtable_get\n");
+    }
+  }
+
+  rv = SemUtil::inc(hashtable->mutex);
+  if(rv != 0) {
+    LoggerFactory::getLogger()->error(rv, "hashtable_get\n");
+  }
+  return res;
 }
 
 void hashtable_put(hashtable_t *hashtable, int key, void *value) {
-   
-  SemUtil::dec(hashtable->mutex);
+
+  int rv;
+
+  rv = SemUtil::dec(hashtable->mutex);
+  if(rv != 0) {
+    LoggerFactory::getLogger()->error(rv, "hashtable_put\n");
+  }
   // 璁剧疆璇讳紭鍏堢骇楂�
   while (hashtable->readcnt > 0)
   {
-    SemUtil::set(hashtable->cond, 0);
-    SemUtil::inc(hashtable->mutex);
+    rv = SemUtil::set(hashtable->cond, 0);
+    if(rv != 0) {
+      LoggerFactory::getLogger()->error(rv, "hashtable_put\n");
+    }
+    rv = SemUtil::inc(hashtable->mutex);
+    if(rv != 0) {
+      LoggerFactory::getLogger()->error(rv, "hashtable_put\n");
+    }
     //绛夊緟鍐欓�氱煡
-    SemUtil::dec(hashtable->cond);
+    rv = SemUtil::dec(hashtable->cond);
+    if(rv != 0) {
+      LoggerFactory::getLogger()->error(rv, "hashtable_put\n");
+    }
 
-    SemUtil::dec(hashtable->mutex);
+
+    rv = SemUtil::dec(hashtable->mutex);
+    if(rv != 0) {
+      LoggerFactory::getLogger()->error(rv, "hashtable_put\n");
+    }
   }
-  SemUtil::inc(hashtable->mutex);
+  rv = SemUtil::inc(hashtable->mutex);
+  if(rv != 0) {
+    LoggerFactory::getLogger()->error(rv, "hashtable_put\n");
+  }
 
   //鑾峰彇璇诲啓閿�
-  SemUtil::dec(hashtable->wlock);
- // err_msg(0, "hashtable_put dec %d\n", --hashtable->tmp);
+  rv = SemUtil::dec(hashtable->wlock);
+  if(rv != 0) {
+    LoggerFactory::getLogger()->error(rv, "hashtable_put\n");
+  }
 
   _hashtable_put(hashtable, key, value);
 
   //閲婃斁璇诲啓閿�
-  SemUtil::inc(hashtable->wlock);
-// err_msg(0, "hashtable_put inc %d\n", ++hashtable->tmp);
+  rv = SemUtil::inc(hashtable->wlock);
+  if(rv != 0) {
+    LoggerFactory::getLogger()->error(rv, "hashtable_put\n");
+  }
 }
 
 
@@ -261,29 +321,51 @@
 
 
 void hashtable_foreach(hashtable_t *hashtable,  std::function<void(int, void *)>  cb) {
-   SemUtil::dec(hashtable->mutex);
-   hashtable->readcnt++;
-   if (hashtable->readcnt == 1) {
+  int rv;
+  rv = SemUtil::dec(hashtable->mutex);
+  if(rv != 0) {
+    LoggerFactory::getLogger()->error(rv, "hashtable_foreach\n");
+  }
+  hashtable->readcnt++;
+  if (hashtable->readcnt == 1) {
     //鑾峰彇璇诲啓閿�
-    SemUtil::dec(hashtable->wlock);
-   }
-   SemUtil::inc(hashtable->mutex);
+    rv = SemUtil::dec(hashtable->wlock);
+    if(rv != 0) {
+      LoggerFactory::getLogger()->error(rv, "hashtable_foreach\n");
+    }
+  }
+  rv = SemUtil::inc(hashtable->mutex);
+  if(rv != 0) {
+    LoggerFactory::getLogger()->error(rv, "hashtable_foreach\n");
+  }
 
-   // ==================
+  // ==================
 
-    _hashtable_foreach(hashtable, cb);
+  _hashtable_foreach(hashtable, cb);
 
-   // ==================
+  // ==================
 
-   SemUtil::dec(hashtable->mutex);
-   hashtable->readcnt--;
-   if(hashtable->readcnt == 0) {
+  rv = SemUtil::dec(hashtable->mutex);
+  if(rv != 0) {
+    LoggerFactory::getLogger()->error(rv, "hashtable_foreach\n");
+  }
+  hashtable->readcnt--;
+  if(hashtable->readcnt == 0) {
     //閲婃斁璇诲啓閿�
-    SemUtil::inc(hashtable->wlock);
-  //閫氱煡鍐�
-    SemUtil::set(hashtable->cond, 1);
-   }
-   SemUtil::inc(hashtable->mutex);
+    rv = SemUtil::inc(hashtable->wlock);
+    if(rv != 0) {
+      LoggerFactory::getLogger()->error(rv, "hashtable_foreach\n");
+    }
+    //閫氱煡鍐�
+    rv = SemUtil::set(hashtable->cond, 1);
+    if(rv != 0) {
+      LoggerFactory::getLogger()->error(rv, "hashtable_foreach\n");
+    }
+  }
+  rv = SemUtil::inc(hashtable->mutex);
+  if(rv != 0) {
+    LoggerFactory::getLogger()->error(rv, "hashtable_foreach\n");
+  }
 
 }
 
@@ -307,8 +389,12 @@
 
 
 int hashtable_alloc_key(hashtable_t *hashtable) {
+  int rv;
   int key = START_KEY;
-  SemUtil::dec(hashtable->wlock);
+  rv = SemUtil::dec(hashtable->wlock);
+  if(rv != 0) {
+    LoggerFactory::getLogger()->error(rv, "hashtable_alloc_key\n");
+  }
 
   while(_hashtable_get(hashtable, key) != NULL) {
     key++;
@@ -316,7 +402,9 @@
   // 鍗犵敤key
   _hashtable_put(hashtable, key, (void *)1);
 
-  SemUtil::inc(hashtable->wlock);
-// err_msg(0, "hashtable_alloc_key inc %d\n", ++hashtable->tmp);
+  rv = SemUtil::inc(hashtable->wlock);
+  if(rv != 0) {
+    LoggerFactory::getLogger()->error(rv, "hashtable_alloc_key\n");
+  }
   return key;
 }

--
Gitblit v1.8.0