| | |
| | | #include "mm.h" |
| | | #include "sem_util.h" |
| | | #include <set> |
| | | #include <functional> |
| | | |
| | | typedef struct tailq_entry_t |
| | | { |
| | |
| | | } |
| | | |
| | | |
| | | 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; |
| | |
| | | |
| | | } |
| | | |
| | | 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; |
| | |
| | | SemUtil::dec(hashtable->wlock); |
| | | } |
| | | SemUtil::inc(hashtable->mutex); |
| | | // ================ |
| | | |
| | | void * res = _hashtable_get(hashtable, key); |
| | | |
| | | // ================== |
| | | |
| | | SemUtil::dec(hashtable->mutex); |
| | | hashtable->readcnt--; |
| | |
| | | |
| | | void hashtable_put(hashtable_t *hashtable, int key, void *value) { |
| | | SemUtil::dec(hashtable->mutex); |
| | | // 设置读优先级高 |
| | | while (hashtable->readcnt > 0) |
| | | { |
| | | SemUtil::set(hashtable->cond, 0); |
| | |
| | | |
| | | //获取读写锁 |
| | | SemUtil::dec(hashtable->wlock); |
| | | |
| | | _hashtable_put(hashtable, key, value); |
| | | //释放读写锁 |
| | | SemUtil::inc(hashtable->wlock); |
| | |
| | | |
| | | |
| | | |
| | | 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] ; |
| | |
| | | } |
| | | } |
| | | |
| | | int hashtable_alloc_key(hashtable_t *hashtable) { |
| | | int key = START_KEY; |
| | | SemUtil::dec(hashtable->wlock); |
| | | |
| | | while(_hashtable_get(hashtable, key) != NULL) { |
| | | key++; |
| | | } |
| | | 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_put(hashtable, key, (void *)1); |
| | | SemUtil::inc(hashtable->wlock); |
| | | return key; |
| | | // ================== |
| | | |
| | | _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>; |
| | | tailq_entry_t *item; |
| | |
| | | } |
| | | |
| | | |
| | | 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; |
| | | } |