[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

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



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 redhat com    +1-801-349-2682
Libvirt virtualization library http://libvirt.org

Attachment: signature.asc
Description: OpenPGP digital signature


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]