[pulseaudio-discuss] [PATCH RFCv3 22/51] once: Inline functions

Peter Meerwald pmeerw at pmeerw.net
Tue Nov 4 15:26:17 PST 2014


From: Peter Meerwald <p.meerwald at bct-electronic.com>

v2: (thanks David Henningsson)
* keep attribute order
* rename function

Signed-off-by: Peter Meerwald <pmeerw at pmeerw.net>
---
 src/pulsecore/once.c | 18 +-----------------
 src/pulsecore/once.h | 25 ++++++++++++++++++++++---
 2 files changed, 23 insertions(+), 20 deletions(-)

diff --git a/src/pulsecore/once.c b/src/pulsecore/once.c
index 16059c3..066c24c 100644
--- a/src/pulsecore/once.c
+++ b/src/pulsecore/once.c
@@ -30,14 +30,9 @@
 /* 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) {
+bool pa_once_begin_internal(pa_once *control) {
     pa_mutex *m;
 
-    pa_assert(control);
-
-    if (pa_atomic_load(&control->done))
-        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. */
@@ -64,14 +59,3 @@ void pa_once_end(pa_once *control) {
     m = pa_static_mutex_get(&control->mutex, false, false);
     pa_mutex_unlock(m);
 }
-
-/* Not reentrant -- how could it be? */
-void pa_run_once(pa_once *control, pa_once_func_t func) {
-    pa_assert(control);
-    pa_assert(func);
-
-    if (pa_once_begin(control)) {
-        func();
-        pa_once_end(control);
-    }
-}
diff --git a/src/pulsecore/once.h b/src/pulsecore/once.h
index 460a700..be7443e 100644
--- a/src/pulsecore/once.h
+++ b/src/pulsecore/once.h
@@ -33,11 +33,20 @@ typedef struct pa_once {
 #define PA_ONCE_INIT                                                    \
     {                                                                   \
         .mutex = PA_STATIC_MUTEX_INIT,                                  \
-        .done = PA_ATOMIC_INIT(0)                                       \
+        .done = PA_ATOMIC_INIT(0),                                      \
     }
 
 /* Not to be called directly, use the macros defined below instead */
-bool pa_once_begin(pa_once *o);
+bool pa_once_begin_internal(pa_once *control);
+
+static inline bool pa_once_begin(pa_once *control) {
+    pa_assert(control);
+
+    if (pa_atomic_load(&control->done))
+        return false;
+
+    return pa_once_begin_internal(control);
+}
 void pa_once_end(pa_once *o);
 
 #define PA_ONCE_BEGIN                                                   \
@@ -68,6 +77,16 @@ void pa_once_end(pa_once *o);
 
 /* Same API but calls a function */
 typedef void (*pa_once_func_t) (void);
-void pa_run_once(pa_once *o, pa_once_func_t f);
+
+/* Not reentrant -- how could it be? */
+static inline void pa_run_once(pa_once *control, pa_once_func_t func) {
+    pa_assert(control);
+    pa_assert(func);
+
+    if (pa_once_begin(control)) {
+        func();
+        pa_once_end(control);
+    }
+}
 
 #endif
-- 
1.9.1



More information about the pulseaudio-discuss mailing list