| | |
| | | } |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | void *hashtable_get(hashtable_t *hashtable, int key) { |
| | | void * res = _hashtable_get(hashtable, key); |
| | | return res; |
| | | } |
| | | |
| | | void hashtable_put(hashtable_t *hashtable, int key, void *value) { |
| | | _hashtable_put(hashtable, key, value); |
| | | } |
| | | |
| | | bool hashtable_check_put(hashtable_t *hashtable, int key, void *value, bool overwrite) { |
| | | |
| | | int rv; |
| | | void * val; |
| | | if(( rv = svsem_wait(hashtable->mutex)) != 0) { |
| | | LoggerFactory::getLogger()->error(errno, "hashtable_put\n"); |
| | | } |
| | | if(overwrite) { |
| | | _hashtable_put(hashtable, key, value); |
| | | goto suc; |
| | | } |
| | | val = _hashtable_get(hashtable, key); |
| | | // val = 1是allockey的情况 |
| | | if(val != NULL && val != (void *)1) |
| | | goto fail; |
| | | |
| | | _hashtable_put(hashtable, key, value); |
| | | |
| | | suc: |
| | | if(( rv = svsem_post(hashtable->mutex)) != 0) { |
| | | LoggerFactory::getLogger()->error(errno, "hashtable_put\n"); |
| | | } |
| | | return true; |
| | | |
| | | fail: |
| | | if(( rv = svsem_post(hashtable->mutex)) != 0) { |
| | | LoggerFactory::getLogger()->error(errno, "hashtable_put\n"); |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | |
| | | |
| | | int hashtable_alloc_key(hashtable_t *hashtable) { |
| | | int rv; |
| | | int key = START_KEY; |
| | | rv = svsem_wait(hashtable->mutex); |
| | | if(rv != 0) { |
| | | LoggerFactory::getLogger()->error(errno, "hashtable_alloc_key\n"); |
| | | } |
| | | |
| | | while(_hashtable_get(hashtable, key) != NULL) { |
| | | key++; |
| | | } |
| | | // 占用key |
| | | _hashtable_put(hashtable, key, (void *)1); |
| | | |
| | | rv = svsem_post(hashtable->mutex); |
| | | if(rv != 0) { |
| | | LoggerFactory::getLogger()->error(errno, "hashtable_alloc_key\n"); |
| | | } |
| | | return key; |
| | | } |
| | | |
| | | static inline void _hashtable_foreach(hashtable_t *hashtable, std::function<void(int, void *)> cb) { |
| | | tailq_entry_t *item; |
| | |
| | | } |
| | | |
| | | |
| | | int hashtable_alloc_key(hashtable_t *hashtable) { |
| | | int rv; |
| | | int key = START_KEY; |
| | | rv = svsem_wait(hashtable->mutex); |
| | | if(rv != 0) { |
| | | LoggerFactory::getLogger()->error(errno, "hashtable_alloc_key\n"); |
| | | } |
| | | |
| | | while(_hashtable_get(hashtable, key) != NULL) { |
| | | key++; |
| | | } |
| | | // 占用key |
| | | _hashtable_put(hashtable, key, (void *)1); |
| | | int hashtable_lock(hashtable_t *hashtable) { |
| | | return svsem_wait(hashtable->mutex); |
| | | } |
| | | |
| | | rv = svsem_post(hashtable->mutex); |
| | | if(rv != 0) { |
| | | LoggerFactory::getLogger()->error(errno, "hashtable_alloc_key\n"); |
| | | } |
| | | return key; |
| | | int hashtable_unlock(hashtable_t *hashtable) { |
| | | return svsem_post(hashtable->mutex); |
| | | } |
| | | |
| | | |