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

michaelo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/tomcat-native.git


The following commit(s) were added to refs/heads/master by this push:
     new f95f531  Introduce tcn_get_thread_id(void) to reduce code duplication
f95f531 is described below

commit f95f531e98278cc7555367084b967e3550734559
Author: Michael Osipov <micha...@apache.org>
AuthorDate: Thu Apr 23 18:52:44 2020 +0200

    Introduce tcn_get_thread_id(void) to reduce code duplication
    
    At two spots (ssl.c and thread.c) we need to obtain the native thread id.
    This has been done with two different approaches. Move out to
    tcn_get_thread(void) which uses the previous ssl_thread_id(void) 
implementation
    while the previous functions delegate to the new one. 
apr_os_thread_current(void)
    is not used anymore which does internally the same thing as 
ssl_thread_id(void)
    was doing.
    
    Also add properly #ifdefs for Windows and macOS for function prototype 
includes.
---
 native/include/tcn.h              |  1 +
 native/src/jnilib.c               | 45 +++++++++++++++++++++++++++++++++++++++
 native/src/ssl.c                  | 33 +---------------------------
 native/src/thread.c               |  3 ++-
 xdocs/miscellaneous/changelog.xml |  5 ++++-
 5 files changed, 53 insertions(+), 34 deletions(-)

diff --git a/native/include/tcn.h b/native/include/tcn.h
index 2b2ae59..d2f316b 100644
--- a/native/include/tcn.h
+++ b/native/include/tcn.h
@@ -175,6 +175,7 @@ char           *tcn_strdup(JNIEnv *, jstring);
 char           *tcn_pstrdup(JNIEnv *, jstring, apr_pool_t *);
 apr_status_t    tcn_load_finfo_class(JNIEnv *, jclass);
 apr_status_t    tcn_load_ainfo_class(JNIEnv *, jclass);
+unsigned long   tcn_get_thread_id(void);
 
 #define J2S(V)  c##V
 #define J2L(V)  p##V
diff --git a/native/src/jnilib.c b/native/src/jnilib.c
index dae3ade..e88d4d5 100644
--- a/native/src/jnilib.c
+++ b/native/src/jnilib.c
@@ -23,6 +23,22 @@
 
 #include "tcn_version.h"
 
+#ifdef WIN32
+#include <Windows.h>
+#endif
+
+#ifdef DARWIN
+#include <pthread.h>
+#endif
+
+#ifdef __FreeBSD__
+#include <pthread_np.h>
+#endif
+
+#ifdef __linux__
+#include <sys/syscall.h>
+#endif
+
 #ifdef TCN_DO_STATISTICS
 extern void sp_poll_dump_statistics();
 extern void sp_network_dump_statistics();
@@ -481,3 +497,32 @@ jint tcn_get_java_env(JNIEnv **env)
     }
     return JNI_OK;
 }
+
+unsigned long tcn_get_thread_id(void)
+{
+    /* OpenSSL needs this to return an unsigned long.  On OS/390, the pthread
+     * id is a structure twice that big.  Use the TCB pointer instead as a
+     * unique unsigned long.
+     */
+#ifdef __MVS__
+    struct PSA {
+        char unmapped[540];
+        unsigned long PSATOLD;
+    } *psaptr = 0;
+
+    return psaptr->PSATOLD;
+#elif defined(WIN32)
+    return (unsigned long)GetCurrentThreadId();
+#elif defined(DARWIN)
+    uint64_t tid;
+    pthread_threadid_np(NULL, &tid);
+    return (unsigned long)tid;
+#elif defined(__FreeBSD__)
+    return (unsigned long)pthread_getthreadid_np();
+#elif defined(__linux__)
+    return (unsigned long)syscall(SYS_gettid);
+#else
+    return (unsigned long)pthread_self();
+#endif
+}
+
diff --git a/native/src/ssl.c b/native/src/ssl.c
index 57fbb53..98d77eb 100644
--- a/native/src/ssl.c
+++ b/native/src/ssl.c
@@ -20,14 +20,6 @@
 #include "apr_atomic.h"
 #include "apr_poll.h"
 
-#ifdef __linux__
-#include <sys/syscall.h>
-#endif
-
-#ifdef __FreeBSD__
-#include <pthread_np.h>
-#endif
-
 #ifdef HAVE_OPENSSL
 #include "ssl_private.h"
 
@@ -465,30 +457,7 @@ static void ssl_thread_lock(int mode, int type,
 
 static unsigned long ssl_thread_id(void)
 {
-    /* OpenSSL needs this to return an unsigned long.  On OS/390, the pthread
-     * id is a structure twice that big.  Use the TCB pointer instead as a
-     * unique unsigned long.
-     */
-#ifdef __MVS__
-    struct PSA {
-        char unmapped[540];
-        unsigned long PSATOLD;
-    } *psaptr = 0;
-
-    return psaptr->PSATOLD;
-#elif defined(WIN32)
-    return (unsigned long)GetCurrentThreadId();
-#elif defined(DARWIN)
-    uint64_t tid;
-    pthread_threadid_np(NULL, &tid);
-    return (unsigned long)tid;
-#elif defined(__FreeBSD__)
-    return (unsigned long)pthread_getthreadid_np();
-#elif defined(__linux__)
-    return (unsigned long)syscall(SYS_gettid);
-#else
-    return (unsigned long)(apr_os_thread_current());
-#endif
+    return (unsigned long)tcn_get_thread_id();
 }
 
 #if OPENSSL_VERSION_NUMBER < 0x10100000L
diff --git a/native/src/thread.c b/native/src/thread.c
index bddaee3..6696dc0 100644
--- a/native/src/thread.c
+++ b/native/src/thread.c
@@ -19,5 +19,6 @@
 TCN_IMPLEMENT_CALL(jlong, Thread, current)(TCN_STDARGS)
 {
     UNREFERENCED_STDARGS;
-    return (jlong)((unsigned long)apr_os_thread_current());
+    return (jlong)tcn_get_thread_id();
 }
+
diff --git a/xdocs/miscellaneous/changelog.xml 
b/xdocs/miscellaneous/changelog.xml
index 0b64cee..a04005a 100644
--- a/xdocs/miscellaneous/changelog.xml
+++ b/xdocs/miscellaneous/changelog.xml
@@ -58,9 +58,12 @@ This will have been <?xml version="1.0" encoding="UTF-8"?>
       ssl_thread_id(void). (michaelo)
     </add>
     <update>
-      <bug>63701 </bug>Use new OpenSSL initialisation process when building 
with
+      <bug>63701</bug>: Use new OpenSSL initialisation process when building 
with
       OpenSSL 1.1.0 onwards. (mturk)
     </update>
+    <add>
+      Introduce tcn_get_thread_id(void) to reduce code duplication. (michaelo)
+    </add>
   </changelog>
 </section>
 <section name="Changes in 1.2.23">


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org

Reply via email to