[Android] java_event_thread: safer thread create/destroy

Thomas Guillem git at videolan.org
Thu Mar 5 17:47:21 CET 2015


vlc-ports/android | branch: master | Thomas Guillem <thomas at gllm.fr> | Thu Mar  5 17:43:50 2015 +0100| [d0e98ade8e7ef2ad65b4e2dceecac0a523f8933c] | committer: Thomas Guillem

java_event_thread: safer thread create/destroy

> http://git.videolan.org/gitweb.cgi/vlc-ports/android.git/?a=commit;h=d0e98ade8e7ef2ad65b4e2dceecac0a523f8933c
---

 libvlc/jni/java_event_thread.c |   26 ++++++++++++++++++--------
 1 file changed, 18 insertions(+), 8 deletions(-)

diff --git a/libvlc/jni/java_event_thread.c b/libvlc/jni/java_event_thread.c
index 83021c3..06e4e21 100644
--- a/libvlc/jni/java_event_thread.c
+++ b/libvlc/jni/java_event_thread.c
@@ -134,12 +134,20 @@ JavaEventThread_create(jweak jweak, jobject jweakCompat, bool b_sync)
     pthread_cond_init(&p_java_event_thread->cond, NULL);
     TAILQ_INIT(&p_java_event_thread->queue);
 
+    pthread_mutex_lock(&p_java_event_thread->lock);
     p_java_event_thread->jweak = jweak;
     p_java_event_thread->jweakCompat = jweakCompat;
     p_java_event_thread->b_run = true;
     p_java_event_thread->b_sync = b_sync;
-    pthread_create(&p_java_event_thread->thread, NULL,
-                   JavaEventThread_thread, p_java_event_thread);
+    if (pthread_create(&p_java_event_thread->thread, NULL,
+                       JavaEventThread_thread, p_java_event_thread) != 0)
+    {
+        p_java_event_thread->b_run = false;
+        pthread_mutex_unlock(&p_java_event_thread->lock);
+        JavaEventThread_destroy(p_java_event_thread);
+        p_java_event_thread = NULL;
+    } else
+        pthread_mutex_unlock(&p_java_event_thread->lock);
 
     return p_java_event_thread;
 }
@@ -148,12 +156,14 @@ void
 JavaEventThread_destroy(java_event_thread *p_java_event_thread)
 {
     pthread_mutex_lock(&p_java_event_thread->lock);
-    p_java_event_thread->b_run = false;
-
-    pthread_cond_signal(&p_java_event_thread->cond);
-    pthread_mutex_unlock(&p_java_event_thread->lock);
-
-    pthread_join(p_java_event_thread->thread, NULL);
+    if (p_java_event_thread->b_run)
+    {
+        p_java_event_thread->b_run = false;
+        pthread_cond_signal(&p_java_event_thread->cond);
+        pthread_mutex_unlock(&p_java_event_thread->lock);
+        pthread_join(p_java_event_thread->thread, NULL);
+    } else
+        pthread_mutex_unlock(&p_java_event_thread->lock);
 
     pthread_mutex_destroy(&p_java_event_thread->lock);
     pthread_cond_destroy(&p_java_event_thread->cond);



More information about the Android mailing list