// ----------------------------------------------------------------------------
// dictionary locks
-static inline void dictionary_atomic_updates_lock(DICTIONARY *dict) {
- if(likely(!(dict->flags & DICTIONARY_FLAG_SINGLE_THREADED))) {
- // debug(D_DICTIONARY, "Dictionary ATOMIC lock");
- pthread_mutex_lock(&dict->atomic_mutex);
- }
-}
-
-static inline void dictionary_atomic_updates_unlock(DICTIONARY *dict) {
- if(likely(!(dict->flags & DICTIONARY_FLAG_SINGLE_THREADED))) {
- // debug(D_DICTIONARY, "Dictionary ATOMIC unlock");
- pthread_mutex_unlock(&dict->atomic_mutex);
- }
-}
-
static inline void dictionary_read_lock(DICTIONARY *dict) {
if(likely(!(dict->flags & DICTIONARY_FLAG_SINGLE_THREADED))) {
// debug(D_DICTIONARY, "Dictionary READ lock");
// ----------------------------------------------------------------------------
// avl index
-static int name_value_iterator(avl *a) { if(a) {}; return 0; }
-
static int name_value_compare(void* a, void* b) {
if(((NAME_VALUE *)a)->hash < ((NAME_VALUE *)b)->hash) return -1;
else if(((NAME_VALUE *)a)->hash > ((NAME_VALUE *)b)->hash) return 1;
#define dictionary_name_value_index_del_nolock(dict, nv) do { (dict)->deletes++; avl_remove(&(dict->values_index), (avl *)(nv)); } while(0)
static inline NAME_VALUE *dictionary_name_value_index_find_nolock(DICTIONARY *dict, const char *name, uint32_t hash) {
- NAME_VALUE *result = NULL, tmp;
+ NAME_VALUE tmp;
tmp.hash = (hash)?hash:simple_hash(name);
tmp.name = (char *)name;
dict->searches++;
- avl_search(&(dict->values_index), (avl *)&tmp, name_value_iterator, (avl **)&result);
-
- return result;
+ return (NAME_VALUE *)avl_search(&(dict->values_index), (avl *) &tmp);
}
// ----------------------------------------------------------------------------
avl_init(&dict->values_index, name_value_compare);
pthread_rwlock_init(&dict->rwlock, NULL);
- pthread_mutex_init(&dict->atomic_mutex, NULL);
dict->flags = flags;
void dictionary_destroy(DICTIONARY *dict) {
debug(D_DICTIONARY, "Destroying dictionary.");
- dictionary_atomic_updates_lock(dict);
-
dictionary_write_lock(dict);
while(dict->values_index.root)
dictionary_unlock(dict);
- dictionary_atomic_updates_unlock(dict);
-
free(dict);
}
uint32_t hash = simple_hash(name);
- dictionary_atomic_updates_lock(dict);
+ dictionary_write_lock(dict);
- dictionary_read_lock(dict);
NAME_VALUE *nv = dictionary_name_value_index_find_nolock(dict, name, hash);
- dictionary_unlock(dict);
-
if(unlikely(!nv)) {
debug(D_DICTIONARY, "Dictionary entry with name '%s' not found. Creating a new one.", name);
- dictionary_write_lock(dict);
nv = dictionary_name_value_create_nolock(dict, name, value, value_len, hash);
- dictionary_unlock(dict);
-
if(unlikely(!nv))
fatal("Cannot create name_value.");
}
}
}
- dictionary_atomic_updates_unlock(dict);
+ dictionary_unlock(dict);
return nv->value;
}
debug(D_DICTIONARY, "DEL dictionary entry with name '%s'.", name);
- dictionary_atomic_updates_lock(dict);
+ dictionary_write_lock(dict);
- dictionary_read_lock(dict);
NAME_VALUE *nv = dictionary_name_value_index_find_nolock(dict, name, 0);
- dictionary_unlock(dict);
-
if(unlikely(!nv)) {
debug(D_DICTIONARY, "Not found dictionary entry with name '%s'.", name);
ret = -1;
}
else {
debug(D_DICTIONARY, "Found dictionary entry with name '%s'.", name);
- dictionary_write_lock(dict);
dictionary_name_value_destroy_nolock(dict, nv);
- dictionary_unlock(dict);
ret = 0;
}
- dictionary_atomic_updates_unlock(dict);
+ dictionary_unlock(dict);
return ret;
}