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 |   75 +++++++++++++++++++++----------------
 1 files changed, 43 insertions(+), 32 deletions(-)

diff --git a/src/shm/hashtable.cpp b/src/shm/hashtable.cpp
index 7a5faf4..62d052e 100755
--- a/src/shm/hashtable.cpp
+++ b/src/shm/hashtable.cpp
@@ -5,6 +5,9 @@
 #include "logger_factory.h"
 #include <set>
 #include <functional>
+#include <limits.h>
+
+
 
 typedef struct tailq_entry_t
 {
@@ -29,22 +32,12 @@
 
   memset(hashtable, 0, sizeof(hashtable_t));
   hashtable->mutex = svsem_get(IPC_PRIVATE, 1);
-  // hashtable->wlock = svsem_get(IPC_PRIVATE, 1);
-  // hashtable->cond = svsem_get(IPC_PRIVATE, 1);
-  // hashtable->readcnt = 0;
-
-  // FILE * semfile = fopen("./sem.txt", "w+");
-  // if(semfile == NULL) {
-  //   err_exit(errno, "fopen");
-  // }
-  // fprintf(semfile, "hashtable->mutex=%d\n", hashtable->mutex);
-  // fclose(semfile);
+  hashtable->queueCount = 0;
+  hashtable->currentKey = START_KEY;
 }
 
 void hashtable_destroy(hashtable_t *hashtable) {
   svsem_remove( hashtable->mutex);
-  // svsem_remove( hashtable->wlock);
-  // svsem_remove( hashtable->cond);
 }
 
 
@@ -109,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] ;
@@ -130,6 +123,7 @@
 
         /* mm_free the item as we don't need it anymore. */
         mm_free(item);
+        hashtable->queueCount--;
         svsem_post(hashtable->mutex);
         return oldvalue;
       }
@@ -144,19 +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) {
@@ -164,7 +178,6 @@
     goto suc;
   }
   val = _hashtable_get(hashtable, key);
-  // val = 1鏄痑llockey鐨勬儏鍐�
   if(val != NULL && val != (void *)1) 
     goto fail;
 
@@ -183,12 +196,19 @@
   return false;
 }
 
-
+int hashtable_get_queue_count(hashtable_t *hashtable) {
+  return hashtable->queueCount;
+}
 
 int hashtable_alloc_key(hashtable_t *hashtable) {
   int rv;
-  int key = START_KEY;
-  rv = svsem_wait(hashtable->mutex);
+  int key = hashtable->currentKey;
+ 
+  if( key == INT_MAX || key < START_KEY) {
+    key = START_KEY;
+  }
+
+  rv = svsem_uni_wait(hashtable->mutex);
   if(rv != 0) {
     LoggerFactory::getLogger()->error(errno, "hashtable_alloc_key\n");
   }
@@ -199,10 +219,12 @@
   // 鍗犵敤key
   _hashtable_put(hashtable, key, (void *)1);
 
+  hashtable->currentKey = key;
   rv = svsem_post(hashtable->mutex);
   if(rv != 0) {
     LoggerFactory::getLogger()->error(errno, "hashtable_alloc_key\n");
   }
+
   return key;
 }
 
@@ -245,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++)
@@ -279,6 +290,7 @@
     hashtable->array[i] = NULL;
   }
 
+  hashtable->queueCount = 0;
   if((rv = svsem_post(hashtable->mutex)) != 0) {
     LoggerFactory::getLogger()->error(errno, "hashtable_removeall\n");
   }
@@ -289,7 +301,6 @@
 {
 
   return key % MAPSIZE;
-  /*printf("hashfun = %ld\n", code);*/
 }
 
 /**
@@ -312,4 +323,4 @@
     }
     printf("\n");
   }
-}
\ No newline at end of file
+}

--
Gitblit v1.8.0