8235962: os::current_thread_id() is not signal safe on macOS

Use mach_thread_self instead of pthread_mach_thread_np

Reviewed-by: dholmes, cjplummer
This commit is contained in:
Gerard Ziemski 2020-02-10 11:41:55 -06:00
parent 9886cb401c
commit 080c67f096
3 changed files with 38 additions and 37 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 1999, 2014, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1999, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -36,6 +36,7 @@ void OSThread::pd_initialize() {
#else
_thread_id = NULL;
#endif
_unique_thread_id = 0;
_pthread_id = NULL;
_siginfo = NULL;
_ucontext = NULL;
@ -49,6 +50,22 @@ void OSThread::pd_initialize() {
assert(_startThread_lock !=NULL, "check");
}
// Additional thread_id used to correlate threads in SA
void OSThread::set_unique_thread_id() {
#ifdef __APPLE__
thread_identifier_info_data_t m_ident_info;
mach_msg_type_number_t count = THREAD_IDENTIFIER_INFO_COUNT;
mach_port_t mach_thread_port = mach_thread_self();
guarantee(mach_thread_port != 0, "just checking");
thread_info(mach_thread_port, THREAD_IDENTIFIER_INFO,
(thread_info_t) &m_ident_info, &count);
mach_port_deallocate(mach_task_self(), mach_thread_port);
_unique_thread_id = m_ident_info.thread_id;
#endif
}
void OSThread::pd_destroy() {
delete _startThread_lock;
}

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 1999, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1999, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -82,9 +82,7 @@
_pthread_id = tid;
}
void set_unique_thread_id(uint64_t id) {
_unique_thread_id = id;
}
void set_unique_thread_id();
// ***************************************************************
// suspension support.

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 1999, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1999, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -634,19 +634,6 @@ extern "C" objc_registerThreadWithCollector_t objc_registerThreadWithCollectorFu
objc_registerThreadWithCollector_t objc_registerThreadWithCollectorFunction = NULL;
#endif
#ifdef __APPLE__
static uint64_t locate_unique_thread_id(mach_port_t mach_thread_port) {
// Additional thread_id used to correlate threads in SA
thread_identifier_info_data_t m_ident_info;
mach_msg_type_number_t count = THREAD_IDENTIFIER_INFO_COUNT;
thread_info(mach_thread_port, THREAD_IDENTIFIER_INFO,
(thread_info_t) &m_ident_info, &count);
return m_ident_info.thread_id;
}
#endif
// Thread start routine for all newly created threads
static void *thread_native_entry(Thread *thread) {
@ -672,10 +659,10 @@ static void *thread_native_entry(Thread *thread) {
os::current_thread_id(), (uintx) pthread_self());
#ifdef __APPLE__
uint64_t unique_thread_id = locate_unique_thread_id(osthread->thread_id());
guarantee(unique_thread_id != 0, "unique thread id was not found");
osthread->set_unique_thread_id(unique_thread_id);
// Store unique OS X thread id used by SA
osthread->set_unique_thread_id();
#endif
// initialize signal mask for this thread
os::Bsd::hotspot_sigmask(thread);
@ -823,12 +810,12 @@ bool os::create_attached_thread(JavaThread* thread) {
osthread->set_thread_id(os::Bsd::gettid());
// Store pthread info into the OSThread
#ifdef __APPLE__
uint64_t unique_thread_id = locate_unique_thread_id(osthread->thread_id());
guarantee(unique_thread_id != 0, "just checking");
osthread->set_unique_thread_id(unique_thread_id);
// Store unique OS X thread id used by SA
osthread->set_unique_thread_id();
#endif
// Store pthread info into the OSThread
osthread->set_pthread_id(::pthread_self());
// initialize floating point control register
@ -1100,12 +1087,11 @@ void os::die() {
pid_t os::Bsd::gettid() {
int retval = -1;
#ifdef __APPLE__ //XNU kernel
// despite the fact mach port is actually not a thread id use it
// instead of syscall(SYS_thread_selfid) as it certainly fits to u4
retval = ::pthread_mach_thread_np(::pthread_self());
guarantee(retval != 0, "just checking");
return retval;
#ifdef __APPLE__ // XNU kernel
mach_port_t port = mach_thread_self();
guarantee(MACH_PORT_VALID(port), "just checking");
mach_port_deallocate(mach_task_self(), port);
return (pid_t)port;
#else
#ifdef __FreeBSD__
@ -1128,7 +1114,7 @@ pid_t os::Bsd::gettid() {
intx os::current_thread_id() {
#ifdef __APPLE__
return (intx)::pthread_mach_thread_np(::pthread_self());
return (intx)os::Bsd::gettid();
#else
return (intx)::pthread_self();
#endif