]> code.delx.au - pulseaudio/blobdiff - src/pulsecore/once.c
remap: Change remapping function argument type from void to int16_t / float as approp...
[pulseaudio] / src / pulsecore / once.c
index 4e509e0cca0fc71af5e85b990bad87ada5178e8b..16059c34c3f4b6ff6c19e5b84c28dcd8dd7a28c5 100644 (file)
 #endif
 
 #include <pulsecore/macro.h>
-#include <pulsecore/mutex.h>
 
 #include "once.h"
 
-pa_bool_t pa_once_begin(pa_once *control) {
+/* See http://www.hpl.hp.com/research/linux/atomic_ops/example.php4 for the
+ * reference algorithm used here. */
+
+bool pa_once_begin(pa_once *control) {
+    pa_mutex *m;
+
     pa_assert(control);
 
     if (pa_atomic_load(&control->done))
-        return FALSE;
-
-    pa_atomic_inc(&control->ref);
+        return false;
 
     /* Caveat: We have to make sure that the once func has completed
      * before returning, even if the once func is not actually
      * executed by us. Hence the awkward locking. */
 
-    for (;;) {
-        pa_mutex *m;
-
-        if ((m = pa_atomic_ptr_load(&control->mutex))) {
-
-            /* The mutex is stored in locked state, hence let's just
-             * wait until it is unlocked */
-            pa_mutex_lock(m);
-
-            pa_assert(pa_atomic_load(&control->done));
-
-            pa_once_end(control);
-            return FALSE;
-        }
-
-        pa_assert_se(m = pa_mutex_new(FALSE, FALSE));
-        pa_mutex_lock(m);
-
-        if (pa_atomic_ptr_cmpxchg(&control->mutex, NULL, m))
-            return TRUE;
+    m = pa_static_mutex_get(&control->mutex, false, false);
+    pa_mutex_lock(m);
 
+    if (pa_atomic_load(&control->done)) {
         pa_mutex_unlock(m);
-        pa_mutex_free(m);
+        return false;
     }
+
+    return true;
 }
 
 void pa_once_end(pa_once *control) {
@@ -71,15 +58,11 @@ void pa_once_end(pa_once *control) {
 
     pa_assert(control);
 
+    pa_assert(!pa_atomic_load(&control->done));
     pa_atomic_store(&control->done, 1);
 
-    pa_assert_se(m = pa_atomic_ptr_load(&control->mutex));
+    m = pa_static_mutex_get(&control->mutex, false, false);
     pa_mutex_unlock(m);
-
-    if (pa_atomic_dec(&control->ref) <= 1) {
-        pa_assert_se(pa_atomic_ptr_cmpxchg(&control->mutex, m, NULL));
-        pa_mutex_free(m);
-    }
 }
 
 /* Not reentrant -- how could it be? */