|
1 /* |
|
2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. |
|
8 * |
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
|
13 * accompanied this code). |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License version |
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 * |
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 * or visit www.oracle.com if you need additional information or have any |
|
21 * questions. |
|
22 * |
|
23 */ |
|
24 |
|
25 #include "runtime/threadLocalStorage.hpp" |
|
26 #include <pthread.h> |
|
27 |
|
28 static pthread_key_t _thread_key; |
|
29 static bool _initialized = false; |
|
30 |
|
31 // Restore the thread pointer if the destructor is called. This is in case |
|
32 // someone from JNI code sets up a destructor with pthread_key_create to run |
|
33 // detachCurrentThread on thread death. Unless we restore the thread pointer we |
|
34 // will hang or crash. When detachCurrentThread is called the key will be set |
|
35 // to null and we will not be called again. If detachCurrentThread is never |
|
36 // called we could loop forever depending on the pthread implementation. |
|
37 extern "C" void restore_thread_pointer(void* p) { |
|
38 ThreadLocalStorage::set_thread((Thread*) p); |
|
39 } |
|
40 |
|
41 void ThreadLocalStorage::init() { |
|
42 assert(!_initialized, "initializing TLS more than once!"); |
|
43 int rslt = pthread_key_create(&_thread_key, restore_thread_pointer); |
|
44 // If this assert fails we will get a recursive assertion failure |
|
45 // and not see the actual error message or get a hs_err file |
|
46 assert_status(rslt == 0, rslt, "pthread_key_create"); |
|
47 _initialized = true; |
|
48 } |
|
49 |
|
50 bool ThreadLocalStorage::is_initialized() { |
|
51 return _initialized; |
|
52 } |
|
53 |
|
54 Thread* ThreadLocalStorage::thread() { |
|
55 // If this assert fails we will get a recursive assertion failure |
|
56 // and not see the actual error message or get a hs_err file. |
|
57 // Which most likely indicates we have taken an error path early in |
|
58 // the initialization process, which is using Thread::current without |
|
59 // checking TLS is initialized - see java.cpp vm_exit |
|
60 assert(_initialized, "TLS not initialized yet!"); |
|
61 return (Thread*) pthread_getspecific(_thread_key); // may be NULL |
|
62 } |
|
63 |
|
64 void ThreadLocalStorage::set_thread(Thread* current) { |
|
65 assert(_initialized, "TLS not initialized yet!"); |
|
66 int rslt = pthread_setspecific(_thread_key, current); |
|
67 assert_status(rslt == 0, rslt, "pthread_setspecific"); |
|
68 } |