From e1e97f1f98baf82efcd5825d7c7a7b4b1b2f2e40 Mon Sep 17 00:00:00 2001 From: wangzhengquan <wangzhengquan85@126.com> Date: 星期一, 13 七月 2020 19:03:21 +0800 Subject: [PATCH] udpate --- queue/hashtable.c | 72 ++++++++++++++++++++++++++++++++++- 1 files changed, 69 insertions(+), 3 deletions(-) diff --git a/queue/hashtable.c b/queue/hashtable.c index 9b66bb3..35530df 100755 --- a/queue/hashtable.c +++ b/queue/hashtable.c @@ -1,7 +1,7 @@ #include "usg_common.h" #include "hashtable.h" #include "mm.h" - +#include "sem_util.h" typedef struct tailq_entry_t { @@ -14,6 +14,9 @@ TAILQ_ENTRY(tailq_entry_t) joint; } tailq_entry_t; +static int hashtable_mutex; + +#define START_KEY 1000 typedef TAILQ_HEAD(tailq_header_t, tailq_entry_t) tailq_header_t; @@ -21,11 +24,16 @@ 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; } -void *hashtable_get(hashtable_t *hashtable, int key) +void *_hashtable_get(hashtable_t *hashtable, int key) { size_t code = hashcode(key); tailq_entry_t *item; @@ -47,7 +55,7 @@ } -void* hashtable_put(hashtable_t *hashtable, int key, void *value) +void* _hashtable_put(hashtable_t *hashtable, int key, void *value) { size_t code = hashcode(key); void *oldvalue; @@ -158,3 +166,61 @@ return key % MAPSIZE; /*printf("hashfun = %ld\n", code);*/ } + + +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); + hashtable->readcnt++; + if (hashtable->readcnt == 1) { + //鑾峰彇璇诲啓閿� + SemUtil::dec(hashtable->wlock); + } + SemUtil::inc(hashtable->mutex); + + void * res = _hashtable_get(hashtable, key); + + SemUtil::dec(hashtable->mutex); + hashtable->readcnt--; + if(hashtable->readcnt == 0) { + //閲婃斁璇诲啓閿� + SemUtil::inc(hashtable->wlock); + //閫氱煡鍐� + SemUtil::set(hashtable->cond, 1); + } + SemUtil::inc(hashtable->mutex); + return res; +} + +void* hashtable_put(hashtable_t *hashtable, int key, void *value) { + SemUtil::dec(hashtable->mutex); + while (hashtable->readcnt > 0) + { + SemUtil::set(hashtable->cond, 0); + SemUtil::inc(hashtable->mutex); + //绛夊緟鍐欓�氱煡 + SemUtil::dec(hashtable->cond); + + SemUtil::dec(hashtable->mutex); + + } + SemUtil::inc(hashtable->mutex); + + //鑾峰彇璇诲啓閿� + SemUtil::dec(hashtable->wlock); + _hashtable_put(hashtable, key, value); + //閲婃斁璇诲啓閿� + SemUtil::inc(hashtable->wlock); +} -- Gitblit v1.8.0