1 /*
   2  * Copyright (c) 2003, 2013, 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 #ifndef SHARE_VM_CLASSFILE_PLACEHOLDERS_HPP
  26 #define SHARE_VM_CLASSFILE_PLACEHOLDERS_HPP
  27 
  28 #include "runtime/thread.hpp"
  29 #include "utilities/hashtable.hpp"
  30 
  31 class PlaceholderEntry;
  32 
  33 // Placeholder objects. These represent classes currently
  34 // being loaded, as well as arrays of primitives.
  35 //
  36 
  37 class PlaceholderTable : public TwoOopHashtable<Symbol*, mtClass> {
  38   friend class VMStructs;
  39 
  40 public:
  41   PlaceholderTable(int table_size);
  42 
  43   PlaceholderEntry* new_entry(int hash, Symbol* name, ClassLoaderData* loader_data, bool havesupername, Symbol* supername);
  44   void free_entry(PlaceholderEntry* entry);
  45 
  46   PlaceholderEntry* bucket(int i) {
  47     return (PlaceholderEntry*)Hashtable<Symbol*, mtClass>::bucket(i);
  48   }
  49 
  50   PlaceholderEntry** bucket_addr(int i) {
  51     return (PlaceholderEntry**)Hashtable<Symbol*, mtClass>::bucket_addr(i);
  52   }
  53 
  54   void add_entry(int index, PlaceholderEntry* new_entry) {
  55     Hashtable<Symbol*, mtClass>::add_entry(index, (HashtableEntry<Symbol*, mtClass>*)new_entry);
  56   }
  57 
  58   void add_entry(int index, unsigned int hash, Symbol* name,
  59                 ClassLoaderData* loader_data, bool havesupername, Symbol* supername);
  60 
  61   // This returns a Symbol* to match type for SystemDictionary
  62   Symbol* find_entry(int index, unsigned int hash,
  63                        Symbol* name, ClassLoaderData* loader_data);
  64 
  65   PlaceholderEntry* get_entry(int index, unsigned int hash,
  66                        Symbol* name, ClassLoaderData* loader_data);
  67 
  68 // caller to create a placeholder entry must enumerate an action
  69 // caller claims ownership of that action
  70 // For parallel classloading:
  71 // multiple LOAD_INSTANCE threads can proceed in parallel
  72 // multiple LOAD_SUPER threads can proceed in parallel
  73 // LOAD_SUPER needed to check for class circularity
  74 // DEFINE_CLASS: ultimately define class must be single threaded
  75 // on a class/classloader basis
  76 // so the head of that queue owns the token
  77 // and the rest of the threads return the result the first thread gets
  78  enum classloadAction {
  79     LOAD_INSTANCE = 1,             // calling load_instance_class
  80     LOAD_SUPER = 2,                // loading superclass for this class
  81     DEFINE_CLASS = 3               // find_or_define class
  82  };
  83 
  84   // find_and_add returns probe pointer - old or new
  85   // If no entry exists, add a placeholder entry and push SeenThread for classloadAction
  86   // If entry exists, reuse entry and push SeenThread for classloadAction
  87   PlaceholderEntry* find_and_add(int index, unsigned int hash,
  88                                  Symbol* name, ClassLoaderData* loader_data,
  89                                  classloadAction action, Symbol* supername,
  90                                  Thread* thread);
  91 
  92   void remove_entry(int index, unsigned int hash,
  93                     Symbol* name, ClassLoaderData* loader_data);
  94 
  95   // find_and_remove first removes SeenThread for classloadAction
  96   // If all queues are empty and definer is null, remove the PlacheholderEntry completely
  97   void find_and_remove(int index, unsigned int hash,
  98                        Symbol* name, ClassLoaderData* loader_data,
  99                        classloadAction action, Thread* thread);
 100 
 101   // GC support.
 102   void classes_do(KlassClosure* f);
 103 
 104 #ifndef PRODUCT
 105   void print();
 106 #endif
 107   void verify();
 108 };
 109 
 110 // SeenThread objects represent list of threads that are
 111 // currently performing a load action on a class.
 112 // For class circularity, set before loading a superclass.
 113 // For bootclasssearchpath, set before calling load_instance_class.
 114 // Defining must be single threaded on a class/classloader basis
 115 // For DEFINE_CLASS, the head of the queue owns the
 116 // define token and the rest of the threads wait to return the
 117 // result the first thread gets.
 118 class SeenThread: public CHeapObj<mtInternal> {
 119 private:
 120    Thread *_thread;
 121    SeenThread* _stnext;
 122    SeenThread* _stprev;
 123 public:
 124    SeenThread(Thread *thread) {
 125        _thread = thread;
 126        _stnext = NULL;
 127        _stprev = NULL;
 128    }
 129    Thread* thread()                const { return _thread;}
 130    void set_thread(Thread *thread) { _thread = thread; }
 131 
 132    SeenThread* next()              const { return _stnext;}
 133    void set_next(SeenThread *seen) { _stnext = seen; }
 134    void set_prev(SeenThread *seen) { _stprev = seen; }
 135 
 136 #ifndef PRODUCT
 137   void printActionQ() {
 138     SeenThread* seen = this;
 139     while (seen != NULL) {
 140       seen->thread()->print_value();
 141       tty->print(", ");
 142       seen = seen->next();
 143     }
 144   }
 145 #endif // PRODUCT
 146 };
 147 
 148 // Placeholder objects represent classes currently being loaded.
 149 // All threads examining the placeholder table must hold the
 150 // SystemDictionary_lock, so we don't need special precautions
 151 // on store ordering here.
 152 // The system dictionary is the only user of this class.
 153 
 154 class PlaceholderEntry : public HashtableEntry<Symbol*, mtClass> {
 155   friend class VMStructs;
 156 
 157 
 158  private:
 159   ClassLoaderData*  _loader_data;   // initiating loader
 160   bool              _havesupername; // distinguish between null supername, and unknown
 161   Symbol*           _supername;
 162   Thread*           _definer;       // owner of define token
 163   Klass*            _instanceKlass; // InstanceKlass from successful define
 164   SeenThread*       _superThreadQ;  // doubly-linked queue of Threads loading a superclass for this class
 165   SeenThread*       _loadInstanceThreadQ;  // loadInstance thread
 166                                     // can be multiple threads if classloader object lock broken by application
 167                                     // or if classloader supports parallel classloading
 168 
 169   SeenThread*       _defineThreadQ; // queue of Threads trying to define this class
 170                                     // including _definer
 171                                     // _definer owns token
 172                                     // queue waits for and returns results from _definer
 173 
 174  public:
 175   // Simple accessors, used only by SystemDictionary
 176   Symbol*            klassname()           const { return literal(); }
 177 
 178   ClassLoaderData*   loader_data()         const { return _loader_data; }
 179   void               set_loader_data(ClassLoaderData* loader_data) { _loader_data = loader_data; }
 180 
 181   bool               havesupername()       const { return _havesupername; }
 182   void               set_havesupername(bool havesupername) { _havesupername = havesupername; }
 183 
 184   Symbol*            supername()           const { return _supername; }
 185   void               set_supername(Symbol* supername) {
 186     _supername = supername;
 187     if (_supername != NULL) _supername->increment_refcount();
 188   }
 189 
 190   Thread*            definer()             const {return _definer; }
 191   void               set_definer(Thread* definer) { _definer = definer; }
 192 
 193   Klass*             instance_klass()      const {return _instanceKlass; }
 194   void               set_instance_klass(Klass* ik) { _instanceKlass = ik; }
 195 
 196   SeenThread*        superThreadQ()        const { return _superThreadQ; }
 197   void               set_superThreadQ(SeenThread* SeenThread) { _superThreadQ = SeenThread; }
 198 
 199   SeenThread*        loadInstanceThreadQ() const { return _loadInstanceThreadQ; }
 200   void               set_loadInstanceThreadQ(SeenThread* SeenThread) { _loadInstanceThreadQ = SeenThread; }
 201 
 202   SeenThread*        defineThreadQ()        const { return _defineThreadQ; }
 203   void               set_defineThreadQ(SeenThread* SeenThread) { _defineThreadQ = SeenThread; }
 204 
 205   PlaceholderEntry* next() const {
 206     return (PlaceholderEntry*)HashtableEntry<Symbol*, mtClass>::next();
 207   }
 208 
 209   PlaceholderEntry** next_addr() {
 210     return (PlaceholderEntry**)HashtableEntry<Symbol*, mtClass>::next_addr();
 211   }
 212 
 213   // Test for equality
 214   // Entries are unique for class/classloader name pair
 215   bool equals(Symbol* class_name, ClassLoaderData* loader) const {
 216     return (klassname() == class_name && loader_data() == loader);
 217   }
 218 
 219   SeenThread* actionToQueue(PlaceholderTable::classloadAction action) {
 220     SeenThread* queuehead = NULL;
 221     switch (action) {
 222       case PlaceholderTable::LOAD_INSTANCE:
 223          queuehead = _loadInstanceThreadQ;
 224          break;
 225       case PlaceholderTable::LOAD_SUPER:
 226          queuehead = _superThreadQ;
 227          break;
 228       case PlaceholderTable::DEFINE_CLASS:
 229          queuehead = _defineThreadQ;
 230          break;
 231       default: Unimplemented();
 232     }
 233     return queuehead;
 234   }
 235 
 236   void set_threadQ(SeenThread* seenthread, PlaceholderTable::classloadAction action) {
 237     switch (action) {
 238       case PlaceholderTable::LOAD_INSTANCE:
 239          _loadInstanceThreadQ = seenthread;
 240          break;
 241       case PlaceholderTable::LOAD_SUPER:
 242          _superThreadQ = seenthread;
 243          break;
 244       case PlaceholderTable::DEFINE_CLASS:
 245          _defineThreadQ = seenthread;
 246          break;
 247       default: Unimplemented();
 248     }
 249     return;
 250   }
 251 
 252   bool super_load_in_progress() {
 253      return (_superThreadQ != NULL);
 254   }
 255 
 256   bool instance_load_in_progress() {
 257     return (_loadInstanceThreadQ != NULL);
 258   }
 259 
 260   bool define_class_in_progress() {
 261     return (_defineThreadQ != NULL);
 262   }
 263 
 264 // Doubly-linked list of Threads per action for class/classloader pair
 265 // Class circularity support: links in thread before loading superclass
 266 // bootstrapsearchpath support: links in a thread before load_instance_class
 267 // definers: use as queue of define requestors, including owner of
 268 // define token. Appends for debugging of requestor order
 269   void add_seen_thread(Thread* thread, PlaceholderTable::classloadAction action) {
 270     assert_lock_strong(SystemDictionary_lock);
 271     SeenThread* threadEntry = new SeenThread(thread);
 272     SeenThread* seen = actionToQueue(action);
 273 
 274     if (seen == NULL) {
 275       set_threadQ(threadEntry, action);
 276       return;
 277     }
 278     SeenThread* next;
 279     while ((next = seen->next()) != NULL) {
 280       seen = next;
 281     }
 282     seen->set_next(threadEntry);
 283     threadEntry->set_prev(seen);
 284     return;
 285   }
 286 
 287   bool check_seen_thread(Thread* thread, PlaceholderTable::classloadAction action) {
 288     assert_lock_strong(SystemDictionary_lock);
 289     SeenThread* threadQ = actionToQueue(action);
 290     SeenThread* seen = threadQ;
 291     while (seen) {
 292       if (thread == seen->thread()) {
 293         return true;
 294       }
 295       seen = seen->next();
 296     }
 297     return false;
 298   }
 299 
 300   // returns true if seenthreadQ is now empty
 301   // Note, caller must ensure probe still exists while holding
 302   // SystemDictionary_lock
 303   // ignores if cleanup has already been done
 304   // if found, deletes SeenThread
 305   bool remove_seen_thread(Thread* thread, PlaceholderTable::classloadAction action) {
 306     assert_lock_strong(SystemDictionary_lock);
 307     SeenThread* threadQ = actionToQueue(action);
 308     SeenThread* seen = threadQ;
 309     SeenThread* prev = NULL;
 310     while (seen) {
 311       if (thread == seen->thread()) {
 312         if (prev) {
 313           prev->set_next(seen->next());
 314         } else {
 315           set_threadQ(seen->next(), action);
 316         }
 317         if (seen->next()) {
 318           seen->next()->set_prev(prev);
 319         }
 320         delete seen;
 321         break;
 322       }
 323       prev = seen;
 324       seen = seen->next();
 325     }
 326     return (actionToQueue(action) == NULL);
 327   }
 328 
 329   // GC support
 330   // Applies "f->do_oop" to all root oops in the placeholder table.
 331   void classes_do(KlassClosure* closure);
 332 
 333   // Print method doesn't append a cr
 334   void print() const  PRODUCT_RETURN;
 335   void verify() const;
 336 };
 337 
 338 #endif // SHARE_VM_CLASSFILE_PLACEHOLDERS_HPP