Le 2015-10-07 23:48, Luca Barbato a écrit :
---
 libavutil/thread.h | 10 ++++++++++
 1 file changed, 10 insertions(+)

It isn't exactly beautiful with `!control` used to return 0 and
suppress the unused warning, but seems to work as intended and it
is fully portable, I'd use `({ routine(); !control; })` if we assume
the construct is always supported, but I'm afraid it is not.

diff --git a/libavutil/thread.h b/libavutil/thread.h
index 3556544..d2fb847 100644
--- a/libavutil/thread.h
+++ b/libavutil/thread.h
@@ -39,6 +39,11 @@
 #define ff_mutex_unlock  pthread_mutex_unlock
 #define ff_mutex_destroy pthread_mutex_destroy

+#define AVOnce pthread_once_t
+#define AV_ONCE_INIT PTHREAD_ONCE_INIT
+
+#define ff_thread_once(control, routine) pthread_once(control, routine)
+
 #else

 #define AVMutex char
@@ -48,6 +53,11 @@
 #define ff_mutex_unlock(mutex) (0)
 #define ff_mutex_destroy(mutex) (0)

+#define AVOnce char
+#define AV_ONCE_INIT 0
+
+#define ff_thread_once(control, routine) !control; routine();

Why don't you use a static inline? I don't think you can make a semantically correct and expansion-safe pure C macro here.

This should be semantically correct, but is obviously not safe w.r.t. to evaluation of "control":

#define ff_thread_once(control, routine) \
    (*(control) == 0 ? ((routine)(), *(control) = 1, 0) \
                     : ((*(control) != 1) * EINVAL))

I'm not convinced this is very useful. The overhead of pthread_once() should be negligible when single threaded.

(And pthread-stubs is a much better solution to this "problem", as it can (de)select multithreading at run time rather than build time.)

+

 #endif

 #endif /* AVUTIL_THREAD_H */
--
2.5.0

_______________________________________________
libav-devel mailing list
[email protected]
https://lists.libav.org/mailman/listinfo/libav-devel

--
Rémi Denis-Courmont
http://www.remlab.net/
_______________________________________________
libav-devel mailing list
[email protected]
https://lists.libav.org/mailman/listinfo/libav-devel

Reply via email to