]> source.dussan.org Git - rspamd.git/commitdiff
* Add simple locking interface to mem_pool library
authorVsevolod Stakhov <vsevolod@rambler-co.ru>
Wed, 26 Nov 2008 16:31:26 +0000 (19:31 +0300)
committerVsevolod Stakhov <vsevolod@rambler-co.ru>
Wed, 26 Nov 2008 16:31:26 +0000 (19:31 +0300)
src/mem_pool.c
src/mem_pool.h

index f116fff7328572d67a322c3539b7bfd6cdc5126a..a074de62a80188151c77b22c364cfa00e9b44af8 100644 (file)
@@ -240,9 +240,9 @@ memory_pool_find_pool (memory_pool_t *pool, void *pointer)
 }
 
 static void
-memory_pool_spin (struct _pool_chain_shared *chain)
+memory_pool_spin (gint *mutex)
 {
-       while (!g_atomic_int_compare_and_exchange (&chain->lock, 0, 1)) {
+       while (!g_atomic_int_compare_and_exchange (mutex, 0, 1)) {
                /* lock was aqquired */
 #ifdef HAVE_NANOSLEEP
                struct timespec ts;
@@ -271,7 +271,7 @@ memory_pool_lock_shared (memory_pool_t *pool, void *pointer)
                return;
        }
        
-       memory_pool_spin (chain);
+       memory_pool_spin (&chain->lock);
 }
 
 void memory_pool_unlock_shared (memory_pool_t *pool, void *pointer)
@@ -355,6 +355,31 @@ memory_pool_get_size ()
 #endif
 }
 
+gint* 
+memory_pool_get_mutex (memory_pool_t *pool)
+{
+       gint *res;
+       if (pool != NULL) {
+               res = memory_pool_alloc_shared (pool, sizeof (gint));
+               /* Initialize unlocked */
+               *res = 0;
+               return res;
+       }
+       return NULL;
+}
+
+void 
+memory_pool_lock_mutex (gint *mutex)
+{
+       memory_pool_spin (mutex);
+}
+
+void 
+memory_pool_unlock_mutex (gint *mutex)
+{
+       (void)g_atomic_int_dec_and_test (mutex);
+}
+
 /*
  * vi:ts=4
  */
index 4027b5de27a446d77a5479a9910e877297c34b4e..22388df703e1c10e0ef4f9da25670a16eed97ef2 100644 (file)
@@ -49,7 +49,10 @@ void memory_pool_add_destructor (memory_pool_t *pool, pool_destruct_func func, v
 void* memory_pool_alloc_shared (memory_pool_t *pool, size_t size);
 void memory_pool_lock_shared (memory_pool_t *pool, void *pointer);
 void memory_pool_unlock_shared (memory_pool_t *pool, void *pointer);
-void memory_pool_delete (memory_pool_t* pool);
+void memory_pool_delete (memory_pool_t *pool);
+gint* memory_pool_get_mutex (memory_pool_t *pool);
+void memory_pool_lock_mutex (gint *mutex);
+void memory_pool_unlock_mutex (gint *mutex);
 
 void memory_pool_stat (memory_pool_stat_t *st);