From 4d5adf4ac44c864e67e8019bb97d89199bb0b4b7 Mon Sep 17 00:00:00 2001
From: wangzhengquan <wangzhengquan85@126.com>
Date: 星期四, 06 八月 2020 17:06:27 +0800
Subject: [PATCH] fix survey

---
 src/queue/hashtable.c |   36 ++++++++++++++++++++++++++++++++----
 1 files changed, 32 insertions(+), 4 deletions(-)

diff --git a/src/queue/hashtable.c b/src/queue/hashtable.c
index 110b429..ed3fe34 100755
--- a/src/queue/hashtable.c
+++ b/src/queue/hashtable.c
@@ -20,6 +20,7 @@
 
 typedef  TAILQ_HEAD(tailq_header_t, tailq_entry_t) tailq_header_t;
 
+
 static size_t hashcode(int key);
 
 void hashtable_init(hashtable_t *hashtable )
@@ -30,6 +31,7 @@
   hashtable->wlock = SemUtil::get(IPC_PRIVATE, 1);
   hashtable->cond = SemUtil::get(IPC_PRIVATE, 1);
   hashtable->readcnt = 0;
+
 }
 
 
@@ -181,11 +183,17 @@
 
 
 void *hashtable_get(hashtable_t *hashtable, int key) {
-   SemUtil::dec(hashtable->mutex);
+   struct timespec timeout = {1, 0};
+   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);
    // ================
@@ -199,6 +207,7 @@
    if(hashtable->readcnt == 0) {
     //閲婃斁璇诲啓閿�
     SemUtil::inc(hashtable->wlock);
+// err_msg(0, "hashtable_get inc %d\n", ++hashtable->tmp);
   //閫氱煡鍐�
     SemUtil::set(hashtable->cond, 1);
    }
@@ -207,14 +216,22 @@
 }
 
 void hashtable_put(hashtable_t *hashtable, int key, void *value) {
-  SemUtil::dec(hashtable->mutex);
+  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);
     //绛夊緟鍐欓�氱煡
-    SemUtil::dec(hashtable->cond);
+    if (SemUtil::dec_timeout(hashtable->cond, &timeout) != 0) {
+      hashtable->readcnt = 0;
+      SemUtil::inc(hashtable->cond);
+      SemUtil::dec(hashtable->cond);
+    }
 
     SemUtil::dec(hashtable->mutex);
      
@@ -222,11 +239,15 @@
   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);
 }
 
 
@@ -295,13 +316,20 @@
 
 int hashtable_alloc_key(hashtable_t *hashtable) {
   int key = START_KEY;
-  SemUtil::dec(hashtable->wlock);
+
+  struct timespec timeout = {1, 0};
+  if (SemUtil::dec_timeout(hashtable->wlock, &timeout) != 0) {
+    SemUtil::inc(hashtable->wlock);
+    SemUtil::dec(hashtable->wlock);
+  }
 
   while(_hashtable_get(hashtable, key) != NULL) {
     key++;
   }
   // 鍗犵敤key
   _hashtable_put(hashtable, key, (void *)1);
+
   SemUtil::inc(hashtable->wlock);
+// err_msg(0, "hashtable_alloc_key inc %d\n", ++hashtable->tmp);
   return key;
 }

--
Gitblit v1.8.0