threadLocalStorage_windows.cpp revision 9651:f7dc8eebc3f5
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 "precompiled.hpp"
26#include "runtime/threadLocalStorage.hpp"
27#include <windows.h>
28
29static DWORD _thread_key;
30static bool _initialized = false;
31
32
33void ThreadLocalStorage::init() {
34  assert(!_initialized, "initializing TLS more than once!");
35  _thread_key = TlsAlloc();
36  // If this assert fails we will get a recursive assertion failure
37  // and not see the actual error message or get a hs_err file
38  assert(_thread_key != TLS_OUT_OF_INDEXES, "TlsAlloc failed: out of indices");
39  _initialized = true;
40}
41
42bool ThreadLocalStorage::is_initialized() {
43  return _initialized;
44}
45
46Thread* ThreadLocalStorage::thread() {
47  // If this assert fails we will get a recursive assertion failure
48  // and not see the actual error message or get a hs_err file.
49  // Which most likely indicates we have taken an error path early in
50  // the initialization process, which is using Thread::current without
51  // checking TLS is initialized - see java.cpp vm_exit
52  assert(_initialized, "TLS not initialized yet!");
53  Thread* current = (Thread*) TlsGetValue(_thread_key);
54  assert(current != 0 || GetLastError() == ERROR_SUCCESS,
55         "TlsGetValue failed with error code: %lu", GetLastError());
56  return current;
57}
58
59void ThreadLocalStorage::set_thread(Thread* current) {
60  assert(_initialized, "TLS not initialized yet!");
61  BOOL res = TlsSetValue(_thread_key, current);
62  assert(res, "TlsSetValue failed with error code: %lu", GetLastError());
63}
64