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_OOPS_MARKWORD_HPP
26 #define SHARE_OOPS_MARKWORD_HPP
27
28 #include "metaprogramming/integralConstant.hpp"
29 #include "metaprogramming/primitiveConversions.hpp"
30 #include "oops/oopsHierarchy.hpp"
31 #include "runtime/globals.hpp"
32
33 // The markWord describes the header of an object.
34 //
35 // Bit-format of an object header (most significant first, big endian layout below):
36 //
37 // 32 bits:
38 // --------
39 // hash:25 ------------>| age:4 biased_lock:1 lock:2 (normal object)
40 // JavaThread*:23 epoch:2 age:4 biased_lock:1 lock:2 (biased object)
41 //
42 // 64 bits:
43 // --------
44 // unused:25 hash:31 -->| unused_gap:1 age:4 biased_lock:1 lock:2 (normal object)
45 // JavaThread*:54 epoch:2 unused_gap:1 age:4 biased_lock:1 lock:2 (biased object)
46 //
47 // - hash contains the identity hash value: largest value is
48 // 31 bits, see os::random(). Also, 64-bit vm's require
49 // a hash value no bigger than 32 bits because they will not
50 // properly generate a mask larger than that: see library_call.cpp
51 //
52 // - the biased lock pattern is used to bias a lock toward a given
53 // thread. When this pattern is set in the low three bits, the lock
54 // is either biased toward a given thread or "anonymously" biased,
55 // indicating that it is possible for it to be biased. When the
56 // lock is biased toward a given thread, locking and unlocking can
57 // be performed by that thread without using atomic operations.
58 // When a lock's bias is revoked, it reverts back to the normal
59 // locking scheme described below.
60 //
61 // Note that we are overloading the meaning of the "unlocked" state
62 // of the header. Because we steal a bit from the age we can
63 // guarantee that the bias pattern will never be seen for a truly
64 // unlocked object.
65 //
66 // Note also that the biased state contains the age bits normally
67 // contained in the object header. Large increases in scavenge
68 // times were seen when these bits were absent and an arbitrary age
69 // assigned to all biased objects, because they tended to consume a
70 // significant fraction of the eden semispaces and were not
71 // promoted promptly, causing an increase in the amount of copying
72 // performed. The runtime system aligns all JavaThread* pointers to
73 // a very large value (currently 128 bytes (32bVM) or 256 bytes (64bVM))
74 // to make room for the age bits & the epoch bits (used in support of
75 // biased locking).
76 //
77 // [JavaThread* | epoch | age | 1 | 01] lock is biased toward given thread
78 // [0 | epoch | age | 1 | 01] lock is anonymously biased
79 //
80 // - the two lock bits are used to describe three states: locked/unlocked and monitor.
81 //
82 // [ptr | 00] locked ptr points to real header on stack
83 // [header | 0 | 01] unlocked regular object header
84 // [ptr | 10] monitor inflated lock (header is wapped out)
85 // [ptr | 11] marked used to mark an object
86 // [0 ............ 0| 00] inflating inflation in progress
87 //
88 // We assume that stack/thread pointers have the lowest two bits cleared.
89 //
90 // - INFLATING() is a distinguished markword value of all zeros that is
91 // used when inflating an existing stack-lock into an ObjectMonitor.
92 // See below for is_being_inflated() and INFLATING().
93
94 class BasicLock;
95 class ObjectMonitor;
96 class JavaThread;
97 class outputStream;
98
99 class markWord {
100 private:
101 uintptr_t _value;
102
103 public:
104 explicit markWord(uintptr_t value) : _value(value) {}
105
106 markWord() { /* uninitialized */}
107
108 // It is critical for performance that this class be trivially
109 // destructable, copyable, and assignable.
110
111 static markWord from_pointer(void* ptr) {
112 return markWord((uintptr_t)ptr);
113 }
114 void* to_pointer() const {
115 return (void*)_value;
116 }
117
118 bool operator==(const markWord& other) const {
119 return _value == other._value;
120 }
121 bool operator!=(const markWord& other) const {
122 return !operator==(other);
123 }
124
125 // Conversion
126 uintptr_t value() const { return _value; }
127
128 // Constants
129 static const int age_bits = 4;
130 static const int lock_bits = 2;
131 static const int biased_lock_bits = 1;
132 static const int max_hash_bits = BitsPerWord - age_bits - lock_bits - biased_lock_bits;
133 static const int hash_bits = max_hash_bits > 31 ? 31 : max_hash_bits;
134 static const int unused_gap_bits = LP64_ONLY(1) NOT_LP64(0);
135 static const int epoch_bits = 2;
136
137 // The biased locking code currently requires that the age bits be
138 // contiguous to the lock bits.
139 static const int lock_shift = 0;
140 static const int biased_lock_shift = lock_bits;
141 static const int age_shift = lock_bits + biased_lock_bits;
142 static const int unused_gap_shift = age_shift + age_bits;
143 static const int hash_shift = unused_gap_shift + unused_gap_bits;
144 static const int epoch_shift = hash_shift;
145
146 static const uintptr_t lock_mask = right_n_bits(lock_bits);
147 static const uintptr_t lock_mask_in_place = lock_mask << lock_shift;
148 static const uintptr_t biased_lock_mask = right_n_bits(lock_bits + biased_lock_bits);
149 static const uintptr_t biased_lock_mask_in_place= biased_lock_mask << lock_shift;
150 static const uintptr_t biased_lock_bit_in_place = 1 << biased_lock_shift;
151 static const uintptr_t age_mask = right_n_bits(age_bits);
152 static const uintptr_t age_mask_in_place = age_mask << age_shift;
153 static const uintptr_t epoch_mask = right_n_bits(epoch_bits);
154 static const uintptr_t epoch_mask_in_place = epoch_mask << epoch_shift;
155
156 static const uintptr_t hash_mask = right_n_bits(hash_bits);
157 static const uintptr_t hash_mask_in_place = hash_mask << hash_shift;
158
159 // Alignment of JavaThread pointers encoded in object header required by biased locking
160 static const size_t biased_lock_alignment = 2 << (epoch_shift + epoch_bits);
161
162 static const uintptr_t locked_value = 0;
163 static const uintptr_t unlocked_value = 1;
164 static const uintptr_t monitor_value = 2;
165 static const uintptr_t marked_value = 3;
166 static const uintptr_t biased_lock_pattern = 5;
167
168 static const uintptr_t no_hash = 0 ; // no hash value assigned
169 static const uintptr_t no_hash_in_place = (address_word)no_hash << hash_shift;
170 static const uintptr_t no_lock_in_place = unlocked_value;
171
172 static const uint max_age = age_mask;
173
174 static const int max_bias_epoch = epoch_mask;
175
176 // Creates a markWord with all bits set to zero.
177 static markWord zero() { return markWord(uintptr_t(0)); }
219 return (mask_bits(value(), lock_mask_in_place) != unlocked_value);
220 }
221 bool is_unlocked() const {
222 return (mask_bits(value(), biased_lock_mask_in_place) == unlocked_value);
223 }
224 bool is_marked() const {
225 return (mask_bits(value(), lock_mask_in_place) == marked_value);
226 }
227 bool is_neutral() const { return (mask_bits(value(), biased_lock_mask_in_place) == unlocked_value); }
228
229 // Special temporary state of the markWord while being inflated.
230 // Code that looks at mark outside a lock need to take this into account.
231 bool is_being_inflated() const { return (value() == 0); }
232
233 // Distinguished markword value - used when inflating over
234 // an existing stack-lock. 0 indicates the markword is "BUSY".
235 // Lockword mutators that use a LD...CAS idiom should always
236 // check for and avoid overwriting a 0 value installed by some
237 // other thread. (They should spin or block instead. The 0 value
238 // is transient and *should* be short-lived).
239 static markWord INFLATING() { return zero(); } // inflate-in-progress
240
241 // Should this header be preserved during GC?
242 inline bool must_be_preserved(const oopDesc* obj) const;
243
244 // Should this header (including its age bits) be preserved in the
245 // case of a promotion failure during scavenge?
246 // Note that we special case this situation. We want to avoid
247 // calling BiasedLocking::preserve_marks()/restore_marks() (which
248 // decrease the number of mark words that need to be preserved
249 // during GC) during each scavenge. During scavenges in which there
250 // is no promotion failure, we actually don't need to call the above
251 // routines at all, since we don't mutate and re-initialize the
252 // marks of promoted objects using init_mark(). However, during
253 // scavenges which result in promotion failure, we do re-initialize
254 // the mark words of objects, meaning that we should have called
255 // these mark word preservation routines. Currently there's no good
256 // place in which to call them in any of the scavengers (although
257 // guarded by appropriate locks we could make one), but the
258 // observation is that promotion failures are quite rare and
259 // reducing the number of mark words preserved during them isn't a
260 // high priority.
261 inline bool must_be_preserved_for_promotion_failure(const oopDesc* obj) const;
262
263 // WARNING: The following routines are used EXCLUSIVELY by
264 // synchronization functions. They are not really gc safe.
265 // They must get updated if markWord layout get changed.
266 markWord set_unlocked() const {
267 return markWord(value() | unlocked_value);
268 }
269 bool has_locker() const {
270 return ((value() & lock_mask_in_place) == locked_value);
271 }
272 BasicLock* locker() const {
273 assert(has_locker(), "check");
274 return (BasicLock*) value();
275 }
276 bool has_monitor() const {
277 return ((value() & monitor_value) != 0);
278 }
279 ObjectMonitor* monitor() const {
280 assert(has_monitor(), "check");
281 // Use xor instead of &~ to provide one extra tag-bit check.
282 return (ObjectMonitor*) (value() ^ monitor_value);
283 }
284 bool has_displaced_mark_helper() const {
285 return ((value() & unlocked_value) == 0);
286 }
287 markWord displaced_mark_helper() const;
288 void set_displaced_mark_helper(markWord m) const;
289 markWord copy_set_hash(intptr_t hash) const {
290 uintptr_t tmp = value() & (~hash_mask_in_place);
291 tmp |= ((hash & hash_mask) << hash_shift);
292 return markWord(tmp);
293 }
294 // it is only used to be stored into BasicLock as the
295 // indicator that the lock is using heavyweight monitor
296 static markWord unused_mark() {
297 return markWord(marked_value);
298 }
299 // the following two functions create the markWord to be
300 // stored into object header, it encodes monitor info
301 static markWord encode(BasicLock* lock) {
302 return from_pointer(lock);
303 }
304 static markWord encode(ObjectMonitor* monitor) {
305 uintptr_t tmp = (uintptr_t) monitor;
306 return markWord(tmp | monitor_value);
307 }
308 static markWord encode(JavaThread* thread, uint age, int bias_epoch) {
309 uintptr_t tmp = (uintptr_t) thread;
310 assert(UseBiasedLocking && ((tmp & (epoch_mask_in_place | age_mask_in_place | biased_lock_mask_in_place)) == 0), "misaligned JavaThread pointer");
311 assert(age <= max_age, "age too large");
312 assert(bias_epoch <= max_bias_epoch, "bias epoch too large");
313 return markWord(tmp | (bias_epoch << epoch_shift) | (age << age_shift) | biased_lock_pattern);
314 }
315
316 // used to encode pointers during GC
317 markWord clear_lock_bits() { return markWord(value() & ~lock_mask_in_place); }
318
319 // age operations
320 markWord set_marked() { return markWord((value() & ~lock_mask_in_place) | marked_value); }
321 markWord set_unmarked() { return markWord((value() & ~lock_mask_in_place) | unlocked_value); }
322
323 uint age() const { return mask_bits(value() >> age_shift, age_mask); }
324 markWord set_age(uint v) const {
325 assert((v & ~age_mask) == 0, "shouldn't overflow age field");
326 return markWord((value() & ~age_mask_in_place) | ((v & age_mask) << age_shift));
327 }
328 markWord incr_age() const { return age() == max_age ? markWord(_value) : set_age(age() + 1); }
329
330 // hash operations
331 intptr_t hash() const {
332 return mask_bits(value() >> hash_shift, hash_mask);
333 }
334
335 bool has_no_hash() const {
336 return hash() == no_hash;
337 }
338
339 // Prototype mark for initialization
340 static markWord prototype() {
341 return markWord( no_hash_in_place | no_lock_in_place );
342 }
343
344 // Helper function for restoration of unmarked mark oops during GC
345 static inline markWord prototype_for_klass(const Klass* klass);
346
347 // Debugging
348 void print_on(outputStream* st, bool print_monitor_info = true) const;
349
350 // Prepare address of oop for placement into mark
351 inline static markWord encode_pointer_as_mark(void* p) { return from_pointer(p).set_marked(); }
352
353 // Recover address of oop from encoded form used in mark
354 inline void* decode_pointer() { if (UseBiasedLocking && has_bias_pattern()) return NULL; return (void*)clear_lock_bits().value(); }
355 };
356
357 // Support atomic operations.
358 template<>
359 struct PrimitiveConversions::Translate<markWord> : public TrueType {
360 typedef markWord Value;
361 typedef uintptr_t Decayed;
362
363 static Decayed decay(const Value& x) { return x.value(); }
364 static Value recover(Decayed x) { return Value(x); }
365 };
366
367 #endif // SHARE_OOPS_MARKWORD_HPP
|
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_OOPS_MARKWORD_HPP
26 #define SHARE_OOPS_MARKWORD_HPP
27
28 #include "gc/shared/gc_globals.hpp"
29 #include "metaprogramming/integralConstant.hpp"
30 #include "metaprogramming/primitiveConversions.hpp"
31 #include "oops/oopsHierarchy.hpp"
32 #include "runtime/globals.hpp"
33
34 // The markWord describes the header of an object.
35 //
36 // Bit-format of an object header (most significant first, big endian layout below):
37 //
38 // 32 bits:
39 // --------
40 // hash:25 ------------>| age:4 biased_lock:1 lock:2 (normal object)
41 // JavaThread*:23 epoch:2 age:4 biased_lock:1 lock:2 (biased object)
42 //
43 // 64 bits:
44 // --------
45 // unused:25 hash:31 -->| unused_gap:1 age:4 biased_lock:1 lock:2 (normal object)
46 // JavaThread*:54 epoch:2 unused_gap:1 age:4 biased_lock:1 lock:2 (biased object)
47 //
48 // 64 bits (with compact headers):
49 // -------------------------------
50 // nklass:32 hash:25 -->| unused_gap:1 age:4 self-fwded:1 lock:2 (normal object)
51 //
52 // - hash contains the identity hash value: largest value is
53 // 31 bits, see os::random(). Also, 64-bit vm's require
54 // a hash value no bigger than 32 bits because they will not
55 // properly generate a mask larger than that: see library_call.cpp
56 //
57 // - the biased lock pattern is used to bias a lock toward a given
58 // thread. When this pattern is set in the low three bits, the lock
59 // is either biased toward a given thread or "anonymously" biased,
60 // indicating that it is possible for it to be biased. When the
61 // lock is biased toward a given thread, locking and unlocking can
62 // be performed by that thread without using atomic operations.
63 // When a lock's bias is revoked, it reverts back to the normal
64 // locking scheme described below.
65 //
66 // Note that we are overloading the meaning of the "unlocked" state
67 // of the header. Because we steal a bit from the age we can
68 // guarantee that the bias pattern will never be seen for a truly
69 // unlocked object.
70 //
71 // Note also that the biased state contains the age bits normally
72 // contained in the object header. Large increases in scavenge
73 // times were seen when these bits were absent and an arbitrary age
74 // assigned to all biased objects, because they tended to consume a
75 // significant fraction of the eden semispaces and were not
76 // promoted promptly, causing an increase in the amount of copying
77 // performed. The runtime system aligns all JavaThread* pointers to
78 // a very large value (currently 128 bytes (32bVM) or 256 bytes (64bVM))
79 // to make room for the age bits & the epoch bits (used in support of
80 // biased locking).
81 //
82 // [JavaThread* | epoch | age | 1 | 01] lock is biased toward given thread
83 // [0 | epoch | age | 1 | 01] lock is anonymously biased
84 //
85 // - the two lock bits are used to describe three states: locked/unlocked and monitor.
86 //
87 // [ptr | 00] locked ptr points to real header on stack
88 // [header | 0 | 01] unlocked regular object header
89 // [ptr | 10] monitor inflated lock (header is swapped out)
90 // [ptr | 11] marked used to mark an object
91 // [0 ............ 0| 00] inflating inflation in progress (stack-locking in use)
92 //
93 // We assume that stack/thread pointers have the lowest two bits cleared.
94 //
95 // - INFLATING() is a distinguished markword value of all zeros that is
96 // used when inflating an existing stack-lock into an ObjectMonitor.
97 // See below for is_being_inflated() and INFLATING().
98
99 class BasicLock;
100 class ObjectMonitor;
101 class JavaThread;
102 class Klass;
103 class outputStream;
104
105 class markWord {
106 private:
107 uintptr_t _value;
108
109 public:
110 explicit markWord(uintptr_t value) : _value(value) {}
111
112 markWord() { /* uninitialized */}
113
114 // It is critical for performance that this class be trivially
115 // destructable, copyable, and assignable.
116
117 static markWord from_pointer(void* ptr) {
118 return markWord((uintptr_t)ptr);
119 }
120 void* to_pointer() const {
121 return (void*)_value;
122 }
123
124 bool operator==(const markWord& other) const {
125 return _value == other._value;
126 }
127 bool operator!=(const markWord& other) const {
128 return !operator==(other);
129 }
130
131 // Conversion
132 uintptr_t value() const { return _value; }
133
134 // Constants
135 static const int age_bits = 4;
136 static const int lock_bits = 2;
137 static const int biased_lock_bits = 1;
138 static const int self_forwarded_bits = 1;
139 static const int max_hash_bits = BitsPerWord - age_bits - lock_bits - self_forwarded_bits;
140 static const int hash_bits = max_hash_bits > 31 ? 31 : max_hash_bits;
141 static const int hash_bits_compact = max_hash_bits > 25 ? 25 : max_hash_bits;
142 // Used only without compact headers.
143 static const int unused_gap_bits = LP64_ONLY(1) NOT_LP64(0);
144 static const int epoch_bits = 2;
145 #ifdef _LP64
146 // Used only with compact headers.
147 static const int klass_bits = 32;
148 #endif
149
150 // The biased locking code currently requires that the age bits be
151 // contiguous to the lock bits.
152 static const int lock_shift = 0;
153 static const int biased_lock_shift = lock_bits;
154 static const int self_forwarded_shift = lock_shift + lock_bits;
155 static const int age_shift = self_forwarded_shift + self_forwarded_bits;
156 static const int unused_gap_shift = age_shift + age_bits;
157 static const int hash_shift = unused_gap_shift + unused_gap_bits;
158 static const int hash_shift_compact = age_shift + age_bits;
159 #ifdef _LP64
160 // Used only with compact headers.
161 static const int klass_shift = hash_shift_compact + hash_bits_compact;
162 #endif
163 static const int epoch_shift = hash_shift;
164
165 static const uintptr_t lock_mask = right_n_bits(lock_bits);
166 static const uintptr_t lock_mask_in_place = lock_mask << lock_shift;
167 static const uintptr_t biased_lock_mask = right_n_bits(lock_bits + biased_lock_bits);
168 static const uintptr_t biased_lock_mask_in_place= biased_lock_mask << lock_shift;
169 static const uintptr_t biased_lock_bit_in_place = 1 << biased_lock_shift;
170 static const uintptr_t self_forwarded_mask = right_n_bits(self_forwarded_bits);
171 static const uintptr_t self_forwarded_mask_in_place = self_forwarded_mask << self_forwarded_shift;
172 static const uintptr_t age_mask = right_n_bits(age_bits);
173 static const uintptr_t age_mask_in_place = age_mask << age_shift;
174 static const uintptr_t epoch_mask = right_n_bits(epoch_bits);
175 static const uintptr_t epoch_mask_in_place = epoch_mask << epoch_shift;
176
177 static const uintptr_t hash_mask = right_n_bits(hash_bits);
178 static const uintptr_t hash_mask_in_place = hash_mask << hash_shift;
179 static const uintptr_t hash_mask_compact = right_n_bits(hash_bits_compact);
180 static const uintptr_t hash_mask_compact_in_place = hash_mask_compact << hash_shift_compact;
181
182 #ifdef _LP64
183 static const uintptr_t klass_mask = right_n_bits(klass_bits);
184 static const uintptr_t klass_mask_in_place = klass_mask << klass_shift;
185 #endif
186
187 // Alignment of JavaThread pointers encoded in object header required by biased locking
188 static const size_t biased_lock_alignment = 2 << (epoch_shift + epoch_bits);
189
190 static const uintptr_t locked_value = 0;
191 static const uintptr_t unlocked_value = 1;
192 static const uintptr_t monitor_value = 2;
193 static const uintptr_t marked_value = 3;
194 static const uintptr_t biased_lock_pattern = 5;
195
196 static const uintptr_t no_hash = 0 ; // no hash value assigned
197 static const uintptr_t no_hash_in_place = (address_word)no_hash << hash_shift;
198 static const uintptr_t no_lock_in_place = unlocked_value;
199
200 static const uint max_age = age_mask;
201
202 static const int max_bias_epoch = epoch_mask;
203
204 // Creates a markWord with all bits set to zero.
205 static markWord zero() { return markWord(uintptr_t(0)); }
247 return (mask_bits(value(), lock_mask_in_place) != unlocked_value);
248 }
249 bool is_unlocked() const {
250 return (mask_bits(value(), biased_lock_mask_in_place) == unlocked_value);
251 }
252 bool is_marked() const {
253 return (mask_bits(value(), lock_mask_in_place) == marked_value);
254 }
255 bool is_neutral() const { return (mask_bits(value(), biased_lock_mask_in_place) == unlocked_value); }
256
257 // Special temporary state of the markWord while being inflated.
258 // Code that looks at mark outside a lock need to take this into account.
259 bool is_being_inflated() const { return (value() == 0); }
260
261 // Distinguished markword value - used when inflating over
262 // an existing stack-lock. 0 indicates the markword is "BUSY".
263 // Lockword mutators that use a LD...CAS idiom should always
264 // check for and avoid overwriting a 0 value installed by some
265 // other thread. (They should spin or block instead. The 0 value
266 // is transient and *should* be short-lived).
267 // Fast-locking does not use INFLATING.
268 static markWord INFLATING() { return zero(); } // inflate-in-progress
269
270 // Should this header be preserved during GC?
271 inline bool must_be_preserved(const oopDesc* obj) const;
272
273 // Should this header (including its age bits) be preserved in the
274 // case of a promotion failure during scavenge?
275 // Note that we special case this situation. We want to avoid
276 // calling BiasedLocking::preserve_marks()/restore_marks() (which
277 // decrease the number of mark words that need to be preserved
278 // during GC) during each scavenge. During scavenges in which there
279 // is no promotion failure, we actually don't need to call the above
280 // routines at all, since we don't mutate and re-initialize the
281 // marks of promoted objects using init_mark(). However, during
282 // scavenges which result in promotion failure, we do re-initialize
283 // the mark words of objects, meaning that we should have called
284 // these mark word preservation routines. Currently there's no good
285 // place in which to call them in any of the scavengers (although
286 // guarded by appropriate locks we could make one), but the
287 // observation is that promotion failures are quite rare and
288 // reducing the number of mark words preserved during them isn't a
289 // high priority.
290 inline bool must_be_preserved_for_promotion_failure(const oopDesc* obj) const;
291
292 // WARNING: The following routines are used EXCLUSIVELY by
293 // synchronization functions. They are not really gc safe.
294 // They must get updated if markWord layout get changed.
295 markWord set_unlocked() const {
296 return markWord(value() | unlocked_value);
297 }
298 bool has_locker() const {
299 assert(LockingMode == LM_LEGACY, "should only be called with legacy stack locking");
300 return (value() & lock_mask_in_place) == locked_value;
301 }
302 BasicLock* locker() const {
303 assert(has_locker(), "check");
304 return (BasicLock*) value();
305 }
306
307 bool is_fast_locked() const {
308 assert(LockingMode == LM_LIGHTWEIGHT, "should only be called with new lightweight locking");
309 return (value() & lock_mask_in_place) == locked_value;
310 }
311 markWord set_fast_locked() const {
312 return markWord(value() & ~lock_mask_in_place);
313 }
314
315 bool has_monitor() const {
316 return ((value() & monitor_value) != 0);
317 }
318 ObjectMonitor* monitor() const {
319 assert(has_monitor(), "check");
320 // Use xor instead of &~ to provide one extra tag-bit check.
321 return (ObjectMonitor*) (value() ^ monitor_value);
322 }
323 bool has_displaced_mark_helper() const {
324 intptr_t lockbits = value() & lock_mask_in_place;
325 return LockingMode == LM_LIGHTWEIGHT ? lockbits == monitor_value // monitor?
326 : (lockbits & unlocked_value) == 0; // monitor | stack-locked?
327 }
328 markWord displaced_mark_helper() const;
329 void set_displaced_mark_helper(markWord m) const;
330 markWord copy_set_hash(intptr_t hash) const {
331 if (UseCompactObjectHeaders) {
332 uintptr_t tmp = value() & (~hash_mask_compact_in_place);
333 tmp |= ((hash & hash_mask_compact) << hash_shift_compact);
334 return markWord(tmp);
335 } else {
336 uintptr_t tmp = value() & (~hash_mask_in_place);
337 tmp |= ((hash & hash_mask) << hash_shift);
338 return markWord(tmp);
339 }
340 }
341 // it is only used to be stored into BasicLock as the
342 // indicator that the lock is using heavyweight monitor
343 static markWord unused_mark() {
344 return markWord(marked_value);
345 }
346 // the following two functions create the markWord to be
347 // stored into object header, it encodes monitor info
348 static markWord encode(BasicLock* lock) {
349 return from_pointer(lock);
350 }
351 static markWord encode(ObjectMonitor* monitor) {
352 uintptr_t tmp = (uintptr_t) monitor;
353 return markWord(tmp | monitor_value);
354 }
355 static markWord encode(JavaThread* thread, uint age, int bias_epoch) {
356 uintptr_t tmp = (uintptr_t) thread;
357 assert(UseBiasedLocking && ((tmp & (epoch_mask_in_place | age_mask_in_place | biased_lock_mask_in_place)) == 0), "misaligned JavaThread pointer");
358 assert(age <= max_age, "age too large");
359 assert(bias_epoch <= max_bias_epoch, "bias epoch too large");
360 return markWord(tmp | (bias_epoch << epoch_shift) | (age << age_shift) | biased_lock_pattern);
361 }
362
363 // used to encode pointers during GC
364 markWord clear_lock_bits() { return markWord(value() & ~lock_mask_in_place); }
365
366 // age operations
367 markWord set_marked() { return markWord((value() & ~lock_mask_in_place) | marked_value); }
368 markWord set_unmarked() { return markWord((value() & ~lock_mask_in_place) | unlocked_value); }
369
370 uint age() const { return mask_bits(value() >> age_shift, age_mask); }
371 markWord set_age(uint v) const {
372 assert((v & ~age_mask) == 0, "shouldn't overflow age field");
373 return markWord((value() & ~age_mask_in_place) | ((v & age_mask) << age_shift));
374 }
375 markWord incr_age() const { return age() == max_age ? markWord(_value) : set_age(age() + 1); }
376
377 // hash operations
378 intptr_t hash() const {
379 if (UseCompactObjectHeaders) {
380 return mask_bits(value() >> hash_shift_compact, hash_mask_compact);
381 } else {
382 return mask_bits(value() >> hash_shift, hash_mask);
383 }
384 }
385
386 bool has_no_hash() const {
387 return hash() == no_hash;
388 }
389
390 #ifdef _LP64
391 inline markWord actual_mark() const;
392 inline Klass* klass() const;
393 inline Klass* klass_or_null() const;
394 inline Klass* safe_klass() const;
395 inline markWord set_klass(const Klass* klass) const;
396 inline narrowKlass narrow_klass() const;
397 inline markWord set_narrow_klass(const narrowKlass klass) const;
398 #endif
399
400 // Prototype mark for initialization
401 static markWord prototype() {
402 return markWord( no_hash_in_place | no_lock_in_place );
403 }
404
405 // Helper function for restoration of unmarked mark oops during GC
406 static inline markWord prototype_for_klass(const Klass* klass);
407
408 // Debugging
409 void print_on(outputStream* st, bool print_monitor_info = true) const;
410
411 // Prepare address of oop for placement into mark
412 inline static markWord encode_pointer_as_mark(void* p) { return from_pointer(p).set_marked(); }
413
414 // Recover address of oop from encoded form used in mark
415 inline void* decode_pointer() { if (UseBiasedLocking && has_bias_pattern()) return NULL; return (void*)clear_lock_bits().value(); }
416
417 #ifdef _LP64
418 inline bool self_forwarded() const {
419 bool self_fwd = mask_bits(value(), self_forwarded_mask_in_place) != 0;
420 assert(!self_fwd || UseAltGCForwarding, "Only set self-fwd bit when using alt GC forwarding");
421 return self_fwd;
422 }
423
424 inline markWord set_self_forwarded() const {
425 assert(UseAltGCForwarding, "Only call this with alt GC forwarding");
426 return markWord(value() | self_forwarded_mask_in_place | marked_value);
427 }
428 #endif
429 };
430
431 // Support atomic operations.
432 template<>
433 struct PrimitiveConversions::Translate<markWord> : public TrueType {
434 typedef markWord Value;
435 typedef uintptr_t Decayed;
436
437 static Decayed decay(const Value& x) { return x.value(); }
438 static Value recover(Decayed x) { return Value(x); }
439 };
440
441 #endif // SHARE_OOPS_MARKWORD_HPP
|