Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use a sinlock instead of a mutex for mallocators.
authorArnaud Giersch <arnaud.giersch@iut-bm.univ-fcomte.fr>
Tue, 11 Dec 2012 14:13:41 +0000 (15:13 +0100)
committerArnaud Giersch <arnaud.giersch@iut-bm.univ-fcomte.fr>
Tue, 11 Dec 2012 14:13:41 +0000 (15:13 +0100)
src/xbt/mallocator.c
src/xbt/mallocator_private.h

index 469c435..3acc9b3 100644 (file)
@@ -44,25 +44,26 @@ static int initialization_done = 0;
 
 static XBT_INLINE void lock_create(xbt_mallocator_t m)
 {
 
 static XBT_INLINE void lock_create(xbt_mallocator_t m)
 {
-  m->mutex = initialization_done > 1 ? xbt_os_mutex_init() : NULL;
+  m->lock = 0;
 }
 
 static XBT_INLINE void lock_destroy(xbt_mallocator_t m)
 {
 }
 
 static XBT_INLINE void lock_destroy(xbt_mallocator_t m)
 {
-  if (m->mutex)
-    xbt_os_mutex_destroy(m->mutex);
 }
 
 static XBT_INLINE void lock_acquire(xbt_mallocator_t m)
 {
 }
 
 static XBT_INLINE void lock_acquire(xbt_mallocator_t m)
 {
-  if (m->mutex)
-    xbt_os_mutex_acquire(m->mutex);
+  if (initialization_done > 1) {
+    int *lock = &m->lock;
+    while (__sync_lock_test_and_set(lock, 1))
+      /* nop */;
+  }
 }
 
 static XBT_INLINE void lock_release(xbt_mallocator_t m)
 {
 }
 
 static XBT_INLINE void lock_release(xbt_mallocator_t m)
 {
-  if (m->mutex)
-    xbt_os_mutex_release(m->mutex);
+  if (initialization_done > 1)
+    __sync_lock_release(&m->lock);
 }
 
 /**
 }
 
 /**
index 12cccbf..e5e918c 100644 (file)
@@ -8,7 +8,7 @@
 
 #ifndef _XBT_MALLOCATOR_PRIVATE_H__
 #define _XBT_MALLOCATOR_PRIVATE_H__
 
 #ifndef _XBT_MALLOCATOR_PRIVATE_H__
 #define _XBT_MALLOCATOR_PRIVATE_H__
-#include "xbt/xbt_os_thread.h"
+
 typedef struct s_xbt_mallocator {
   void **objects;               /* objects stored by the mallocator and available for the user */
   int current_size;             /* number of objects currently stored */
 typedef struct s_xbt_mallocator {
   void **objects;               /* objects stored by the mallocator and available for the user */
   int current_size;             /* number of objects currently stored */
@@ -16,7 +16,7 @@ typedef struct s_xbt_mallocator {
   pvoid_f_void_t new_f;         /* function to call when we are running out of objects */
   void_f_pvoid_t free_f;        /* function to call when we have got too many objects */
   void_f_pvoid_t reset_f;       /* function to call when an object is released by the user */
   pvoid_f_void_t new_f;         /* function to call when we are running out of objects */
   void_f_pvoid_t free_f;        /* function to call when we have got too many objects */
   void_f_pvoid_t reset_f;       /* function to call when an object is released by the user */
-  xbt_os_mutex_t mutex;         /* mutex to ensure the mallocator is thread-safe */
+  int lock;                     /* lock to ensure the mallocator is thread-safe */
 } s_xbt_mallocator_t;
 
 #endif                          /* _XBT_MALLOCATOR_PRIVATE_H__ */
 } s_xbt_mallocator_t;
 
 #endif                          /* _XBT_MALLOCATOR_PRIVATE_H__ */