return (gint)w2 - w1;
}
+/**
+ * Set counter for a symbol
+ */
+static double
+rspamd_set_counter (struct cache_item *item, guint32 value)
+{
+ struct counter_data *cd;
+ double alpha;
+ cd = item->cd;
+
+ /* Calculate new value */
+ rspamd_mempool_lock_mutex (item->mtx);
+
+ alpha = 2. / (++cd->number + 1);
+ cd->value = cd->value * (1. - alpha) + value * alpha;
+
+ rspamd_mempool_unlock_mutex (item->mtx);
+
+ return cd->value;
+}
+
static GChecksum *
get_mem_cksum (struct symbols_cache *cache)
{
item = rspamd_mempool_alloc0 (pcache->static_pool,
sizeof (struct cache_item));
item->s =
- rspamd_mempool_alloc0 (pcache->static_pool,
+ rspamd_mempool_alloc0_shared (pcache->static_pool,
sizeof (struct saved_cache_item));
+ item->cd = rspamd_mempool_alloc0_shared (pcache->static_pool,
+ sizeof (struct counter_data));
+
+ item->mtx = rspamd_mempool_get_mutex (pcache->static_pool);
+
rspamd_strlcpy (item->s->symbol, name, sizeof (item->s->symbol));
item->func = func;
item->user_data = user_data;
pcache->used_items++;
g_hash_table_insert (pcache->items_by_symbol, item->s->symbol, item);
msg_debug ("used items: %d, added symbol: %s", (*cache)->used_items, name);
- rspamd_set_counter (item->s->symbol, 0);
+ rspamd_set_counter (item, 0);
*target = g_list_prepend (*target, item);
}
pcache->used_items++;
msg_debug ("used items: %d, added symbol: %s", (*cache)->used_items, name);
- rspamd_set_counter (item->s->symbol, 0);
+ rspamd_set_counter (item, 0);
g_hash_table_insert (pcache->items_by_symbol, item->s->symbol, item);
return FALSE;
}
- /* Init locking */
- cache->lock = rspamd_mempool_get_rwlock (pool);
-
cache->cfg = cfg;
/* Just in-memory cache */
}
if (cache->uses++ >= MAX_USES) {
msg_info ("resort symbols cache");
- rspamd_mempool_wlock_rwlock (cache->lock);
cache->uses = 0;
/* Resort while having write lock */
post_cache_init (cache);
- rspamd_mempool_wunlock_rwlock (cache->lock);
}
s =
rspamd_mempool_alloc0 (task->task_pool,
diff =
(tv2.tv_sec - tv1.tv_sec) * 1000000 + (tv2.tv_usec - tv1.tv_usec);
#endif
- item->s->avg_time = rspamd_set_counter (item->s->symbol, diff);
+ item->s->avg_time = rspamd_set_counter (item, diff);
}
s->saved_item = item;
return NULL;
}
-
-double
-rspamd_set_counter (const gchar *name, guint32 value)
-{
- struct counter_data *cd;
- double alpha;
- gchar *key;
-
- cd = rspamd_hash_lookup (rspamd_main->counters, (gpointer) name);
-
- if (cd == NULL) {
- cd =
- rspamd_mempool_alloc_shared (rspamd_main->counters->pool,
- sizeof (struct counter_data));
- cd->value = value;
- cd->number = 0;
- key = rspamd_mempool_strdup_shared (rspamd_main->counters->pool, name);
- rspamd_hash_insert (rspamd_main->counters, (gpointer) key,
- (gpointer) cd);
- }
- else {
- /* Calculate new value */
- rspamd_mempool_wlock_rwlock (rspamd_main->counters->lock);
-
- alpha = 2. / (++cd->number + 1);
- cd->value = cd->value * (1. - alpha) + value * alpha;
-
- rspamd_mempool_wunlock_rwlock (rspamd_main->counters->lock);
- }
-
- return cd->value;
-}
-
sig_atomic_t wanna_die = 0;
#ifndef HAVE_SA_SIGINFO