Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
do not force the O2 parameter inside SMPI scripts
[simgrid.git] / src / xbt / mallocator.c
index 469c435..1088355 100644 (file)
@@ -42,27 +42,24 @@ XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_mallocator, xbt, "Mallocators");
  * mallocators should be protected from concurrent accesses.  */
 static int initialization_done = 0;
 
-static XBT_INLINE void lock_create(xbt_mallocator_t m)
+static XBT_INLINE void lock_reset(xbt_mallocator_t m)
 {
-  m->mutex = initialization_done > 1 ? xbt_os_mutex_init() : NULL;
-}
-
-static XBT_INLINE void lock_destroy(xbt_mallocator_t m)
-{
-  if (m->mutex)
-    xbt_os_mutex_destroy(m->mutex);
+  m->lock = 0;
 }
 
 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)
 {
-  if (m->mutex)
-    xbt_os_mutex_release(m->mutex);
+  if (initialization_done > 1)
+    __sync_lock_release(&m->lock);
 }
 
 /**
@@ -146,7 +143,6 @@ void xbt_mallocator_free(xbt_mallocator_t m)
     m->free_f(m->objects[i]);
   }
   xbt_free(m->objects);
-  lock_destroy(m);
   xbt_free(m);
 }
 
@@ -194,7 +190,7 @@ void *xbt_mallocator_get(xbt_mallocator_t m)
     if (xbt_mallocator_is_active()) {
       // We have to switch this mallocator from inactive to active (and then get an object)
       m->objects = xbt_new0(void *, m->max_size);
-      lock_create(m);
+      lock_reset(m);
       return xbt_mallocator_get(m);
     } else {
       object = m->new_f();
@@ -241,7 +237,7 @@ void xbt_mallocator_release(xbt_mallocator_t m, void *object)
     if (xbt_mallocator_is_active()) {
       // We have to switch this mallocator from inactive to active (and then store that object)
       m->objects = xbt_new0(void *, m->max_size);
-      lock_create(m);
+      lock_reset(m);
       xbt_mallocator_release(m,object);
     } else {
       m->free_f(object);