| 1 | /* |
| 2 | * Copyright (c) 2015, 2017, 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 | #include "precompiled.hpp" |
| 25 | #include "gc/z/zThread.hpp" |
| 26 | #include "runtime/thread.hpp" |
| 27 | #include "utilities/debug.hpp" |
| 28 | |
| 29 | __thread bool ZThread::_initialized; |
| 30 | __thread uintptr_t ZThread::_id; |
| 31 | __thread bool ZThread::_is_vm; |
| 32 | __thread bool ZThread::_is_java; |
| 33 | __thread bool ZThread::_is_worker; |
| 34 | __thread bool ZThread::_is_runtime_worker; |
| 35 | __thread uint ZThread::_worker_id; |
| 36 | |
| 37 | void ZThread::initialize() { |
| 38 | assert(!_initialized, "Already initialized" ); |
| 39 | const Thread* const thread = Thread::current(); |
| 40 | _initialized = true; |
| 41 | _id = (uintptr_t)thread; |
| 42 | _is_vm = thread->is_VM_thread(); |
| 43 | _is_java = thread->is_Java_thread(); |
| 44 | _is_worker = false; |
| 45 | _is_runtime_worker = false; |
| 46 | _worker_id = (uint)-1; |
| 47 | } |
| 48 | |
| 49 | const char* ZThread::name() { |
| 50 | const Thread* const thread = Thread::current(); |
| 51 | if (thread->is_Named_thread()) { |
| 52 | const NamedThread* const named = (const NamedThread*)thread; |
| 53 | return named->name(); |
| 54 | } else if (thread->is_Java_thread()) { |
| 55 | return "Java" ; |
| 56 | } |
| 57 | |
| 58 | return "Unknown" ; |
| 59 | } |
| 60 | |
| 61 | void ZThread::set_worker() { |
| 62 | ensure_initialized(); |
| 63 | _is_worker = true; |
| 64 | } |
| 65 | |
| 66 | void ZThread::set_runtime_worker() { |
| 67 | ensure_initialized(); |
| 68 | _is_runtime_worker = true; |
| 69 | } |
| 70 | |
| 71 | bool ZThread::has_worker_id() { |
| 72 | return _initialized && |
| 73 | _is_worker && |
| 74 | _worker_id != (uint)-1; |
| 75 | } |
| 76 | |
| 77 | void ZThread::set_worker_id(uint worker_id) { |
| 78 | ensure_initialized(); |
| 79 | assert(!has_worker_id(), "Worker id already initialized" ); |
| 80 | _worker_id = worker_id; |
| 81 | } |
| 82 | |
| 83 | void ZThread::clear_worker_id() { |
| 84 | assert(has_worker_id(), "Worker id not initialized" ); |
| 85 | _worker_id = (uint)-1; |
| 86 | } |
| 87 | |