From ddbeeaaffeab5bc997a0b7a7e8dcac863610feee Mon Sep 17 00:00:00 2001
From: wangzhengquan <wangzhengquan85@126.com>
Date: 星期三, 05 八月 2020 20:04:52 +0800
Subject: [PATCH] udpate

---
 src/queue/hashtable.c |   74 +++++++++++++++++++++++++++++-------
 1 files changed, 59 insertions(+), 15 deletions(-)

diff --git a/src/queue/hashtable.c b/src/queue/hashtable.c
index 43c59ce..110b429 100755
--- a/src/queue/hashtable.c
+++ b/src/queue/hashtable.c
@@ -3,6 +3,7 @@
 #include "mm.h"
 #include "sem_util.h"
 #include <set>
+#include <functional>
 
 typedef struct tailq_entry_t
 {
@@ -32,7 +33,7 @@
 }
 
 
-void *_hashtable_get(hashtable_t *hashtable, int key)
+static inline void *_hashtable_get(hashtable_t *hashtable, int key)
 {
   size_t code = hashcode(key);
   tailq_entry_t *item;
@@ -54,7 +55,7 @@
 
 }
 
-void * _hashtable_put(hashtable_t *hashtable, int key, void *value)
+static inline void * _hashtable_put(hashtable_t *hashtable, int key, void *value)
 {
   size_t code = hashcode(key);
   void *oldvalue;
@@ -91,9 +92,12 @@
   size_t code = hashcode(key);
   tailq_entry_t *item;
   void *oldvalue;
+
+  SemUtil::dec(hashtable->wlock);
   tailq_header_t *my_tailq_head = hashtable->array[code] ;
   if ( my_tailq_head == NULL)
   {
+    SemUtil::inc(hashtable->wlock);
     return NULL;
   }
   else
@@ -108,11 +112,12 @@
 
         /* mm_free the item as we don't need it anymore. */
         mm_free(item);
-
+        SemUtil::inc(hashtable->wlock);
         return oldvalue;
       }
     }
   }
+  SemUtil::inc(hashtable->wlock);
   return NULL;
 
 }
@@ -124,6 +129,7 @@
 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] ;
@@ -139,6 +145,7 @@
     mm_free(my_tailq_head);
     hashtable->array[i] = NULL;
   }
+  SemUtil::inc(hashtable->wlock);
 }
 
 /**
@@ -171,18 +178,7 @@
 }
 
 
-int hashtable_alloc_key(hashtable_t *hashtable) {
-  int key = START_KEY;
-  SemUtil::dec(hashtable->wlock);
 
-  while(_hashtable_get(hashtable, key) != NULL) {
-    key++;
-  }
-
-  _hashtable_put(hashtable, key, (void *)1);
-  SemUtil::inc(hashtable->wlock);
-  return key;
-}
 
 void *hashtable_get(hashtable_t *hashtable, int key) {
    SemUtil::dec(hashtable->mutex);
@@ -192,8 +188,11 @@
     SemUtil::dec(hashtable->wlock);
    }
    SemUtil::inc(hashtable->mutex);
+   // ================
 
    void * res = _hashtable_get(hashtable, key);
+
+   // ==================
 
    SemUtil::dec(hashtable->mutex);
    hashtable->readcnt--;
@@ -209,6 +208,7 @@
 
 void hashtable_put(hashtable_t *hashtable, int key, void *value) {
   SemUtil::dec(hashtable->mutex);
+  // 璁剧疆璇讳紭鍏堢骇楂�
   while (hashtable->readcnt > 0)
   {
     SemUtil::set(hashtable->cond, 0);
@@ -223,6 +223,7 @@
 
   //鑾峰彇璇诲啓閿�
   SemUtil::dec(hashtable->wlock);
+
   _hashtable_put(hashtable, key, value);
   //閲婃斁璇诲啓閿�
   SemUtil::inc(hashtable->wlock);
@@ -230,7 +231,7 @@
 
 
 
-void hashtable_foreach(hashtable_t *hashtable, hashtable_foreach_cb cb) {
+static inline void _hashtable_foreach(hashtable_t *hashtable, std::function<void(int, void *)>  cb) {
   tailq_entry_t *item;
   for (int i = 0; i < MAPSIZE; i++) {
     tailq_header_t *my_tailq_head = hashtable->array[i] ;
@@ -244,6 +245,35 @@
     }
   }
 }
+
+
+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);
+
+}
+
 
 std::set<int> * hashtable_keyset(hashtable_t *hashtable) {
   std::set<int> *keyset = new std::set<int>;
@@ -261,3 +291,17 @@
   }
   return keyset;
 }
+
+
+int hashtable_alloc_key(hashtable_t *hashtable) {
+  int key = START_KEY;
+  SemUtil::dec(hashtable->wlock);
+
+  while(_hashtable_get(hashtable, key) != NULL) {
+    key++;
+  }
+  // 鍗犵敤key
+  _hashtable_put(hashtable, key, (void *)1);
+  SemUtil::inc(hashtable->wlock);
+  return key;
+}

--
Gitblit v1.8.0