[libvirt] [PATCH] Include a thread identifier in log messages

Eric Blake eblake at redhat.com
Mon Nov 22 18:44:42 UTC 2010


On 11/22/2010 07:10 AM, Daniel P. Berrange wrote:
> To allow messages from different threads to be untangled,
> include an integer thread identifier in log messages.
> 
> * src/util/logging.c: Include thread ID
> * src/util/threads.h, src/util/threads.h, src/util/threads-pthread.c:
>   Add new virThreadSelfID() function
> * configure.ac: Check for sys/syscall.h

>  
> +int virThreadSelfID(void)
> +{
> +#if defined(HAVE_SYS_SYSCALL_H) && defined(SYS_gettid)
> +    pid_t tid;
> +    tid = syscall(SYS_gettid);
> +    return (int)tid;

Bummer that glibc doesn't export gettid(), but this looks correct to me
for Linux.

> +#else
> +    return (int)pthread_self();

According to POSIX, this is not portable: pthread_t is an opaque type
and is allowed to be a struct rather than an arithmetic type, in which
case casting to int will cause a compilation failure.  But in practice:
pthread_t is integral on Linux, Solaris, and AIX; and a pointer type on
BSD and Cygwin; therefore, the cast will probably succeed on all
architectures we might encounter, but it still might not be correct
(that is, since pthread_equal() is allowed to return true for two
distinct pointers, merely casting a pointer to int may give different
ids for the same thread).

But I'm okay waiting for an actual bug report of this failing to do the
right thing before we change this code.

-- 
Eric Blake   eblake at redhat.com    +1-801-349-2682
Libvirt virtualization library http://libvirt.org

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 619 bytes
Desc: OpenPGP digital signature
URL: <http://listman.redhat.com/archives/libvir-list/attachments/20101122/125f2396/attachment-0001.sig>


More information about the libvir-list mailing list