From c46be6db32872bfd7c4010b43526b5e6bc0fa6a5 Mon Sep 17 00:00:00 2001
From: wangzhengquan <wangzhengquan85@126.com>
Date: 星期一, 25 一月 2021 14:04:59 +0800
Subject: [PATCH] update

---
 src/shm/hashtable.cpp |  265 +++++++++++++++++++++-------------------------------
 1 files changed, 107 insertions(+), 158 deletions(-)

diff --git a/src/shm/hashtable.cpp b/src/shm/hashtable.cpp
index 243067b..2248ad9 100755
--- a/src/shm/hashtable.cpp
+++ b/src/shm/hashtable.cpp
@@ -1,7 +1,8 @@
 #include "usg_common.h"
 #include "hashtable.h"
 #include "mm.h"
-#include "sem_util.h"
+#include "svsem.h"
+#include "logger_factory.h"
 #include <set>
 #include <functional>
 
@@ -23,23 +24,27 @@
 
 static size_t hashcode(int key);
 
-static struct timespec TIMEOUT = {1, 0};
-
 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);
-  hashtable->cond = SemUtil::get(IPC_PRIVATE, 1);
-  hashtable->readcnt = 0;
 
+  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);
 }
 
 void hashtable_destroy(hashtable_t *hashtable) {
-  SemUtil::remove( hashtable->mutex);
-  SemUtil::remove( hashtable->wlock);
-  SemUtil::remove( hashtable->cond);
+  svsem_remove( hashtable->mutex);
+  // svsem_remove( hashtable->wlock);
+  // svsem_remove( hashtable->cond);
 }
 
 
@@ -102,16 +107,19 @@
   size_t code = hashcode(key);
   tailq_entry_t *item;
   void *oldvalue;
+  int rv;
 
-  SemUtil::dec(hashtable->wlock);
+  if( (rv = svsem_wait(hashtable->mutex)) != 0) {
+    LoggerFactory::getLogger()->error(errno, "hashtable_remove\n");
+  }
   tailq_header_t *my_tailq_head = hashtable->array[code] ;
   if ( my_tailq_head == NULL)
   {
-    SemUtil::inc(hashtable->wlock);
+    if((rv = svsem_post(hashtable->mutex)) != 0) {
+      LoggerFactory::getLogger()->error(errno, "hashtable_remove\n");
+    }
     return NULL;
-  }
-  else
-  {
+  } else {
     for (item = TAILQ_FIRST(my_tailq_head); item != NULL; item = TAILQ_NEXT(item, joint))
     {
       if (key == item->key)
@@ -122,137 +130,39 @@
 
         /* mm_free the item as we don't need it anymore. */
         mm_free(item);
-        SemUtil::inc(hashtable->wlock);
+        svsem_post(hashtable->mutex);
         return oldvalue;
       }
     }
-  }
-  SemUtil::inc(hashtable->wlock);
-  return NULL;
 
-}
-
-
-
-
-
-void hashtable_removeall(hashtable_t *hashtable)
-{
-  tailq_entry_t *item;
-  SemUtil::dec(hashtable->wlock);
-  for (int i = 0; i < MAPSIZE; i++)
-  {
-    tailq_header_t *my_tailq_head = hashtable->array[i] ;
-
-    if (my_tailq_head == NULL )
-      continue;
-
-    while ((item = TAILQ_FIRST(my_tailq_head)) )
-    {
-      TAILQ_REMOVE(my_tailq_head, item, joint);
-      mm_free(item);
+    if((rv = svsem_post(hashtable->mutex)) != 0) {
+      LoggerFactory::getLogger()->error(errno, "hashtable_remove\n");
     }
-    mm_free(my_tailq_head);
-    hashtable->array[i] = NULL;
-  }
-  SemUtil::inc(hashtable->wlock);
-}
 
-/**
- * for debug
- */
-void hashtable_printall(hashtable_t *hashtable)
-{
-  tailq_entry_t *item;
-  for (int i = 0; i < MAPSIZE; i++)
-  {
-    tailq_header_t *my_tailq_head = hashtable->array[i] ;
-
-    if (my_tailq_head == NULL )
-      continue;
-
-    printf("code=%d\n", i);
-    TAILQ_FOREACH(item, my_tailq_head, joint)
-    {
-      printf("%d:%s\n", item->key, (char *)item->value);
-    }
-    printf("\n");
+    return NULL;
   }
 }
 
-static size_t hashcode(int key)
-{
-  
-  return key % MAPSIZE;
-  /*printf("hashfun = %ld\n", code);*/
-}
+
+
 
 void *hashtable_get(hashtable_t *hashtable, int key) {
-  
-   if (SemUtil::dec_timeout(hashtable->mutex, &TIMEOUT) != 0) {
-    SemUtil::inc(hashtable->mutex);
-    SemUtil::dec(hashtable->mutex);
-   }
-
-   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);
-   // ================
-
-   void * res = _hashtable_get(hashtable, key);
-
-   // ==================
-
-   SemUtil::dec(hashtable->mutex);
-   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;
+  void * res = _hashtable_get(hashtable, key);
+  return res;
 }
 
 void hashtable_put(hashtable_t *hashtable, int key, void *value) {
-  struct timespec timeout = {2, 0};
-  if (SemUtil::dec_timeout(hashtable->mutex, &timeout) != 0) {
-    SemUtil::inc(hashtable->mutex);
-    SemUtil::dec(hashtable->mutex);
-  }
-  // 璁剧疆璇讳紭鍏堢骇楂�
-  while (hashtable->readcnt > 0)
-  {
-    SemUtil::set(hashtable->cond, 0);
-    SemUtil::inc(hashtable->mutex);
-    //绛夊緟鍐欓�氱煡
-    if (SemUtil::dec_timeout(hashtable->cond, &timeout) != 0) {
-      hashtable->readcnt = 0;
-      SemUtil::inc(hashtable->cond);
-      SemUtil::dec(hashtable->cond);
-    }
 
-    SemUtil::dec(hashtable->mutex);
-     
+  int rv;
+  if(( rv = svsem_wait(hashtable->mutex)) != 0) {
+    LoggerFactory::getLogger()->error(errno, "hashtable_put\n");
   }
-  SemUtil::inc(hashtable->mutex);
-
-  //鑾峰彇璇诲啓閿�
- 
-  SemUtil::dec(hashtable->wlock);
- // err_msg(0, "hashtable_put dec %d\n", --hashtable->tmp);
 
   _hashtable_put(hashtable, key, value);
 
-  //閲婃斁璇诲啓閿�
-  SemUtil::inc(hashtable->wlock);
-// err_msg(0, "hashtable_put inc %d\n", ++hashtable->tmp);
+  if(( rv = svsem_post(hashtable->mutex)) != 0) {
+    LoggerFactory::getLogger()->error(errno, "hashtable_put\n");
+  }
 }
 
 
@@ -274,29 +184,7 @@
 
 
 void hashtable_foreach(hashtable_t *hashtable,  std::function<void(int, void *)>  cb) {
-   SemUtil::dec(hashtable->mutex);
-   hashtable->readcnt++;
-   if (hashtable->readcnt == 1) {
-    //鑾峰彇璇诲啓閿�
-    SemUtil::dec(hashtable->wlock);
-   }
-   SemUtil::inc(hashtable->mutex);
-
-   // ==================
-
-    _hashtable_foreach(hashtable, cb);
-
-   // ==================
-
-   SemUtil::dec(hashtable->mutex);
-   hashtable->readcnt--;
-   if(hashtable->readcnt == 0) {
-    //閲婃斁璇诲啓閿�
-    SemUtil::inc(hashtable->wlock);
-  //閫氱煡鍐�
-    SemUtil::set(hashtable->cond, 1);
-   }
-   SemUtil::inc(hashtable->mutex);
+ return _hashtable_foreach(hashtable, cb);
 
 }
 
@@ -320,11 +208,11 @@
 
 
 int hashtable_alloc_key(hashtable_t *hashtable) {
+  int rv;
   int key = START_KEY;
-
-  if (SemUtil::dec_timeout(hashtable->wlock, &TIMEOUT) != 0) {
-    SemUtil::inc(hashtable->wlock);
-    SemUtil::dec(hashtable->wlock);
+  rv = svsem_wait(hashtable->mutex);
+  if(rv != 0) {
+    LoggerFactory::getLogger()->error(errno, "hashtable_alloc_key\n");
   }
 
   while(_hashtable_get(hashtable, key) != NULL) {
@@ -333,7 +221,68 @@
   // 鍗犵敤key
   _hashtable_put(hashtable, key, (void *)1);
 
-  SemUtil::inc(hashtable->wlock);
-// err_msg(0, "hashtable_alloc_key inc %d\n", ++hashtable->tmp);
+  rv = svsem_post(hashtable->mutex);
+  if(rv != 0) {
+    LoggerFactory::getLogger()->error(errno, "hashtable_alloc_key\n");
+  }
   return key;
 }
+
+
+void hashtable_removeall(hashtable_t *hashtable)
+{
+  tailq_entry_t *item;
+  int rv;
+  if( (rv = svsem_wait(hashtable->mutex)) != 0) {
+    LoggerFactory::getLogger()->error(errno, "hashtable_removeall\n");
+  }
+  for (int i = 0; i < MAPSIZE; i++)
+  {
+    tailq_header_t *my_tailq_head = hashtable->array[i] ;
+
+    if (my_tailq_head == NULL )
+      continue;
+
+    while ((item = TAILQ_FIRST(my_tailq_head)) )
+    {
+      TAILQ_REMOVE(my_tailq_head, item, joint);
+      mm_free(item);
+    }
+    mm_free(my_tailq_head);
+    hashtable->array[i] = NULL;
+  }
+
+  if((rv = svsem_post(hashtable->mutex)) != 0) {
+    LoggerFactory::getLogger()->error(errno, "hashtable_removeall\n");
+  }
+}
+
+
+static size_t hashcode(int key)
+{
+
+  return key % MAPSIZE;
+  /*printf("hashfun = %ld\n", code);*/
+}
+
+/**
+ * for debug
+ */
+static void hashtable_printall(hashtable_t *hashtable)
+{
+  tailq_entry_t *item;
+  for (int i = 0; i < MAPSIZE; i++)
+  {
+    tailq_header_t *my_tailq_head = hashtable->array[i] ;
+
+    if (my_tailq_head == NULL )
+      continue;
+
+    printf("code=%d\n", i);
+    TAILQ_FOREACH(item, my_tailq_head, joint)
+    {
+      printf("%d:%s\n", item->key, (char *)item->value);
+    }
+    printf("\n");
+  }
+}
\ No newline at end of file

--
Gitblit v1.8.0