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 uint      ZThread::_worker_id;
  35 
  36 void ZThread::initialize() {
  37   assert(!_initialized, "Already initialized");
  38   const Thread* const thread = Thread::current();
  39   _initialized = true;
  40   _id = (uintptr_t)thread;
  41   _is_vm = thread->is_VM_thread();
  42   _is_java = thread->is_Java_thread();
  43   _is_worker = thread->is_Worker_thread();
  44   _worker_id = (uint)-1;
  45 }
  46 
  47 const char* ZThread::name() {
  48   const Thread* const thread = Thread::current();
  49   if (thread->is_Named_thread()) {
  50     const NamedThread* const named = (const NamedThread*)thread;
  51     return named->name();
  52   } else if (thread->is_Java_thread()) {
  53     return "Java";
  54   }
  55 
  56   return "Unknown";
  57 }
  58 
  59 bool ZThread::has_worker_id() {
  60   return _initialized &&
  61          _is_worker &&
  62          _worker_id != (uint)-1;
  63 }
  64 
  65 void ZThread::set_worker_id(uint worker_id) {
  66   ensure_initialized();
  67   assert(!has_worker_id(), "Worker id already initialized");
  68   _worker_id = worker_id;
  69 }
  70 
  71 void ZThread::clear_worker_id() {
  72   assert(has_worker_id(), "Worker id not initialized");
  73   _worker_id = (uint)-1;
  74 }