placeholders.hpp 11.9 KB
Newer Older
D
duke 已提交
1
/*
2
 * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
D
duke 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
19 20 21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
D
duke 已提交
22 23 24
 *
 */

25 26 27
#ifndef SHARE_VM_CLASSFILE_PLACEHOLDERS_HPP
#define SHARE_VM_CLASSFILE_PLACEHOLDERS_HPP

28
#include "runtime/thread.hpp"
29 30
#include "utilities/hashtable.hpp"

D
duke 已提交
31 32 33 34 35 36
class PlaceholderEntry;

// Placeholder objects. These represent classes currently
// being loaded, as well as arrays of primitives.
//

Z
zgu 已提交
37
class PlaceholderTable : public TwoOopHashtable<Symbol*, mtClass> {
D
duke 已提交
38 39 40 41 42
  friend class VMStructs;

public:
  PlaceholderTable(int table_size);

43
  PlaceholderEntry* new_entry(int hash, Symbol* name, ClassLoaderData* loader_data, bool havesupername, Symbol* supername);
44
  void free_entry(PlaceholderEntry* entry);
D
duke 已提交
45 46

  PlaceholderEntry* bucket(int i) {
Z
zgu 已提交
47
    return (PlaceholderEntry*)Hashtable<Symbol*, mtClass>::bucket(i);
D
duke 已提交
48 49 50
  }

  PlaceholderEntry** bucket_addr(int i) {
Z
zgu 已提交
51
    return (PlaceholderEntry**)Hashtable<Symbol*, mtClass>::bucket_addr(i);
D
duke 已提交
52 53 54
  }

  void add_entry(int index, PlaceholderEntry* new_entry) {
Z
zgu 已提交
55
    Hashtable<Symbol*, mtClass>::add_entry(index, (HashtableEntry<Symbol*, mtClass>*)new_entry);
D
duke 已提交
56 57
  }

58
  void add_entry(int index, unsigned int hash, Symbol* name,
59
                ClassLoaderData* loader_data, bool havesupername, Symbol* supername);
D
duke 已提交
60

61 62
  // This returns a Symbol* to match type for SystemDictionary
  Symbol* find_entry(int index, unsigned int hash,
63
                       Symbol* name, ClassLoaderData* loader_data);
D
duke 已提交
64 65

  PlaceholderEntry* get_entry(int index, unsigned int hash,
66
                       Symbol* name, ClassLoaderData* loader_data);
D
duke 已提交
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87

// caller to create a placeholder entry must enumerate an action
// caller claims ownership of that action
// For parallel classloading:
// multiple LOAD_INSTANCE threads can proceed in parallel
// multiple LOAD_SUPER threads can proceed in parallel
// LOAD_SUPER needed to check for class circularity
// DEFINE_CLASS: ultimately define class must be single threaded
// on a class/classloader basis
// so the head of that queue owns the token
// and the rest of the threads return the result the first thread gets
 enum classloadAction {
    LOAD_INSTANCE = 1,             // calling load_instance_class
    LOAD_SUPER = 2,                // loading superclass for this class
    DEFINE_CLASS = 3               // find_or_define class
 };

  // find_and_add returns probe pointer - old or new
  // If no entry exists, add a placeholder entry and push SeenThread
  // If entry exists, reuse entry and push SeenThread for classloadAction
  PlaceholderEntry* find_and_add(int index, unsigned int hash,
88
                                 Symbol* name, ClassLoaderData* loader_data,
89
                                 classloadAction action, Symbol* supername,
D
duke 已提交
90 91 92
                                 Thread* thread);

  void remove_entry(int index, unsigned int hash,
93
                    Symbol* name, ClassLoaderData* loader_data);
D
duke 已提交
94 95 96

// Remove placeholder information
  void find_and_remove(int index, unsigned int hash,
97
                       Symbol* name, ClassLoaderData* loader_data, Thread* thread);
D
duke 已提交
98 99

  // GC support.
100
  void classes_do(KlassClosure* f);
D
duke 已提交
101 102

  // JVMTI support
103
  void entries_do(void f(Symbol*));
D
duke 已提交
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118

#ifndef PRODUCT
  void print();
#endif
  void verify();
};

// SeenThread objects represent list of threads that are
// currently performing a load action on a class.
// For class circularity, set before loading a superclass.
// For bootclasssearchpath, set before calling load_instance_class.
// Defining must be single threaded on a class/classloader basis
// For DEFINE_CLASS, the head of the queue owns the
// define token and the rest of the threads wait to return the
// result the first thread gets.
Z
zgu 已提交
119
class SeenThread: public CHeapObj<mtInternal> {
D
duke 已提交
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
private:
   Thread *_thread;
   SeenThread* _stnext;
   SeenThread* _stprev;
public:
   SeenThread(Thread *thread) {
       _thread = thread;
       _stnext = NULL;
       _stprev = NULL;
   }
   Thread* thread()                const { return _thread;}
   void set_thread(Thread *thread) { _thread = thread; }

   SeenThread* next()              const { return _stnext;}
   void set_next(SeenThread *seen) { _stnext = seen; }
   void set_prev(SeenThread *seen) { _stprev = seen; }

#ifndef PRODUCT
  void printActionQ() {
    SeenThread* seen = this;
    while (seen != NULL) {
      seen->thread()->print_value();
      tty->print(", ");
      seen = seen->next();
    }
  }
#endif // PRODUCT
};

// Placeholder objects represent classes currently being loaded.
// All threads examining the placeholder table must hold the
// SystemDictionary_lock, so we don't need special precautions
// on store ordering here.
// The system dictionary is the only user of this class.

Z
zgu 已提交
155
class PlaceholderEntry : public HashtableEntry<Symbol*, mtClass> {
D
duke 已提交
156 157 158 159
  friend class VMStructs;


 private:
160
  ClassLoaderData*  _loader_data;   // initiating loader
D
duke 已提交
161
  bool              _havesupername; // distinguish between null supername, and unknown
162
  Symbol*           _supername;
D
duke 已提交
163
  Thread*           _definer;       // owner of define token
164
  Klass*            _instanceKlass; // InstanceKlass from successful define
D
duke 已提交
165 166 167 168 169 170 171 172 173 174 175 176
  SeenThread*       _superThreadQ;  // doubly-linked queue of Threads loading a superclass for this class
  SeenThread*       _loadInstanceThreadQ;  // loadInstance thread
                                    // can be multiple threads if classloader object lock broken by application
                                    // or if classloader supports parallel classloading

  SeenThread*       _defineThreadQ; // queue of Threads trying to define this class
                                    // including _definer
                                    // _definer owns token
                                    // queue waits for and returns results from _definer

 public:
  // Simple accessors, used only by SystemDictionary
177
  Symbol*            klassname()           const { return literal(); }
D
duke 已提交
178

179 180
  ClassLoaderData*   loader_data()         const { return _loader_data; }
  void               set_loader_data(ClassLoaderData* loader_data) { _loader_data = loader_data; }
D
duke 已提交
181 182 183 184

  bool               havesupername()       const { return _havesupername; }
  void               set_havesupername(bool havesupername) { _havesupername = havesupername; }

185 186 187 188 189
  Symbol*            supername()           const { return _supername; }
  void               set_supername(Symbol* supername) {
    _supername = supername;
    if (_supername != NULL) _supername->increment_refcount();
  }
D
duke 已提交
190 191 192 193

  Thread*            definer()             const {return _definer; }
  void               set_definer(Thread* definer) { _definer = definer; }

H
hseigel 已提交
194 195
  Klass*             instance_klass()      const {return _instanceKlass; }
  void               set_instance_klass(Klass* ik) { _instanceKlass = ik; }
D
duke 已提交
196 197 198 199 200 201 202 203 204 205 206

  SeenThread*        superThreadQ()        const { return _superThreadQ; }
  void               set_superThreadQ(SeenThread* SeenThread) { _superThreadQ = SeenThread; }

  SeenThread*        loadInstanceThreadQ() const { return _loadInstanceThreadQ; }
  void               set_loadInstanceThreadQ(SeenThread* SeenThread) { _loadInstanceThreadQ = SeenThread; }

  SeenThread*        defineThreadQ()        const { return _defineThreadQ; }
  void               set_defineThreadQ(SeenThread* SeenThread) { _defineThreadQ = SeenThread; }

  PlaceholderEntry* next() const {
Z
zgu 已提交
207
    return (PlaceholderEntry*)HashtableEntry<Symbol*, mtClass>::next();
D
duke 已提交
208 209 210
  }

  PlaceholderEntry** next_addr() {
Z
zgu 已提交
211
    return (PlaceholderEntry**)HashtableEntry<Symbol*, mtClass>::next_addr();
D
duke 已提交
212 213 214 215
  }

  // Test for equality
  // Entries are unique for class/classloader name pair
216 217
  bool equals(Symbol* class_name, ClassLoaderData* loader) const {
    return (klassname() == class_name && loader_data() == loader);
D
duke 已提交
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
  }

  SeenThread* actionToQueue(PlaceholderTable::classloadAction action) {
    SeenThread* queuehead;
    switch (action) {
      case PlaceholderTable::LOAD_INSTANCE:
         queuehead = _loadInstanceThreadQ;
         break;
      case PlaceholderTable::LOAD_SUPER:
         queuehead = _superThreadQ;
         break;
      case PlaceholderTable::DEFINE_CLASS:
         queuehead = _defineThreadQ;
         break;
      default: Unimplemented();
    }
    return queuehead;
  }

  void set_threadQ(SeenThread* seenthread, PlaceholderTable::classloadAction action) {
    switch (action) {
      case PlaceholderTable::LOAD_INSTANCE:
         _loadInstanceThreadQ = seenthread;
         break;
      case PlaceholderTable::LOAD_SUPER:
         _superThreadQ = seenthread;
         break;
      case PlaceholderTable::DEFINE_CLASS:
         _defineThreadQ = seenthread;
         break;
      default: Unimplemented();
    }
    return;
  }

  bool super_load_in_progress() {
     return (_superThreadQ != NULL);
  }

  bool instance_load_in_progress() {
    return (_loadInstanceThreadQ != NULL);
  }

  bool define_class_in_progress() {
    return (_defineThreadQ != NULL);
  }

// Doubly-linked list of Threads per action for class/classloader pair
// Class circularity support: links in thread before loading superclass
// bootstrapsearchpath support: links in a thread before load_instance_class
// definers: use as queue of define requestors, including owner of
// define token. Appends for debugging of requestor order
  void add_seen_thread(Thread* thread, PlaceholderTable::classloadAction action) {
    assert_lock_strong(SystemDictionary_lock);
    SeenThread* threadEntry = new SeenThread(thread);
    SeenThread* seen = actionToQueue(action);

    if (seen == NULL) {
      set_threadQ(threadEntry, action);
      return;
    }
    SeenThread* next;
    while ((next = seen->next()) != NULL) {
      seen = next;
    }
    seen->set_next(threadEntry);
    threadEntry->set_prev(seen);
    return;
  }

  bool check_seen_thread(Thread* thread, PlaceholderTable::classloadAction action) {
    assert_lock_strong(SystemDictionary_lock);
    SeenThread* threadQ = actionToQueue(action);
    SeenThread* seen = threadQ;
    while (seen) {
      if (thread == seen->thread()) {
        return true;
      }
      seen = seen->next();
    }
    return false;
  }

  // returns true if seenthreadQ is now empty
  // Note, caller must ensure probe still exists while holding
  // SystemDictionary_lock
  // ignores if cleanup has already been done
  // if found, deletes SeenThread
  bool remove_seen_thread(Thread* thread, PlaceholderTable::classloadAction action) {
    assert_lock_strong(SystemDictionary_lock);
    SeenThread* threadQ = actionToQueue(action);
    SeenThread* seen = threadQ;
    SeenThread* prev = NULL;
    while (seen) {
      if (thread == seen->thread()) {
        if (prev) {
          prev->set_next(seen->next());
        } else {
          set_threadQ(seen->next(), action);
        }
        if (seen->next()) {
          seen->next()->set_prev(prev);
        }
        delete seen;
        break;
      }
      prev = seen;
      seen = seen->next();
    }
    return (actionToQueue(action) == NULL);
  }

  // GC support
  // Applies "f->do_oop" to all root oops in the placeholder table.
332
  void classes_do(KlassClosure* closure);
D
duke 已提交
333 334 335 336 337

  // Print method doesn't append a cr
  void print() const  PRODUCT_RETURN;
  void verify() const;
};
338 339

#endif // SHARE_VM_CLASSFILE_PLACEHOLDERS_HPP