From cd20fd79fdf9e36f7313fdc96615f124764bb582 Mon Sep 17 00:00:00 2001
From: wangzhengquan <wangzhengquan85@126.com>
Date: 星期五, 15 一月 2021 16:30:25 +0800
Subject: [PATCH] modify name
---
src/shm/hashtable.cpp | 215 +++++++++++++++++++++++++++++++++++------------------
1 files changed, 143 insertions(+), 72 deletions(-)
diff --git a/src/shm/hashtable.cpp b/src/shm/hashtable.cpp
index 243067b..5ee06ba 100755
--- a/src/shm/hashtable.cpp
+++ b/src/shm/hashtable.cpp
@@ -2,6 +2,7 @@
#include "hashtable.h"
#include "mm.h"
#include "sem_util.h"
+#include "logger_factory.h"
#include <set>
#include <functional>
@@ -23,11 +24,11 @@
static size_t hashcode(int key);
-static struct timespec TIMEOUT = {1, 0};
+static struct timespec TIMEOUT = {2, 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);
@@ -102,8 +103,11 @@
size_t code = hashcode(key);
tailq_entry_t *item;
void *oldvalue;
+ int rv;
- SemUtil::dec(hashtable->wlock);
+ if( (rv = SemUtil::dec(hashtable->wlock)) != 0) {
+ LoggerFactory::getLogger()->error(rv, "hashtable_remove\n");
+ }
tailq_header_t *my_tailq_head = hashtable->array[code] ;
if ( my_tailq_head == NULL)
{
@@ -127,7 +131,10 @@
}
}
}
- SemUtil::inc(hashtable->wlock);
+ if((rv = SemUtil::inc(hashtable->wlock)) != 0) {
+ LoggerFactory::getLogger()->error(rv, "hashtable_remove\n");
+ }
+
return NULL;
}
@@ -139,7 +146,11 @@
void hashtable_removeall(hashtable_t *hashtable)
{
tailq_entry_t *item;
- SemUtil::dec(hashtable->wlock);
+ int rv;
+ rv = SemUtil::dec(hashtable->wlock);
+ if(rv != 0) {
+ LoggerFactory::getLogger()->error(rv, "hashtable_removeall\n");
+ }
for (int i = 0; i < MAPSIZE; i++)
{
tailq_header_t *my_tailq_head = hashtable->array[i] ;
@@ -155,7 +166,10 @@
mm_free(my_tailq_head);
hashtable->array[i] = NULL;
}
- SemUtil::inc(hashtable->wlock);
+ rv = SemUtil::inc(hashtable->wlock);
+ if(rv != 0) {
+ LoggerFactory::getLogger()->error(rv, "hashtable_removeall\n");
+ }
}
/**
@@ -182,77 +196,110 @@
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) {
+ int rv;
+ rv = SemUtil::dec(hashtable->mutex);
+ if(rv != 0) {
+ LoggerFactory::getLogger()->error(rv, "hashtable_get\n");
+ }
+ 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);
- // ================
+ rv = SemUtil::dec(hashtable->wlock);
+ if(rv != 0) {
+ LoggerFactory::getLogger()->error(rv, "hashtable_get\n");
+ }
+ }
+ rv = SemUtil::inc(hashtable->mutex);
+ if(rv != 0) {
+ LoggerFactory::getLogger()->error(rv, "hashtable_get\n");
+ }
+ // ================
- void * res = _hashtable_get(hashtable, key);
+ void * res = _hashtable_get(hashtable, key);
- // ==================
+ // ==================
- SemUtil::dec(hashtable->mutex);
- hashtable->readcnt--;
- if(hashtable->readcnt == 0) {
+ rv = SemUtil::dec(hashtable->mutex);
+ if(rv != 0) {
+ LoggerFactory::getLogger()->error(rv, "hashtable_get\n");
+ }
+ 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;
+ rv = SemUtil::inc(hashtable->wlock);
+ if(rv != 0) {
+ LoggerFactory::getLogger()->error(rv, "hashtable_get\n");
+ }
+ //閫氱煡鍐�
+ rv = SemUtil::set(hashtable->cond, 1);
+ if(rv != 0) {
+ LoggerFactory::getLogger()->error(rv, "hashtable_get\n");
+ }
+ }
+
+ rv = SemUtil::inc(hashtable->mutex);
+ if(rv != 0) {
+ LoggerFactory::getLogger()->error(rv, "hashtable_get\n");
+ }
+ 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);
+
+ int rv;
+
+ rv = SemUtil::dec(hashtable->mutex);
+ if(rv != 0) {
+ LoggerFactory::getLogger()->error(rv, "hashtable_put\n");
}
// 璁剧疆璇讳紭鍏堢骇楂�
while (hashtable->readcnt > 0)
{
- SemUtil::set(hashtable->cond, 0);
- SemUtil::inc(hashtable->mutex);
+ rv = SemUtil::set(hashtable->cond, 0);
+ if(rv != 0) {
+ LoggerFactory::getLogger()->error(rv, "hashtable_put\n");
+ }
+ rv = SemUtil::inc(hashtable->mutex);
+ if(rv != 0) {
+ LoggerFactory::getLogger()->error(rv, "hashtable_put\n");
+ }
//绛夊緟鍐欓�氱煡
- if (SemUtil::dec_timeout(hashtable->cond, &timeout) != 0) {
- hashtable->readcnt = 0;
- SemUtil::inc(hashtable->cond);
- SemUtil::dec(hashtable->cond);
+ rv = SemUtil::dec(hashtable->cond);
+ if(rv != 0) {
+ LoggerFactory::getLogger()->error(rv, "hashtable_put\n");
}
- SemUtil::dec(hashtable->mutex);
-
+
+ rv = SemUtil::dec(hashtable->mutex);
+ if(rv != 0) {
+ LoggerFactory::getLogger()->error(rv, "hashtable_put\n");
+ }
}
- SemUtil::inc(hashtable->mutex);
+ rv = SemUtil::inc(hashtable->mutex);
+ if(rv != 0) {
+ LoggerFactory::getLogger()->error(rv, "hashtable_put\n");
+ }
//鑾峰彇璇诲啓閿�
-
- SemUtil::dec(hashtable->wlock);
- // err_msg(0, "hashtable_put dec %d\n", --hashtable->tmp);
+ rv = SemUtil::dec(hashtable->wlock);
+ if(rv != 0) {
+ LoggerFactory::getLogger()->error(rv, "hashtable_put\n");
+ }
_hashtable_put(hashtable, key, value);
//閲婃斁璇诲啓閿�
- SemUtil::inc(hashtable->wlock);
-// err_msg(0, "hashtable_put inc %d\n", ++hashtable->tmp);
+ rv = SemUtil::inc(hashtable->wlock);
+ if(rv != 0) {
+ LoggerFactory::getLogger()->error(rv, "hashtable_put\n");
+ }
}
@@ -274,29 +321,51 @@
void hashtable_foreach(hashtable_t *hashtable, std::function<void(int, void *)> cb) {
- SemUtil::dec(hashtable->mutex);
- hashtable->readcnt++;
- if (hashtable->readcnt == 1) {
+ int rv;
+ rv = SemUtil::dec(hashtable->mutex);
+ if(rv != 0) {
+ LoggerFactory::getLogger()->error(rv, "hashtable_foreach\n");
+ }
+ hashtable->readcnt++;
+ if (hashtable->readcnt == 1) {
//鑾峰彇璇诲啓閿�
- SemUtil::dec(hashtable->wlock);
- }
- SemUtil::inc(hashtable->mutex);
+ rv = SemUtil::dec(hashtable->wlock);
+ if(rv != 0) {
+ LoggerFactory::getLogger()->error(rv, "hashtable_foreach\n");
+ }
+ }
+ rv = SemUtil::inc(hashtable->mutex);
+ if(rv != 0) {
+ LoggerFactory::getLogger()->error(rv, "hashtable_foreach\n");
+ }
- // ==================
+ // ==================
- _hashtable_foreach(hashtable, cb);
+ _hashtable_foreach(hashtable, cb);
- // ==================
+ // ==================
- SemUtil::dec(hashtable->mutex);
- hashtable->readcnt--;
- if(hashtable->readcnt == 0) {
+ rv = SemUtil::dec(hashtable->mutex);
+ if(rv != 0) {
+ LoggerFactory::getLogger()->error(rv, "hashtable_foreach\n");
+ }
+ hashtable->readcnt--;
+ if(hashtable->readcnt == 0) {
//閲婃斁璇诲啓閿�
- SemUtil::inc(hashtable->wlock);
- //閫氱煡鍐�
- SemUtil::set(hashtable->cond, 1);
- }
- SemUtil::inc(hashtable->mutex);
+ rv = SemUtil::inc(hashtable->wlock);
+ if(rv != 0) {
+ LoggerFactory::getLogger()->error(rv, "hashtable_foreach\n");
+ }
+ //閫氱煡鍐�
+ rv = SemUtil::set(hashtable->cond, 1);
+ if(rv != 0) {
+ LoggerFactory::getLogger()->error(rv, "hashtable_foreach\n");
+ }
+ }
+ rv = SemUtil::inc(hashtable->mutex);
+ if(rv != 0) {
+ LoggerFactory::getLogger()->error(rv, "hashtable_foreach\n");
+ }
}
@@ -320,11 +389,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 = SemUtil::dec(hashtable->wlock);
+ if(rv != 0) {
+ LoggerFactory::getLogger()->error(rv, "hashtable_alloc_key\n");
}
while(_hashtable_get(hashtable, key) != NULL) {
@@ -333,7 +402,9 @@
// 鍗犵敤key
_hashtable_put(hashtable, key, (void *)1);
- SemUtil::inc(hashtable->wlock);
-// err_msg(0, "hashtable_alloc_key inc %d\n", ++hashtable->tmp);
+ rv = SemUtil::inc(hashtable->wlock);
+ if(rv != 0) {
+ LoggerFactory::getLogger()->error(rv, "hashtable_alloc_key\n");
+ }
return key;
}
--
Gitblit v1.8.0