You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@qpid.apache.org by kg...@apache.org on 2020/06/29 13:42:17 UTC

[qpid-dispatch] branch master updated: DISPATCH-1694: sys_thread_self() returns non-null for main thread

This is an automated email from the ASF dual-hosted git repository.

kgiusti pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/qpid-dispatch.git


The following commit(s) were added to refs/heads/master by this push:
     new 920df27  DISPATCH-1694: sys_thread_self() returns non-null for main thread
920df27 is described below

commit 920df277eb2342df751e256be96224ebd9865878
Author: Kenneth Giusti <kg...@apache.org>
AuthorDate: Thu Jun 25 10:25:27 2020 -0400

    DISPATCH-1694: sys_thread_self() returns non-null for main thread
    
    This closes #768
---
 src/posix/threading.c | 13 +++++++++++--
 tests/thread_test.c   | 17 ++++++++++++++++-
 2 files changed, 27 insertions(+), 3 deletions(-)

diff --git a/src/posix/threading.c b/src/posix/threading.c
index db367f5..ec9f416 100644
--- a/src/posix/threading.c
+++ b/src/posix/threading.c
@@ -26,6 +26,7 @@
 #include <qpid/dispatch/threading.h>
 #include <qpid/dispatch/ctools.h>
 #include <stdio.h>
+#include <stdint.h>
 #include <pthread.h>
 #include <assert.h>
 
@@ -154,10 +155,16 @@ struct sys_thread_t {
     void *arg;
 };
 
-static __thread sys_thread_t *_self;
+// initialize the per-thread _self to a non-zero value.  This dummy value will
+// be returned when sys_thread_self() is called from the process's main thread
+// of execution (which is not a pthread).  Using a non-zero value provides a
+// way to distinguish a thread id from a zero (unset) value.
+//
+static sys_thread_t  _main_thread_id;
+static __thread sys_thread_t *_self = &_main_thread_id;
 
 
-// bootstrap _self before calling main thread function
+// bootstrap _self before calling thread's main function
 //
 static void *_thread_init(void *arg)
 {
@@ -184,11 +191,13 @@ sys_thread_t *sys_thread_self()
 
 void sys_thread_free(sys_thread_t *thread)
 {
+    assert(thread != &_main_thread_id);
     free(thread);
 }
 
 
 void sys_thread_join(sys_thread_t *thread)
 {
+    assert(thread != &_main_thread_id);
     pthread_join(thread->thread, 0);
 }
diff --git a/tests/thread_test.c b/tests/thread_test.c
index 9a59d0c..9e6e159 100644
--- a/tests/thread_test.c
+++ b/tests/thread_test.c
@@ -79,7 +79,22 @@ static char *test_thread_id(void *context)
     for (int i = 0; i < thread_count; ++i) {
         sys_thread_join(threads[i]);
         sys_thread_free(threads[i]);
-        threads[i] = 0;
+    }
+
+    //
+    // test calling sys_thread_self() from the main context.  This context
+    // was not created by sys_thread(), however a dummy non-zero value is returned.
+    //
+    sys_thread_t *main_id = sys_thread_self();
+    if (!main_id) {
+        result = "sys_thread_self() returned 0 for main thread";
+    } else {
+        for (int i = 0; i < thread_count; ++i) {
+            if (threads[i] == main_id) {   // must be unique!
+                result = "main thread sys_thread_self() not unique!";
+                break;
+            }
+        }
     }
 
     sys_mutex_free(mutex);


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@qpid.apache.org
For additional commands, e-mail: commits-help@qpid.apache.org