thread: Allow thread ID to be value returned by gettid()

Signed-off-by: Thomas Egerer <thomas.egerer@secunet.com>
This commit is contained in:
Thomas Egerer 2016-02-12 17:09:47 +01:00 committed by Tobias Brunner
parent f00f679af9
commit 8ea4cb3e5d
4 changed files with 36 additions and 14 deletions

View File

@ -305,6 +305,7 @@ ARG_ENABL_SET([perl-cpan-install],[enable installation of provided CPAN module.]
ARG_ENABL_SET([coverage], [enable lcov coverage report generation.])
ARG_ENABL_SET([leak-detective], [enable malloc hooks to find memory leaks.])
ARG_ENABL_SET([lock-profiler], [enable lock/mutex profiling code.])
ARG_ENABL_SET([log-thread-ids], [use thread ID, if available, instead of an incremented value starting from 1, to identify threads.])
ARG_ENABL_SET([monolithic], [build monolithic version of libstrongswan that includes all enabled plugins. Similarly, the plugins of charon are assembled in libcharon.])
# ===================================
@ -1642,6 +1643,9 @@ fi
if test x$capabilities = xlibcap -o x$capabilities = xnative; then
AC_DEFINE([CAPABILITIES], [], [capability dropping support])
fi
if test x$log_thread_ids = xtrue; then
AC_DEFINE([USE_THREAD_IDS], [], [use thread ID for thread identification, if available])
fi
if test x$monolithic = xtrue; then
AC_DEFINE([MONOLITHIC], [], [monolithic build embedding plugins])
fi

View File

@ -48,7 +48,7 @@ struct private_thread_t {
thread_t public;
/**
* Human-readable ID of this thread.
* Identificator of this thread (human-readable/thread ID).
*/
u_int id;
@ -157,6 +157,23 @@ static void thread_destroy(private_thread_t *this)
free(this);
}
/**
* Determine the ID of the current thread
*/
static u_int get_thread_id()
{
u_int id;
#if defined(USE_THREAD_IDS) && defined(HAVE_GETTID)
id = gettid();
#else
id_mutex->lock(id_mutex);
id = next_id++;
id_mutex->unlock(id_mutex);
#endif
return id;
}
METHOD(thread_t, cancel, void,
private_thread_t *this)
{
@ -284,6 +301,8 @@ static void *thread_main(private_thread_t *this)
{
void *res;
this->id = get_thread_id();
current_thread->set(current_thread, this);
pthread_cleanup_push((thread_cleanup_t)thread_cleanup, this);
@ -315,9 +334,6 @@ thread_t *thread_create(thread_main_t main, void *arg)
this->main = main;
this->arg = arg;
id_mutex->lock(id_mutex);
this->id = next_id++;
id_mutex->unlock(id_mutex);
if (pthread_create(&this->thread_id, NULL, (void*)thread_main, this) != 0)
{
@ -341,11 +357,7 @@ thread_t *thread_current()
if (!this)
{
this = thread_create_internal();
id_mutex->lock(id_mutex);
this->id = next_id++;
id_mutex->unlock(id_mutex);
this->id = get_thread_id();
current_thread->set(current_thread, (void*)this);
}
return &this->public;
@ -475,12 +487,12 @@ void threads_init()
dummy1 = thread_value_create(NULL);
next_id = 1;
main_thread->id = 0;
next_id = 0;
main_thread->thread_id = pthread_self();
current_thread = thread_value_create(NULL);
current_thread->set(current_thread, (void*)main_thread);
id_mutex = mutex_create(MUTEX_TYPE_DEFAULT);
main_thread->id = get_thread_id();
#ifndef HAVE_PTHREAD_CANCEL
{ /* install a signal handler for our custom SIG_CANCEL */

View File

@ -97,11 +97,13 @@ thread_t *thread_create(thread_main_t main, void *arg);
thread_t *thread_current();
/**
* Get the human-readable ID of the current thread.
* Get the ID of the current thread.
*
* The IDs are assigned incrementally starting from 1.
* Depending on the build configuration thread IDs are either assigned
* incrementally starting from 1, or equal the value returned by an appropriate
* syscall (like gettid() or GetCurrentThreadId()), if available.
*
* @return human-readable ID
* @return ID of the current thread
*/
u_int thread_current_id();

View File

@ -516,7 +516,11 @@ thread_t *thread_current()
*/
u_int thread_current_id()
{
#ifdef USE_THREAD_IDS
return get_current_thread()->id;
#else
return get_current_thread()->tid;
#endif
}
/**