Fix bugs in posix thread-id calculation when sizeof(pthread_t) != sizeof(long)

When pthread_t was smaller, our calculated thread IDs would include
uninitialized RAM, and so our unit tests would fail because thread_ids
would never match one another.

When pthread_t was larger and alignment was big-endian, our calculated
thread IDs would only have the most significant bytes of the
pthread_t, when in practice all the entropy is in the low-order bytes.

Found with help from Dagobert Michelsen.
This commit is contained in:
Nick Mathewson 2010-10-26 12:09:20 -04:00
parent ac1931ac3d
commit fbaf0770a7
2 changed files with 13 additions and 1 deletions

View File

@ -529,6 +529,10 @@ if test x$bwin32 != xtrue && test "$enable_thread_support" != "no"; then
[Define if we have pthreads on this system])
have_pthreads=yes])
CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
AC_CHECK_SIZEOF(pthread_t, ,
[AC_INCLUDES_DEFAULT()
#include <pthread.h> ]
)
fi
AM_CONDITIONAL(PTHREADS, [test "$have_pthreads" != "no" && test "$enable_thread_support" != "no"])

View File

@ -33,6 +33,7 @@ struct event_base;
#include <event2/thread.h>
#include <stdlib.h>
#include <string.h>
#include "mm-internal.h"
#include "evthread-internal.h"
@ -84,10 +85,17 @@ evthread_posix_get_id(void)
{
union {
pthread_t thr;
#if _EVENT_SIZEOF_PTHREAD_T > _EVENT_SIZEOF_LONG
ev_uint64_t id;
#else
unsigned long id;
#endif
} r;
#if _EVENT_SIZEOF_PTHREAD_T < _EVENT_SIZEOF_LONG
memset(&r, 0, sizeof(r));
#endif
r.thr = pthread_self();
return r.id;
return (unsigned long)r.id;
}
static void *