[Android] [PATCH] Android: work around pthread_once bug on Android 2.1

Edward Wang edward.c.wang at compdigitec.com
Tue Jun 26 20:45:38 CEST 2012


http://www.compdigitec.com/labs/2012/06/25/fclose-freezes-when-run-inside-pthread-on-android-2-1/
---
 Patch is against vlc.git

 src/posix/linux_cpu.c |   34 ++++++++++++++++++++++++++++++++++
 1 files changed, 34 insertions(+), 0 deletions(-)

diff --git a/src/posix/linux_cpu.c b/src/posix/linux_cpu.c
index d243424..636eba5 100644
--- a/src/posix/linux_cpu.c
+++ b/src/posix/linux_cpu.c
@@ -157,6 +157,33 @@ static void vlc_CPU_init (void)
     cpu_flags = all_caps;
 }
 
+#ifdef __ANDROID__
+#define pthread_once_t pthread_once_t_android
+#define pthread_once pthread_once_android
+#define PTHREAD_ONCE_INIT {0,{0}}
+
+typedef struct {
+    int count;
+    vlc_mutex_t lock;
+} pthread_once_t_android;
+
+int pthread_once_android(pthread_once_t_android* once_control, void (*init_routine)(void)) {
+    if( once_control == NULL || init_routine == NULL ) return -1;
+    if( once_control->count == 0 )
+        vlc_mutex_init( &(once_control->lock) );
+    vlc_mutex_lock( &(once_control->lock) );
+    int count = once_control->count;
+    if(count > 0) {
+        vlc_mutex_unlock( &(once_control->lock) );
+        return 0;
+    }
+    (*init_routine)();
+    count = 1;
+    vlc_mutex_unlock( &(once_control->lock) );
+    return 0;
+}
+#endif
+
 unsigned vlc_CPU (void)
 {
     static pthread_once_t once = PTHREAD_ONCE_INIT;
@@ -164,6 +191,13 @@ unsigned vlc_CPU (void)
     pthread_once (&once, vlc_CPU_init);
     return cpu_flags;
 }
+
+#ifdef __ANDROID__
+#undef pthread_once_t
+#undef pthread_once
+#undef PTHREAD_ONCE_INIT
+#endif
+
 #else /* CPU_FLAGS */
 unsigned vlc_CPU (void)
 {
-- 
1.7.5.4



More information about the Android mailing list