From e4bc372e1b911afc55b4e72afc30848005691210 Mon Sep 17 00:00:00 2001 From: Eric Blake Date: Sat, 4 Dec 2010 14:33:23 -0700 Subject: [PATCH] threads: add virThreadID for debugging use * src/util/threads.h (virThreadID): New prototype. * src/util/threads-pthread.c (virThreadID): New function. * src/util/threads-win32.c (virThreadID): Likewise. * src/libvirt_private.syms (threads.h): Export it. * daemon/event.c (virEventInterruptLocked): Use it to avoid warning on BSD systems. --- daemon/event.c | 3 ++- src/libvirt_private.syms | 1 + src/util/threads-pthread.c | 11 +++++++++++ src/util/threads-win32.c | 7 +++++++ src/util/threads.h | 6 ++++++ 5 files changed, 27 insertions(+), 1 deletion(-) diff --git a/daemon/event.c b/daemon/event.c index 0920748d19..01cb674459 100644 --- a/daemon/event.c +++ b/daemon/event.c @@ -653,7 +653,8 @@ static int virEventInterruptLocked(void) if (!eventLoop.running || virThreadIsSelf(&eventLoop.leader)) { - VIR_DEBUG("Skip interrupt, %d %d", eventLoop.running, (int)eventLoop.leader.thread); + VIR_DEBUG("Skip interrupt, %d %d", eventLoop.running, + virThreadID(&eventLoop.leader)); return 0; } diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index aaf48ab82e..e2def6c6e2 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -773,6 +773,7 @@ virMutexInitRecursive; virMutexLock; virMutexUnlock; virThreadCreate; +virThreadID; virThreadIsSelf; virThreadJoin; virThreadSelf; diff --git a/src/util/threads-pthread.c b/src/util/threads-pthread.c index bff49797bc..c406f27de0 100644 --- a/src/util/threads-pthread.c +++ b/src/util/threads-pthread.c @@ -22,6 +22,7 @@ #include #include +#include #if HAVE_SYS_SYSCALL_H # include #endif @@ -186,6 +187,8 @@ bool virThreadIsSelf(virThreadPtr thread) return pthread_equal(pthread_self(), thread->thread) ? true : false; } +/* For debugging use only; this result is not guaranteed unique on BSD + * systems when pthread_t is a 64-bit pointer. */ int virThreadSelfID(void) { #if defined(HAVE_SYS_SYSCALL_H) && defined(SYS_gettid) @@ -197,6 +200,14 @@ int virThreadSelfID(void) #endif } +/* For debugging use only; this result is not guaranteed unique on BSD + * systems when pthread_t is a 64-bit pointer, nor does it match the + * thread id of virThreadSelfID on Linux. */ +int virThreadID(virThreadPtr thread) +{ + return (int)(uintptr_t)thread->thread; +} + void virThreadJoin(virThreadPtr thread) { pthread_join(thread->thread, NULL); diff --git a/src/util/threads-win32.c b/src/util/threads-win32.c index 436b3bd31d..ddb4737c56 100644 --- a/src/util/threads-win32.c +++ b/src/util/threads-win32.c @@ -299,11 +299,18 @@ bool virThreadIsSelf(virThreadPtr thread) return self.thread == thread->thread ? true : false; } +/* For debugging use only; see comments in threads-pthread.c. */ int virThreadSelfID(void) { return (int)GetCurrentThreadId(); } +/* For debugging use only; see comments in threads-pthread.c. */ +int virThreadID(virThreadPtr thread) +{ + return (int)thread->thread; +} + void virThreadJoin(virThreadPtr thread) { diff --git a/src/util/threads.h b/src/util/threads.h index fa00a91014..35e319e6d5 100644 --- a/src/util/threads.h +++ b/src/util/threads.h @@ -51,7 +51,13 @@ int virThreadCreate(virThreadPtr thread, void virThreadSelf(virThreadPtr thread); bool virThreadIsSelf(virThreadPtr thread); void virThreadJoin(virThreadPtr thread); + +/* These next two functions are for debugging only, since they are not + * guaranteed to give unique values for distinct threads on all + * architectures, nor are the two functions guaranteed to give the same + * value for the same thread. */ int virThreadSelfID(void); +int virThreadID(virThreadPtr thread); int virMutexInit(virMutexPtr m) ATTRIBUTE_RETURN_CHECK; int virMutexInitRecursive(virMutexPtr m) ATTRIBUTE_RETURN_CHECK; -- GitLab