From bae3a4fd9406635608edf0c0d16c52cf7ca06a66 Mon Sep 17 00:00:00 2001
From: fujuntang <fujuntang@smartai.com>
Date: 星期六, 23 十月 2021 15:20:57 +0800
Subject: [PATCH] Optimize the code source.

---
 src/shm/hashtable.cpp |   42 ++++++++++++++++++++++++------------------
 1 files changed, 24 insertions(+), 18 deletions(-)

diff --git a/src/shm/hashtable.cpp b/src/shm/hashtable.cpp
index e435172..62d052e 100755
--- a/src/shm/hashtable.cpp
+++ b/src/shm/hashtable.cpp
@@ -102,7 +102,7 @@
   void *oldvalue;
   int rv;
 
-  if( (rv = svsem_wait(hashtable->mutex)) != 0) {
+  if( (rv = svsem_uni_wait(hashtable->mutex)) != 0) {
     LoggerFactory::getLogger()->error(errno, "hashtable_remove\n");
   }
   tailq_header_t *my_tailq_head = hashtable->array[code] ;
@@ -138,20 +138,39 @@
 }
 
 void *hashtable_get(hashtable_t *hashtable, int key) {
+  int rv;
+
+  if((rv = svsem_uni_wait(hashtable->mutex)) != 0) {
+    LoggerFactory::getLogger()->error(errno, "hashtable_get\n");
+  }
   void * res = _hashtable_get(hashtable, key);
+
+  if((rv = svsem_post(hashtable->mutex)) != 0) {
+    LoggerFactory::getLogger()->error(errno, "hashtable_get\n");
+  }
   return res;
 }
 
 void hashtable_put(hashtable_t *hashtable, int key, void *value) {
+  int rv;
+
+  if((rv = svsem_uni_wait(hashtable->mutex)) != 0) {
+    LoggerFactory::getLogger()->error(errno, "hashtable_put\n");
+  }
   _hashtable_put(hashtable, key, value); 
   hashtable->queueCount++;
+
+  if((rv = svsem_post(hashtable->mutex)) != 0) {
+    LoggerFactory::getLogger()->error(errno, "hashtable_put\n");
+  }
+
 }
 
 bool hashtable_check_put(hashtable_t *hashtable, int key, void *value, bool overwrite) {
 
   int rv;
   void * val;
-  if(( rv = svsem_wait(hashtable->mutex)) != 0) {
+  if(( rv = svsem_uni_wait(hashtable->mutex)) != 0) {
     LoggerFactory::getLogger()->error(errno, "hashtable_put\n");
   }
   if(overwrite) {
@@ -159,7 +178,6 @@
     goto suc;
   }
   val = _hashtable_get(hashtable, key);
-  // val = 1鏄痑llockey鐨勬儏鍐�
   if(val != NULL && val != (void *)1) 
     goto fail;
 
@@ -190,7 +208,7 @@
     key = START_KEY;
   }
 
-  rv = svsem_wait(hashtable->mutex);
+  rv = svsem_uni_wait(hashtable->mutex);
   if(rv != 0) {
     LoggerFactory::getLogger()->error(errno, "hashtable_alloc_key\n");
   }
@@ -249,22 +267,11 @@
   return keyset;
 }
 
-
-
-int hashtable_lock(hashtable_t *hashtable) {
-  return svsem_wait(hashtable->mutex);
-}
-
-int hashtable_unlock(hashtable_t *hashtable) {
-  return svsem_post(hashtable->mutex);
-}
-
-
 void hashtable_removeall(hashtable_t *hashtable)
 {
   tailq_entry_t *item;
   int rv;
-  if( (rv = svsem_wait(hashtable->mutex)) != 0) {
+  if( (rv = svsem_uni_wait(hashtable->mutex)) != 0) {
     LoggerFactory::getLogger()->error(errno, "hashtable_removeall\n");
   }
   for (int i = 0; i < MAPSIZE; i++)
@@ -294,7 +301,6 @@
 {
 
   return key % MAPSIZE;
-  /*printf("hashfun = %ld\n", code);*/
 }
 
 /**
@@ -317,4 +323,4 @@
     }
     printf("\n");
   }
-}
\ No newline at end of file
+}

--
Gitblit v1.8.0