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