wangzhengquan
2020-12-04 aa2f3b2a9968bb4928463bdae05fb026d16b60bb
src/queue/hashtable.c
@@ -3,6 +3,7 @@
#include "mm.h"
#include "sem_util.h"
#include <set>
#include <functional>
typedef struct tailq_entry_t
{
@@ -19,7 +20,10 @@
typedef  TAILQ_HEAD(tailq_header_t, tailq_entry_t) tailq_header_t;
static size_t hashcode(int key);
static struct timespec TIMEOUT = {1, 0};
void hashtable_init(hashtable_t *hashtable )
{
@@ -29,10 +33,11 @@
  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)
static inline void *_hashtable_get(hashtable_t *hashtable, int key)
{
  size_t code = hashcode(key);
  tailq_entry_t *item;
@@ -54,7 +59,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 +96,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 +116,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 +133,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 +149,7 @@
    mm_free(my_tailq_head);
    hashtable->array[i] = NULL;
  }
  SemUtil::inc(hashtable->wlock);
}
/**
@@ -170,36 +181,32 @@
  /*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);
   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);
   }
@@ -208,13 +215,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,15 +238,20 @@
  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);
}
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 +265,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 +311,23 @@
  }
  return keyset;
}
int hashtable_alloc_key(hashtable_t *hashtable) {
  int key = START_KEY;
  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;
}