From 3f202d2e6dd26ce4867e19921ab7585fa779c096 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?R=C3=A9mi=20Denis-Courmont?= <rdenis@simphalempin.com>
Date: Tue, 12 Aug 2008 20:18:07 +0300
Subject: [PATCH] Decrease thread count after join rather than before exit

---
 src/misc/threads.c | 20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)

diff --git a/src/misc/threads.c b/src/misc/threads.c
index 06e85a3116..36947ea84e 100644
--- a/src/misc/threads.c
+++ b/src/misc/threads.c
@@ -465,15 +465,6 @@ static THREAD_RTYPE thread_entry (void *data)
     func (obj);
     msg_Dbg (obj, "thread ended");
 
-    libvlc_priv_t *libpriv = libvlc_priv (obj->p_libvlc);
-    vlc_mutex_lock (&libpriv->threads_lock);
-#ifndef NDEBUG
-    libpriv->threads_count--;
-#else
-    if (--libpriv->threads_count == 0)
-#endif
-        vlc_cond_signal (&libpriv->threads_wait);
-    vlc_mutex_unlock (&libpriv->threads_lock);
     return THREAD_RVAL;
 }
 
@@ -775,8 +766,19 @@ error:
                          (unsigned long)p_priv->thread_id, psz_file, i_line );
     }
     else
+    {
+        libvlc_priv_t *libpriv = libvlc_priv (p_this->p_libvlc);
         msg_Dbg( p_this, "thread %lu joined (%s:%d)",
                          (unsigned long)p_priv->thread_id, psz_file, i_line );
+        vlc_mutex_lock (&libpriv->threads_lock);
+#ifndef NDEBUG
+        libpriv->threads_count--;
+#else
+        if (--libpriv->threads_count == 0)
+#endif
+            vlc_cond_signal (&libpriv->threads_wait);
+        vlc_mutex_unlock (&libpriv->threads_lock);
+    }
 
     p_priv->b_thread = false;
 }
-- 
2.25.4