[vlc-commits] thread: make vlc_thread_self() always work
Rémi Denis-Courmont
git at videolan.org
Tue Feb 18 18:58:25 CET 2020
vlc | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Mon Feb 17 22:43:12 2020 +0200| [18dc97ddf3e5dc9f3ab53b8c30728944af8a767f] | committer: Rémi Denis-Courmont
thread: make vlc_thread_self() always work
This changes the return type of vlc_thread_self() to match (one of) the
C run-time native thread handle type. Then we add a macro to compare
handles for equality, which may be useful for self-debugging or to
implement recursive locking.
Without this, vlc_thread_self() returned NULL on non-POSIX platforms
on threads not created by vlc_clone() - such as the main thread - which
was not terribly helpful.
This function overlaps with vlc_thread_id(). But unlike the later, it
works on all platforms. There are in particular no portable ways to
implement vlc_thread_id() on POSIX Threads.
> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=18dc97ddf3e5dc9f3ab53b8c30728944af8a767f
---
include/vlc_threads.h | 36 ++++++++++++++++++++++++++++--------
src/android/thread.c | 4 ++--
src/os2/thread.c | 4 ++--
src/posix/thread.c | 5 ++---
src/win32/thread.c | 4 ++--
5 files changed, 36 insertions(+), 17 deletions(-)
diff --git a/include/vlc_threads.h b/include/vlc_threads.h
index ab9f25cb69..5db4275943 100644
--- a/include/vlc_threads.h
+++ b/include/vlc_threads.h
@@ -54,6 +54,10 @@ VLC_API void vlc_testcancel(void);
typedef struct vlc_thread *vlc_thread_t;
# define VLC_THREAD_CANCELED NULL
+
+typedef unsigned long vlc_osthread_t;
+#define vlc_thread_equal(a,b) ((a) == (b))
+
# define LIBVLC_NEED_SLEEP
typedef struct
{
@@ -100,6 +104,10 @@ static inline int vlc_poll(struct pollfd *fds, unsigned nfds, int timeout)
typedef struct vlc_thread *vlc_thread_t;
#define VLC_THREAD_CANCELED NULL
+
+typedef unsigned long vlc_osthread_t;
+#define vlc_thread_equal(a,b) ((a) == (b))
+
typedef struct
{
bool dynamic;
@@ -174,6 +182,8 @@ static inline int vlc_poll (struct pollfd *fds, unsigned nfds, int timeout)
typedef struct vlc_thread *vlc_thread_t;
#define VLC_THREAD_CANCELED NULL
+typedef pthread_t vlc_osthread_t;
+#define vlc_thread_equal(a,b) pthread_equal(a,b)
typedef pthread_mutex_t vlc_mutex_t;
#define VLC_STATIC_MUTEX PTHREAD_MUTEX_INITIALIZER
typedef pthread_once_t vlc_once_t;
@@ -219,6 +229,8 @@ static inline int vlc_poll (struct pollfd *fds, unsigned nfds, int timeout)
typedef pthread_t vlc_thread_t;
#define VLC_THREAD_CANCELED PTHREAD_CANCELED
+typedef pthread_t vlc_osthread_t;
+#define vlc_thread_equal(a,b) pthread_equal(a,b)
typedef pthread_mutex_t vlc_mutex_t;
#define VLC_STATIC_MUTEX PTHREAD_MUTEX_INITIALIZER
typedef pthread_cond_t vlc_cond_t;
@@ -264,6 +276,9 @@ typedef struct
*/
#define VLC_THREAD_CANCELED PTHREAD_CANCELED
+typedef pthread_t vlc_osthread_t;
+#define vlc_thread_equal(a,b) pthread_equal(a,b)
+
/**
* Mutex.
*
@@ -833,18 +848,21 @@ VLC_API void vlc_control_cancel(vlc_cleanup_t *);
* Thread handle.
*
* This function returns the thread handle of the calling thread.
+ * This works even if the thread was <b>not</b> created with vlc_clone().
+ * As a consequence, depending on the platform, this might or might not be the
+ * same as the @ref vlc_thread_t thread handle returned by vlc_clone().
*
- * \note The exact type of the thread handle depends on the platform,
- * including an integer type, a pointer type or a compound type of any size.
- * If you need an integer identifier, use vlc_thread_id() instead.
+ * Also depending on the platform, this might be an integer type, a pointer
+ * type, or a compound type of any (reasonable) size. To compare two thread
+ * handles, use the vlc_thread_equal() macro, not a hand-coded comparison.
+ * Comparing the calling thread for equality with another thread is in fact
+ * pretty much the only purpose of this function.
*
- * \note vlc_join(vlc_thread_self(), NULL) is undefined,
- * as it obviously does not make any sense (it might result in a deadlock, but
- * there are no warranties that it will).
+ * \note If you need an integer identifier, use vlc_thread_id() instead.
*
- * \return the thread handle
+ * \return the OS run-time thread handle
*/
-VLC_API vlc_thread_t vlc_thread_self(void) VLC_USED;
+VLC_API vlc_osthread_t vlc_thread_self(void) VLC_USED;
/**
* Thread identifier.
@@ -858,6 +876,8 @@ VLC_API vlc_thread_t vlc_thread_self(void) VLC_USED;
* There are no particular semantics to the thread ID with LibVLC.
* It is provided mainly for tracing and debugging.
*
+ * See also vlc_thread_self().
+ *
* \warning This function is not currently implemented on all supported
* platforms. Where not implemented, it returns (unsigned long)-1.
*
diff --git a/src/android/thread.c b/src/android/thread.c
index d3e8b25c47..da128a769c 100644
--- a/src/android/thread.c
+++ b/src/android/thread.c
@@ -150,9 +150,9 @@ struct vlc_thread
static thread_local struct vlc_thread *thread = NULL;
-vlc_thread_t vlc_thread_self (void)
+pthread_t vlc_thread_self(void)
{
- return thread;
+ return pthread_self();
}
void vlc_threads_setup (libvlc_int_t *p_libvlc)
diff --git a/src/os2/thread.c b/src/os2/thread.c
index f2eb435129..150b916988 100644
--- a/src/os2/thread.c
+++ b/src/os2/thread.c
@@ -674,9 +674,9 @@ int vlc_set_priority (vlc_thread_t th, int priority)
return VLC_SUCCESS;
}
-vlc_thread_t vlc_thread_self (void)
+unsigned long vlc_thread_self(void)
{
- return vlc_threadvar_get (thread_key);
+ return vlc_thread_id();
}
unsigned long vlc_thread_id (void)
diff --git a/src/posix/thread.c b/src/posix/thread.c
index 9c9271a100..4695056d07 100644
--- a/src/posix/thread.c
+++ b/src/posix/thread.c
@@ -439,10 +439,9 @@ int vlc_clone_detach (vlc_thread_t *th, void *(*entry) (void *), void *data,
return vlc_clone_attr (th, &attr, entry, data, priority);
}
-vlc_thread_t vlc_thread_self (void)
+pthread_t vlc_thread_self(void)
{
- vlc_thread_t thread = { pthread_self() };
- return thread;
+ return pthread_self();
}
VLC_WEAK unsigned long vlc_thread_id(void)
diff --git a/src/win32/thread.c b/src/win32/thread.c
index 42e8232333..66311cd3cb 100644
--- a/src/win32/thread.c
+++ b/src/win32/thread.c
@@ -499,9 +499,9 @@ int vlc_clone_detach (vlc_thread_t *p_handle, void *(*entry) (void *),
return vlc_clone_attr (p_handle, true, entry, data, priority);
}
-vlc_thread_t vlc_thread_self (void)
+unsigned long vlc_thread_self(void)
{
- return TlsGetValue(thread_key);
+ return GetCurrentThreadId();
}
unsigned long vlc_thread_id (void)
More information about the vlc-commits
mailing list