thread: Allow thread ID to be value returned by gettid()
authorThomas Egerer <thomas.egerer@secunet.com>
Fri, 12 Feb 2016 16:09:47 +0000 (17:09 +0100)
committerTobias Brunner <tobias@strongswan.org>
Fri, 4 Mar 2016 08:12:11 +0000 (09:12 +0100)
Signed-off-by: Thomas Egerer <thomas.egerer@secunet.com>
configure.ac
src/libstrongswan/threading/thread.c
src/libstrongswan/threading/thread.h
src/libstrongswan/threading/windows/thread.c

index 5f22808..497b002 100644 (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
index 7a243e8..3d87e7f 100644 (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 */
index c247728..35da244 100644 (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();
 
index 6105247..798d75b 100644 (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
 }
 
 /**