| | |
| | | |
| | | typedef TAILQ_HEAD(tailq_header_t, tailq_entry_t) tailq_header_t; |
| | | |
| | | |
| | | static size_t hashcode(int key); |
| | | |
| | | void hashtable_init(hashtable_t *hashtable ) |
| | |
| | | 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) { |
| | | 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); |
| | | // ================ |
| | |
| | | if(hashtable->readcnt == 0) { |
| | | //释放读写锁 |
| | | SemUtil::inc(hashtable->wlock); |
| | | // err_msg(0, "hashtable_get inc %d\n", ++hashtable->tmp); |
| | | //通知写 |
| | | SemUtil::set(hashtable->cond, 1); |
| | | } |
| | |
| | | } |
| | | |
| | | 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); |
| | | } |
| | | // 设置读优先级高 |
| | | while (hashtable->readcnt > 0) |
| | | { |
| | | SemUtil::set(hashtable->cond, 0); |
| | | SemUtil::inc(hashtable->mutex); |
| | | //等待写通知 |
| | | if (SemUtil::dec_timeout(hashtable->cond, &timeout) != 0) { |
| | | hashtable->readcnt = 0; |
| | | SemUtil::inc(hashtable->cond); |
| | | SemUtil::dec(hashtable->cond); |
| | | } |
| | | |
| | | SemUtil::dec(hashtable->mutex); |
| | | |
| | |
| | | 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); |
| | | } |
| | | |
| | | |
| | |
| | | |
| | | int hashtable_alloc_key(hashtable_t *hashtable) { |
| | | int key = START_KEY; |
| | | |
| | | 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; |
| | | } |