0N/A/*
2273N/A * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1472N/A * or visit www.oracle.com if you need additional information or have any
1472N/A * questions.
0N/A *
0N/A */
0N/A
1879N/A#include "precompiled.hpp"
1879N/A#include "classfile/placeholders.hpp"
1879N/A#include "classfile/systemDictionary.hpp"
1879N/A#include "oops/oop.inline.hpp"
1879N/A#include "runtime/fieldType.hpp"
1879N/A#include "utilities/hashtable.inline.hpp"
0N/A
0N/A// Placeholder methods
0N/A
2062N/APlaceholderEntry* PlaceholderTable::new_entry(int hash, Symbol* name,
0N/A oop loader, bool havesupername,
2062N/A Symbol* supername) {
3863N/A PlaceholderEntry* entry = (PlaceholderEntry*)Hashtable<Symbol*, mtClass>::new_entry(hash, name);
2062N/A // Hashtable with Symbol* literal must increment and decrement refcount.
2062N/A name->increment_refcount();
0N/A entry->set_loader(loader);
0N/A entry->set_havesupername(havesupername);
0N/A entry->set_supername(supername);
0N/A entry->set_superThreadQ(NULL);
0N/A entry->set_loadInstanceThreadQ(NULL);
0N/A entry->set_defineThreadQ(NULL);
0N/A entry->set_definer(NULL);
0N/A entry->set_instanceKlass(NULL);
0N/A return entry;
0N/A}
0N/A
2062N/Avoid PlaceholderTable::free_entry(PlaceholderEntry* entry) {
2062N/A // decrement Symbol refcount here because Hashtable doesn't.
2062N/A entry->literal()->decrement_refcount();
2062N/A if (entry->supername() != NULL) entry->supername()->decrement_refcount();
3863N/A Hashtable<Symbol*, mtClass>::free_entry(entry);
2062N/A}
2062N/A
0N/A
0N/A// Placeholder objects represent classes currently being loaded.
0N/A// All threads examining the placeholder table must hold the
0N/A// SystemDictionary_lock, so we don't need special precautions
0N/A// on store ordering here.
0N/Avoid PlaceholderTable::add_entry(int index, unsigned int hash,
2062N/A Symbol* class_name, Handle class_loader,
2062N/A bool havesupername, Symbol* supername){
0N/A assert_locked_or_safepoint(SystemDictionary_lock);
2062N/A assert(class_name != NULL, "adding NULL obj");
0N/A
0N/A // Both readers and writers are locked so it's safe to just
0N/A // create the placeholder and insert it in the list without a membar.
2062N/A PlaceholderEntry* entry = new_entry(hash, class_name, class_loader(), havesupername, supername);
0N/A add_entry(index, entry);
0N/A}
0N/A
0N/A
0N/A// Remove a placeholder object.
0N/Avoid PlaceholderTable::remove_entry(int index, unsigned int hash,
2062N/A Symbol* class_name,
0N/A Handle class_loader) {
0N/A assert_locked_or_safepoint(SystemDictionary_lock);
0N/A PlaceholderEntry** p = bucket_addr(index);
0N/A while (*p) {
0N/A PlaceholderEntry *probe = *p;
2062N/A if (probe->hash() == hash && probe->equals(class_name, class_loader())) {
0N/A // Delete entry
0N/A *p = probe->next();
0N/A free_entry(probe);
0N/A return;
0N/A }
0N/A p = probe->next_addr();
0N/A }
0N/A}
0N/A
0N/APlaceholderEntry* PlaceholderTable::get_entry(int index, unsigned int hash,
2062N/A Symbol* class_name,
0N/A Handle class_loader) {
0N/A assert_locked_or_safepoint(SystemDictionary_lock);
0N/A
0N/A oop class_loader_ = class_loader();
0N/A
0N/A for (PlaceholderEntry *place_probe = bucket(index);
0N/A place_probe != NULL;
0N/A place_probe = place_probe->next()) {
0N/A if (place_probe->hash() == hash &&
2062N/A place_probe->equals(class_name, class_loader_)) {
0N/A return place_probe;
0N/A }
0N/A }
0N/A return NULL;
0N/A}
0N/A
2062N/ASymbol* PlaceholderTable::find_entry(int index, unsigned int hash,
2062N/A Symbol* class_name,
0N/A Handle class_loader) {
0N/A PlaceholderEntry* probe = get_entry(index, hash, class_name, class_loader);
2062N/A return (probe? probe->klassname(): (Symbol*)NULL);
0N/A}
0N/A
0N/A // find_and_add returns probe pointer - old or new
0N/A // If no entry exists, add a placeholder entry
0N/A // If entry exists, reuse entry
0N/A // For both, push SeenThread for classloadAction
0N/A // if havesupername: this is used for circularity for instanceklass loading
2062N/APlaceholderEntry* PlaceholderTable::find_and_add(int index, unsigned int hash, Symbol* name, Handle loader, classloadAction action, Symbol* supername, Thread* thread) {
0N/A PlaceholderEntry* probe = get_entry(index, hash, name, loader);
0N/A if (probe == NULL) {
0N/A // Nothing found, add place holder
0N/A add_entry(index, hash, name, loader, (action == LOAD_SUPER), supername);
0N/A probe = get_entry(index, hash, name, loader);
0N/A } else {
0N/A if (action == LOAD_SUPER) {
0N/A probe->set_havesupername(true);
2062N/A probe->set_supername(supername);
0N/A }
0N/A }
0N/A if (probe) probe->add_seen_thread(thread, action);
0N/A return probe;
0N/A}
0N/A
0N/A
0N/A// placeholder used to track class loading internal states
0N/A// placeholder existence now for loading superclass/superinterface
0N/A// superthreadQ tracks class circularity, while loading superclass/superinterface
0N/A// loadInstanceThreadQ tracks load_instance_class calls
0N/A// definer() tracks the single thread that owns define token
0N/A// defineThreadQ tracks waiters on defining thread's results
0N/A// 1st claimant creates placeholder
0N/A// find_and_add adds SeenThread entry for appropriate queue
0N/A// All claimants remove SeenThread after completing action
0N/A// On removal: if definer and all queues empty, remove entry
0N/A// Note: you can be in both placeholders and systemDictionary
0N/A// see parse_stream for redefine classes
0N/A// Therefore - must always check SD first
0N/A// Ignores the case where entry is not found
0N/Avoid PlaceholderTable::find_and_remove(int index, unsigned int hash,
2062N/A Symbol* name, Handle loader, Thread* thread) {
0N/A assert_locked_or_safepoint(SystemDictionary_lock);
0N/A PlaceholderEntry *probe = get_entry(index, hash, name, loader);
0N/A if (probe != NULL) {
0N/A // No other threads using this entry
0N/A if ((probe->superThreadQ() == NULL) && (probe->loadInstanceThreadQ() == NULL)
0N/A && (probe->defineThreadQ() == NULL) && (probe->definer() == NULL)) {
0N/A remove_entry(index, hash, name, loader);
0N/A }
0N/A }
0N/A }
0N/A
0N/APlaceholderTable::PlaceholderTable(int table_size)
3863N/A : TwoOopHashtable<Symbol*, mtClass>(table_size, sizeof(PlaceholderEntry)) {
0N/A}
0N/A
0N/A
0N/Avoid PlaceholderTable::oops_do(OopClosure* f) {
0N/A for (int index = 0; index < table_size(); index++) {
0N/A for (PlaceholderEntry* probe = bucket(index);
0N/A probe != NULL;
0N/A probe = probe->next()) {
0N/A probe->oops_do(f);
0N/A }
0N/A }
0N/A}
0N/A
0N/A
0N/Avoid PlaceholderEntry::oops_do(OopClosure* blk) {
2062N/A assert(klassname() != NULL, "should have a non-null klass");
0N/A if (_loader != NULL) {
0N/A blk->do_oop(loader_addr());
0N/A }
0N/A if (_instanceKlass != NULL) {
0N/A blk->do_oop((oop*)instanceKlass_addr());
0N/A }
0N/A}
0N/A
0N/A// do all entries in the placeholder table
2062N/Avoid PlaceholderTable::entries_do(void f(Symbol*, oop)) {
0N/A for (int index = 0; index < table_size(); index++) {
0N/A for (PlaceholderEntry* probe = bucket(index);
0N/A probe != NULL;
0N/A probe = probe->next()) {
2062N/A f(probe->klassname(), probe->loader());
0N/A }
0N/A }
0N/A}
0N/A
0N/A
0N/A#ifndef PRODUCT
0N/A// Note, doesn't append a cr
0N/Avoid PlaceholderEntry::print() const {
2062N/A klassname()->print_value();
0N/A if (loader() != NULL) {
0N/A tty->print(", loader ");
0N/A loader()->print_value();
0N/A }
0N/A if (supername() != NULL) {
0N/A tty->print(", supername ");
0N/A supername()->print_value();
0N/A }
0N/A if (definer() != NULL) {
0N/A tty->print(", definer ");
0N/A definer()->print_value();
0N/A }
0N/A if (instanceKlass() != NULL) {
0N/A tty->print(", instanceKlass ");
0N/A instanceKlass()->print_value();
0N/A }
0N/A tty->print("\n");
0N/A tty->print("loadInstanceThreadQ threads:");
0N/A loadInstanceThreadQ()->printActionQ();
0N/A tty->print("\n");
0N/A tty->print("superThreadQ threads:");
0N/A superThreadQ()->printActionQ();
0N/A tty->print("\n");
0N/A tty->print("defineThreadQ threads:");
0N/A defineThreadQ()->printActionQ();
0N/A tty->print("\n");
0N/A}
0N/A#endif
0N/A
0N/Avoid PlaceholderEntry::verify() const {
0N/A guarantee(loader() == NULL || loader()->is_instance(),
0N/A "checking type of _loader");
0N/A guarantee(instanceKlass() == NULL
0N/A || Klass::cast(instanceKlass())->oop_is_instance(),
0N/A "checking type of instanceKlass result");
0N/A}
0N/A
0N/Avoid PlaceholderTable::verify() {
0N/A int element_count = 0;
0N/A for (int pindex = 0; pindex < table_size(); pindex++) {
0N/A for (PlaceholderEntry* probe = bucket(pindex);
0N/A probe != NULL;
0N/A probe = probe->next()) {
0N/A probe->verify();
0N/A element_count++; // both klasses and place holders count
0N/A }
0N/A }
0N/A guarantee(number_of_entries() == element_count,
0N/A "Verify of system dictionary failed");
0N/A}
0N/A
0N/A
0N/A#ifndef PRODUCT
0N/Avoid PlaceholderTable::print() {
0N/A for (int pindex = 0; pindex < table_size(); pindex++) {
0N/A for (PlaceholderEntry* probe = bucket(pindex);
0N/A probe != NULL;
0N/A probe = probe->next()) {
0N/A if (Verbose) tty->print("%4d: ", pindex);
0N/A tty->print(" place holder ");
0N/A
0N/A probe->print();
0N/A tty->cr();
0N/A }
0N/A }
0N/A}
0N/A#endif