wangzhengquan
2021-01-13 973692652774a5ffe98478ee287b40af529d0b39
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>
@@ -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");
  }
}
/**
@@ -189,57 +203,103 @@
void *hashtable_get(hashtable_t *hashtable, int key) {
  
   SemUtil::dec(hashtable->mutex);
  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);
    rv = SemUtil::dec(hashtable->wlock);
    if(rv != 0) {
      LoggerFactory::getLogger()->error(rv, "hashtable_get\n");
   }
   SemUtil::inc(hashtable->mutex);
  }
  rv = SemUtil::inc(hashtable->mutex);
  if(rv != 0) {
    LoggerFactory::getLogger()->error(rv, "hashtable_get\n");
  }
   // ================
   void * res = _hashtable_get(hashtable, key);
   // ==================
   SemUtil::dec(hashtable->mutex);
  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);
    rv = SemUtil::inc(hashtable->wlock);
    if(rv != 0) {
      LoggerFactory::getLogger()->error(rv, "hashtable_get\n");
   }
   SemUtil::inc(hashtable->mutex);
    //通知写
    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) {
   
  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);
    //等待写通知
    SemUtil::dec(hashtable->cond);
    SemUtil::dec(hashtable->mutex);
    rv = SemUtil::set(hashtable->cond, 0);
    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");
    }
    //等待写通知
    rv = SemUtil::dec(hashtable->cond);
    if(rv != 0) {
      LoggerFactory::getLogger()->error(rv, "hashtable_put\n");
    }
    rv = SemUtil::dec(hashtable->mutex);
    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");
  }
  //获取读写锁
  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");
  }
}
@@ -261,13 +321,23 @@
void hashtable_foreach(hashtable_t *hashtable,  std::function<void(int, void *)>  cb) {
   SemUtil::dec(hashtable->mutex);
  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);
    rv = SemUtil::dec(hashtable->wlock);
    if(rv != 0) {
      LoggerFactory::getLogger()->error(rv, "hashtable_foreach\n");
   }
   SemUtil::inc(hashtable->mutex);
  }
  rv = SemUtil::inc(hashtable->mutex);
  if(rv != 0) {
    LoggerFactory::getLogger()->error(rv, "hashtable_foreach\n");
  }
   // ==================
@@ -275,15 +345,27 @@
   // ==================
   SemUtil::dec(hashtable->mutex);
  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);
    rv = SemUtil::inc(hashtable->wlock);
    if(rv != 0) {
      LoggerFactory::getLogger()->error(rv, "hashtable_foreach\n");
   }
   SemUtil::inc(hashtable->mutex);
    //通知写
    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");
  }
}
@@ -307,8 +389,12 @@
int hashtable_alloc_key(hashtable_t *hashtable) {
  int rv;
  int key = START_KEY;
  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) {
    key++;
@@ -316,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;
}