Tue, 15 Mar 2011 06:35:10 -0700
7024234: 2/3 jvmti tests fail assert(!_oops_are_stale) failed: oops are stale on Win-AMD64
Summary: Move initialization of the '_instance' field to avoid race with ServiceThread start.
Reviewed-by: dholmes, kamg, never, dsamersoff, ysr, coleenp, acorn
duke@435 | 1 | /* |
trims@1907 | 2 | * Copyright (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved. |
duke@435 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
duke@435 | 4 | * |
duke@435 | 5 | * This code is free software; you can redistribute it and/or modify it |
duke@435 | 6 | * under the terms of the GNU General Public License version 2 only, as |
duke@435 | 7 | * published by the Free Software Foundation. |
duke@435 | 8 | * |
duke@435 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
duke@435 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
duke@435 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
duke@435 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
duke@435 | 13 | * accompanied this code). |
duke@435 | 14 | * |
duke@435 | 15 | * You should have received a copy of the GNU General Public License version |
duke@435 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
duke@435 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
duke@435 | 18 | * |
trims@1907 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
trims@1907 | 20 | * or visit www.oracle.com if you need additional information or have any |
trims@1907 | 21 | * questions. |
duke@435 | 22 | * |
duke@435 | 23 | */ |
duke@435 | 24 | |
stefank@2314 | 25 | #ifndef SHARE_VM_RUNTIME_JNIHANDLES_HPP |
stefank@2314 | 26 | #define SHARE_VM_RUNTIME_JNIHANDLES_HPP |
stefank@2314 | 27 | |
stefank@2314 | 28 | #include "runtime/handles.hpp" |
stefank@2314 | 29 | #include "utilities/top.hpp" |
stefank@2314 | 30 | |
duke@435 | 31 | class JNIHandleBlock; |
duke@435 | 32 | |
duke@435 | 33 | |
duke@435 | 34 | // Interface for creating and resolving local/global JNI handles |
duke@435 | 35 | |
duke@435 | 36 | class JNIHandles : AllStatic { |
duke@435 | 37 | friend class VMStructs; |
duke@435 | 38 | private: |
duke@435 | 39 | static JNIHandleBlock* _global_handles; // First global handle block |
duke@435 | 40 | static JNIHandleBlock* _weak_global_handles; // First weak global handle block |
duke@435 | 41 | static oop _deleted_handle; // Sentinel marking deleted handles |
duke@435 | 42 | |
duke@435 | 43 | public: |
duke@435 | 44 | // Resolve handle into oop |
duke@435 | 45 | inline static oop resolve(jobject handle); |
duke@435 | 46 | // Resolve externally provided handle into oop with some guards |
duke@435 | 47 | inline static oop resolve_external_guard(jobject handle); |
duke@435 | 48 | // Resolve handle into oop, result guaranteed not to be null |
duke@435 | 49 | inline static oop resolve_non_null(jobject handle); |
duke@435 | 50 | |
duke@435 | 51 | // Local handles |
duke@435 | 52 | static jobject make_local(oop obj); |
duke@435 | 53 | static jobject make_local(JNIEnv* env, oop obj); // Fast version when env is known |
duke@435 | 54 | static jobject make_local(Thread* thread, oop obj); // Even faster version when current thread is known |
duke@435 | 55 | inline static void destroy_local(jobject handle); |
duke@435 | 56 | |
duke@435 | 57 | // Global handles |
duke@435 | 58 | static jobject make_global(Handle obj); |
duke@435 | 59 | static void destroy_global(jobject handle); |
duke@435 | 60 | |
duke@435 | 61 | // Weak global handles |
duke@435 | 62 | static jobject make_weak_global(Handle obj); |
duke@435 | 63 | static void destroy_weak_global(jobject handle); |
duke@435 | 64 | |
duke@435 | 65 | // jmethodID handling (as Weak global handles). |
duke@435 | 66 | // Because the useful life-span of a jmethodID cannot be determined, once created they are |
duke@435 | 67 | // never reclaimed. The methods to which they refer, however, can be GC'ed away if the class |
duke@435 | 68 | // is unloaded or if the method is made obsolete or deleted -- in these cases, the jmethodID |
duke@435 | 69 | // refers to NULL (as is the case for any weak reference). |
duke@435 | 70 | static jmethodID make_jmethod_id(methodHandle mh); |
duke@435 | 71 | static void destroy_jmethod_id(jmethodID mid); |
dcubed@1890 | 72 | // Use resolve_jmethod_id() in situations where the caller is expected |
dcubed@1890 | 73 | // to provide a valid jmethodID; the only sanity checks are in asserts; |
dcubed@1890 | 74 | // result guaranteed not to be NULL. |
duke@435 | 75 | inline static methodOop resolve_jmethod_id(jmethodID mid); |
dcubed@1890 | 76 | // Use checked_resolve_jmethod_id() in situations where the caller |
dcubed@1890 | 77 | // should provide a valid jmethodID, but might not. NULL is returned |
dcubed@1890 | 78 | // when the jmethodID does not refer to a valid method. |
dcubed@1890 | 79 | inline static methodOop checked_resolve_jmethod_id(jmethodID mid); |
duke@435 | 80 | static void change_method_associated_with_jmethod_id(jmethodID jmid, methodHandle mh); |
duke@435 | 81 | |
duke@435 | 82 | // Sentinel marking deleted handles in block. Note that we cannot store NULL as |
duke@435 | 83 | // the sentinel, since clearing weak global JNI refs are done by storing NULL in |
duke@435 | 84 | // the handle. The handle may not be reused before destroy_weak_global is called. |
duke@435 | 85 | static oop deleted_handle() { return _deleted_handle; } |
duke@435 | 86 | |
duke@435 | 87 | // Initialization |
duke@435 | 88 | static void initialize(); |
duke@435 | 89 | |
duke@435 | 90 | // Debugging |
duke@435 | 91 | static void print_on(outputStream* st); |
duke@435 | 92 | static void print() { print_on(tty); } |
duke@435 | 93 | static void verify(); |
duke@435 | 94 | static bool is_local_handle(Thread* thread, jobject handle); |
duke@435 | 95 | static bool is_frame_handle(JavaThread* thr, jobject obj); |
duke@435 | 96 | static bool is_global_handle(jobject handle); |
duke@435 | 97 | static bool is_weak_global_handle(jobject handle); |
duke@435 | 98 | static long global_handle_memory_usage(); |
duke@435 | 99 | static long weak_global_handle_memory_usage(); |
duke@435 | 100 | |
duke@435 | 101 | // Garbage collection support(global handles only, local handles are traversed from thread) |
duke@435 | 102 | // Traversal of regular global handles |
duke@435 | 103 | static void oops_do(OopClosure* f); |
duke@435 | 104 | // Traversal of weak global handles. Unreachable oops are cleared. |
duke@435 | 105 | static void weak_oops_do(BoolObjectClosure* is_alive, OopClosure* f); |
duke@435 | 106 | }; |
duke@435 | 107 | |
duke@435 | 108 | |
duke@435 | 109 | |
duke@435 | 110 | // JNI handle blocks holding local/global JNI handles |
duke@435 | 111 | |
duke@435 | 112 | class JNIHandleBlock : public CHeapObj { |
duke@435 | 113 | friend class VMStructs; |
never@1445 | 114 | friend class CppInterpreter; |
never@1445 | 115 | |
duke@435 | 116 | private: |
duke@435 | 117 | enum SomeConstants { |
duke@435 | 118 | block_size_in_oops = 32 // Number of handles per handle block |
duke@435 | 119 | }; |
duke@435 | 120 | |
duke@435 | 121 | oop _handles[block_size_in_oops]; // The handles |
duke@435 | 122 | int _top; // Index of next unused handle |
duke@435 | 123 | JNIHandleBlock* _next; // Link to next block |
duke@435 | 124 | |
duke@435 | 125 | // The following instance variables are only used by the first block in a chain. |
duke@435 | 126 | // Having two types of blocks complicates the code and the space overhead in negligble. |
duke@435 | 127 | JNIHandleBlock* _last; // Last block in use |
duke@435 | 128 | JNIHandleBlock* _pop_frame_link; // Block to restore on PopLocalFrame call |
duke@435 | 129 | oop* _free_list; // Handle free list |
duke@435 | 130 | int _allocate_before_rebuild; // Number of blocks to allocate before rebuilding free list |
duke@435 | 131 | |
duke@435 | 132 | #ifndef PRODUCT |
duke@435 | 133 | JNIHandleBlock* _block_list_link; // Link for list below |
duke@435 | 134 | static JNIHandleBlock* _block_list; // List of all allocated blocks (for debugging only) |
duke@435 | 135 | #endif |
duke@435 | 136 | |
duke@435 | 137 | static JNIHandleBlock* _block_free_list; // Free list of currently unused blocks |
duke@435 | 138 | static int _blocks_allocated; // For debugging/printing |
duke@435 | 139 | |
duke@435 | 140 | // Fill block with bad_handle values |
duke@435 | 141 | void zap(); |
duke@435 | 142 | |
never@1445 | 143 | protected: |
duke@435 | 144 | // No more handles in the both the current and following blocks |
duke@435 | 145 | void clear() { _top = 0; } |
duke@435 | 146 | |
never@1445 | 147 | private: |
duke@435 | 148 | // Free list computation |
duke@435 | 149 | void rebuild_free_list(); |
duke@435 | 150 | |
duke@435 | 151 | public: |
duke@435 | 152 | // Handle allocation |
duke@435 | 153 | jobject allocate_handle(oop obj); |
duke@435 | 154 | |
duke@435 | 155 | // Block allocation and block free list management |
duke@435 | 156 | static JNIHandleBlock* allocate_block(Thread* thread = NULL); |
duke@435 | 157 | static void release_block(JNIHandleBlock* block, Thread* thread = NULL); |
duke@435 | 158 | |
duke@435 | 159 | // JNI PushLocalFrame/PopLocalFrame support |
duke@435 | 160 | JNIHandleBlock* pop_frame_link() const { return _pop_frame_link; } |
duke@435 | 161 | void set_pop_frame_link(JNIHandleBlock* block) { _pop_frame_link = block; } |
duke@435 | 162 | |
duke@435 | 163 | // Stub generator support |
duke@435 | 164 | static int top_offset_in_bytes() { return offset_of(JNIHandleBlock, _top); } |
duke@435 | 165 | |
duke@435 | 166 | // Garbage collection support |
duke@435 | 167 | // Traversal of regular handles |
duke@435 | 168 | void oops_do(OopClosure* f); |
duke@435 | 169 | // Traversal of weak handles. Unreachable oops are cleared. |
duke@435 | 170 | void weak_oops_do(BoolObjectClosure* is_alive, OopClosure* f); |
duke@435 | 171 | |
duke@435 | 172 | // Debugging |
duke@435 | 173 | bool chain_contains(jobject handle) const; // Does this block or following blocks contain handle |
duke@435 | 174 | bool contains(jobject handle) const; // Does this block contain handle |
duke@435 | 175 | int length() const; // Length of chain starting with this block |
duke@435 | 176 | long memory_usage() const; |
duke@435 | 177 | #ifndef PRODUCT |
duke@435 | 178 | static bool any_contains(jobject handle); // Does any block currently in use contain handle |
duke@435 | 179 | static void print_statistics(); |
duke@435 | 180 | #endif |
duke@435 | 181 | }; |
duke@435 | 182 | |
duke@435 | 183 | |
duke@435 | 184 | inline oop JNIHandles::resolve(jobject handle) { |
duke@435 | 185 | oop result = (handle == NULL ? (oop)NULL : *(oop*)handle); |
duke@435 | 186 | assert(result != NULL || (handle == NULL || !CheckJNICalls || is_weak_global_handle(handle)), "Invalid value read from jni handle"); |
duke@435 | 187 | assert(result != badJNIHandle, "Pointing to zapped jni handle area"); |
duke@435 | 188 | return result; |
duke@435 | 189 | }; |
duke@435 | 190 | |
duke@435 | 191 | |
duke@435 | 192 | inline oop JNIHandles::resolve_external_guard(jobject handle) { |
duke@435 | 193 | if (handle == NULL) return NULL; |
duke@435 | 194 | oop result = *(oop*)handle; |
duke@435 | 195 | if (result == NULL || result == badJNIHandle) return NULL; |
duke@435 | 196 | return result; |
duke@435 | 197 | }; |
duke@435 | 198 | |
duke@435 | 199 | |
duke@435 | 200 | inline oop JNIHandles::resolve_non_null(jobject handle) { |
duke@435 | 201 | assert(handle != NULL, "JNI handle should not be null"); |
duke@435 | 202 | oop result = *(oop*)handle; |
duke@435 | 203 | assert(result != NULL, "Invalid value read from jni handle"); |
duke@435 | 204 | assert(result != badJNIHandle, "Pointing to zapped jni handle area"); |
duke@435 | 205 | // Don't let that private _deleted_handle object escape into the wild. |
duke@435 | 206 | assert(result != deleted_handle(), "Used a deleted global handle."); |
duke@435 | 207 | return result; |
duke@435 | 208 | }; |
duke@435 | 209 | |
duke@435 | 210 | inline methodOop JNIHandles::resolve_jmethod_id(jmethodID mid) { |
duke@435 | 211 | return (methodOop) resolve_non_null((jobject)mid); |
duke@435 | 212 | }; |
duke@435 | 213 | |
duke@435 | 214 | inline methodOop JNIHandles::checked_resolve_jmethod_id(jmethodID mid) { |
dcubed@1890 | 215 | oop o = resolve_external_guard((jobject) mid); |
dcubed@1890 | 216 | if (o == NULL || !o->is_method()) { |
dcubed@1352 | 217 | return (methodOop) NULL; |
dcubed@1352 | 218 | } |
dcubed@1352 | 219 | |
dcubed@1352 | 220 | return (methodOop) o; |
duke@435 | 221 | }; |
duke@435 | 222 | |
duke@435 | 223 | |
duke@435 | 224 | inline void JNIHandles::destroy_local(jobject handle) { |
duke@435 | 225 | if (handle != NULL) { |
duke@435 | 226 | *((oop*)handle) = deleted_handle(); // Mark the handle as deleted, allocate will reuse it |
duke@435 | 227 | } |
duke@435 | 228 | } |
stefank@2314 | 229 | |
stefank@2314 | 230 | #endif // SHARE_VM_RUNTIME_JNIHANDLES_HPP |