Bug 64316

Summary: Duplicate code for obtaining native current thread id
Product: Tomcat Native Reporter: Michael Osipov <michaelo>
Component: LibraryAssignee: Tomcat Developers Mailing List <dev>
Status: RESOLVED FIXED    
Severity: normal    
Priority: P2    
Version: 1.2.23   
Target Milestone: ---   
Hardware: All   
OS: All   

Description Michael Osipov 2020-04-06 13:26:08 UTC
We can improve/reduce code by collapsing those two into one internal function:

src/thread.c:
> TCN_IMPLEMENT_CALL(jlong, Thread, current)(TCN_STDARGS)
> {
>     UNREFERENCED_STDARGS;
>     return (jlong)((unsigned long)apr_os_thread_current());
> }

and src/ssl.c:
> 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
> }

I think that we can throw away the apr_os_thread_current() because we already have the thread id for Windows and for Unix is it merely:
> APR_DECLARE(apr_os_thread_t) apr_os_thread_current(void)
> {
>     return pthread_self();
> }

There are implementations for BeOS, Netware and OS/2, but we don't care for them.
Comment 1 Michael Osipov 2020-04-26 09:29:04 UTC
Fixed in:
- master for 1.2.24 onwards