Path: blob/master/src/hotspot/os/posix/threadLocalStorage_posix.cpp
40930 views
/*1* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "runtime/threadLocalStorage.hpp"25#include <pthread.h>2627static pthread_key_t _thread_key;28static bool _initialized = false;2930// Restore the thread pointer if the destructor is called. This is in case31// someone from JNI code sets up a destructor with pthread_key_create to run32// detachCurrentThread on thread death. Unless we restore the thread pointer we33// will hang or crash. When detachCurrentThread is called the key will be set34// to null and we will not be called again. If detachCurrentThread is never35// called we could loop forever depending on the pthread implementation.36extern "C" void restore_thread_pointer(void* p) {37ThreadLocalStorage::set_thread((Thread*) p);38}3940void ThreadLocalStorage::init() {41assert(!_initialized, "initializing TLS more than once!");42int rslt = pthread_key_create(&_thread_key, restore_thread_pointer);43// If this assert fails we will get a recursive assertion failure44// and not see the actual error message or get a hs_err file45assert_status(rslt == 0, rslt, "pthread_key_create");46_initialized = true;47}4849bool ThreadLocalStorage::is_initialized() {50return _initialized;51}5253Thread* ThreadLocalStorage::thread() {54// If this assert fails we will get a recursive assertion failure55// and not see the actual error message or get a hs_err file.56// Which most likely indicates we have taken an error path early in57// the initialization process, which is using Thread::current without58// checking TLS is initialized - see java.cpp vm_exit59assert(_initialized, "TLS not initialized yet!");60return (Thread*) pthread_getspecific(_thread_key); // may be NULL61}6263void ThreadLocalStorage::set_thread(Thread* current) {64assert(_initialized, "TLS not initialized yet!");65int rslt = pthread_setspecific(_thread_key, current);66assert_status(rslt == 0, rslt, "pthread_setspecific");67}686970