[vlc-devel] commit: vlc_detach: releases a thread handle asynchronously ( Rémi Denis-Courmont )

git version control git at videolan.org
Sun Aug 2 08:53:51 CEST 2009


vlc | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Sat Aug  1 23:59:41 2009 +0300| [6cecd8ecf80090d4818c5aa4a9e9f00d15d982c2] | committer: Rémi Denis-Courmont 

vlc_detach: releases a thread handle asynchronously

Note that this can only be used safely in the core. In a plug-in, it
would introduce a race whereby dlclose() unmaps the code segment that
a detached thread is running (this was discussed over a year ago).
For that, we'd need something a bit more involved along the lines of
Win32's FreeLibraryAndExitThread().

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=6cecd8ecf80090d4818c5aa4a9e9f00d15d982c2
---

 src/libvlc.h         |    3 +++
 src/misc/pthread.c   |   24 +++++++++++++++++++++++-
 src/misc/w32thread.c |    4 ++++
 3 files changed, 30 insertions(+), 1 deletions(-)

diff --git a/src/libvlc.h b/src/libvlc.h
index 15a8d21..3d15f95 100644
--- a/src/libvlc.h
+++ b/src/libvlc.h
@@ -58,6 +58,9 @@ vlc_list_t *vlc_list_find( vlc_object_t *, int, int );
  * Threads subsystem
  */
 
+/* This cannot be used as is from plugins: */
+void vlc_detach (vlc_thread_t);
+
 /* Hopefully, no need to export this. There is a new thread API instead. */
 void vlc_thread_cancel (vlc_object_t *);
 int vlc_object_waitpipe (vlc_object_t *obj);
diff --git a/src/misc/pthread.c b/src/misc/pthread.c
index 068cfa8..d3f1dbd 100644
--- a/src/misc/pthread.c
+++ b/src/misc/pthread.c
@@ -589,10 +589,10 @@ void vlc_cancel (vlc_thread_t thread_id)
  * occur.
  * @warning
  * A thread cannot join itself (normally VLC will abort if this is attempted).
+ * Also, a detached thread <b>cannot</b> be joined.
  *
  * @param handle thread handle
  * @param p_result [OUT] pointer to write the thread return value or NULL
- * @return 0 on success, a standard error code otherwise.
  */
 void vlc_join (vlc_thread_t handle, void **result)
 {
@@ -601,6 +601,28 @@ void vlc_join (vlc_thread_t handle, void **result)
 }
 
 /**
+ * Detaches a thread. When the specified thread completes, it will be
+ * automatically destroyed (in particular, its stack will be reclaimed),
+ * instead of waiting for another thread to call vlc_join(). If the thread has
+ * already completed, it will be destroyed immediately.
+ *
+ * When a thread performs some work asynchronously and may complete much
+ * earlier than it can be joined, detaching the thread can save memory.
+ * However, care must be taken that any resources used by a detached thread
+ * remains valid until the thread completes. This will typically involve some
+ * kind of thread-safe signaling.
+ *
+ * A thread may detach itself.
+ *
+ * @param handle thread handle
+ */
+void vlc_detach (vlc_thread_t handle)
+{
+    int val = pthread_detach (handle);
+    VLC_THREAD_ASSERT ("detaching thread");
+}
+
+/**
  * Save the current cancellation state (enabled or disabled), then disable
  * cancellation for the calling thread.
  * This function must be called before entering a piece of code that is not
diff --git a/src/misc/w32thread.c b/src/misc/w32thread.c
index bf806f9..5b337e4 100644
--- a/src/misc/w32thread.c
+++ b/src/misc/w32thread.c
@@ -497,6 +497,10 @@ void vlc_join (vlc_thread_t handle, void **result)
 #endif
 }
 
+void vlc_detach (vlc_thread_t handle)
+{
+    CloseHandle (handle);
+}
 
 /*** Thread cancellation ***/
 




More information about the vlc-devel mailing list