diff --git a/libs/libks/src/include/ks_hash.h b/libs/libks/src/include/ks_hash.h index 09ff5dbb3a..e651d5e7f1 100644 --- a/libs/libks/src/include/ks_hash.h +++ b/libs/libks/src/include/ks_hash.h @@ -95,7 +95,8 @@ typedef enum { KS_HASH_FLAG_FREE_KEY = (1 << 1), KS_HASH_FLAG_FREE_VALUE = (1 << 2), KS_HASH_FLAG_RWLOCK = (1 << 3), - KS_HASH_FLAG_DUP_CHECK = (1 << 4) + KS_HASH_FLAG_DUP_CHECK = (1 << 4), + KS_HASH_FLAG_NOLOCK = (1 << 5) } ks_hash_flag_t; #define KS_HASH_FREE_BOTH KS_HASH_FLAG_FREE_KEY | KS_HASH_FLAG_FREE_VALUE @@ -134,7 +135,7 @@ ks_hash_create_ex(ks_hash_t **hp, unsigned int minsize, * @param h the ks_hash to insert into * @param k the key - ks_hash claims ownership and will free on removal * @param v the value - does not claim ownership - * @return non-zero for successful insertion + * @return KS_STATUS_SUCCESS for successful insertion * * This function will cause the table to expand if the insertion would take * the ratio of entries to table size over the maximum load factor. @@ -147,7 +148,7 @@ ks_hash_create_ex(ks_hash_t **hp, unsigned int minsize, */ -KS_DECLARE(int) ks_hash_insert_ex(ks_hash_t *h, void *k, void *v, ks_hash_flag_t flags, ks_hash_destructor_t destructor); +KS_DECLARE(ks_status_t) ks_hash_insert_ex(ks_hash_t *h, void *k, void *v, ks_hash_flag_t flags, ks_hash_destructor_t destructor); #define ks_hash_insert(_h, _k, _v) ks_hash_insert_ex(_h, _k, _v, 0, NULL) #define DEFINE_KS_HASH_INSERT(fnname, keytype, valuetype) \ diff --git a/libs/libks/src/ks_hash.c b/libs/libks/src/ks_hash.c index 4fd611a1be..cdab4d0dfb 100644 --- a/libs/libks/src/ks_hash.c +++ b/libs/libks/src/ks_hash.c @@ -211,7 +211,11 @@ ks_hash_create_ex(ks_hash_t **hp, unsigned int minsize, } if (flags == KS_HASH_FLAG_DEFAULT) { - flags = KS_HASH_FLAG_FREE_KEY | KS_HASH_FLAG_DUP_CHECK | KS_HASH_FLAG_RWLOCK; + flags = KS_HASH_FLAG_FREE_KEY | KS_HASH_FLAG_DUP_CHECK | KS_HASH_FLAG_NOLOCK; + } + + if ((flags & KS_HASH_FLAG_NOLOCK)) { + flags &= ~KS_HASH_FLAG_RWLOCK; } ks_assert(pool); @@ -240,7 +244,9 @@ ks_hash_create_ex(ks_hash_t **hp, unsigned int minsize, ks_rwl_create(&h->rwl, h->pool); } - ks_mutex_create(&h->mutex, KS_MUTEX_FLAG_DEFAULT, h->pool); + if (!(flags & KS_HASH_FLAG_NOLOCK)) { + ks_mutex_create(&h->mutex, KS_MUTEX_FLAG_DEFAULT, h->pool); + } if (NULL == h) abort(); /*oom*/ @@ -377,7 +383,7 @@ static void * _ks_hash_remove(ks_hash_t *h, void *k, unsigned int hashvalue, uns } /*****************************************************************************/ -KS_DECLARE(int) +KS_DECLARE(ks_status_t) ks_hash_insert_ex(ks_hash_t *h, void *k, void *v, ks_hash_flag_t flags, ks_hash_destructor_t destructor) { struct entry *e; @@ -404,7 +410,6 @@ ks_hash_insert_ex(ks_hash_t *h, void *k, void *v, ks_hash_flag_t flags, ks_hash_ index = indexFor(h->tablelength, hashvalue); } e = (struct entry *)ks_pool_alloc(h->pool, sizeof(struct entry)); - if (NULL == e) { --(h->entrycount); return 0; } /*oom*/ e->h = hashvalue; e->k = k; e->v = v; @@ -415,13 +420,15 @@ ks_hash_insert_ex(ks_hash_t *h, void *k, void *v, ks_hash_flag_t flags, ks_hash_ ks_hash_write_unlock(h); - return -1; + return KS_STATUS_SUCCESS; } KS_DECLARE(void) ks_hash_write_lock(ks_hash_t *h) { - if ((h->flags & KS_HASH_FLAG_RWLOCK)) { + if ((h->flags & KS_HASH_FLAG_NOLOCK)) { + return; + } else if ((h->flags & KS_HASH_FLAG_RWLOCK)) { ks_rwl_write_lock(h->rwl); } else { ks_mutex_lock(h->mutex); @@ -430,7 +437,9 @@ KS_DECLARE(void) ks_hash_write_lock(ks_hash_t *h) KS_DECLARE(void) ks_hash_write_unlock(ks_hash_t *h) { - if ((h->flags & KS_HASH_FLAG_RWLOCK)) { + if ((h->flags & KS_HASH_FLAG_NOLOCK)) { + return; + } else if ((h->flags & KS_HASH_FLAG_RWLOCK)) { ks_rwl_write_unlock(h->rwl); } else { ks_mutex_unlock(h->mutex); @@ -565,8 +574,6 @@ KS_DECLARE(void) ks_hash_last(ks_hash_iterator_t **iP) { ks_hash_iterator_t *i = *iP; - //ks_assert(i->locked != KS_READLOCKED || (i->h->flags & KS_HASH_FLAG_RWLOCK)); - if (i->locked == KS_READLOCKED) { ks_mutex_lock(i->h->mutex); i->h->readers--;