43 // --------
44 // unused:25 hash:31 -->| unused_gap:1 age:4 unused_gap:1 lock:2 (normal object)
45 //
46 // - hash contains the identity hash value: largest value is
47 // 31 bits, see os::random(). Also, 64-bit vm's require
48 // a hash value no bigger than 32 bits because they will not
49 // properly generate a mask larger than that: see library_call.cpp
50 //
51 // - the two lock bits are used to describe three states: locked/unlocked and monitor.
52 //
53 // [ptr | 00] locked ptr points to real header on stack (stack-locking in use)
54 // [header | 00] locked locked regular object header (fast-locking in use)
55 // [header | 01] unlocked regular object header
56 // [ptr | 10] monitor inflated lock (header is swapped out, UseObjectMonitorTable == false)
57 // [header | 10] monitor inflated lock (UseObjectMonitorTable == true)
58 // [ptr | 11] marked used to mark an object
59 // [0 ............ 0| 00] inflating inflation in progress (stack-locking in use)
60 //
61 // We assume that stack/thread pointers have the lowest two bits cleared.
62 //
63 // - INFLATING() is a distinguished markword value of all zeros that is
64 // used when inflating an existing stack-lock into an ObjectMonitor.
65 // See below for is_being_inflated() and INFLATING().
66
67 class BasicLock;
68 class ObjectMonitor;
69 class JavaThread;
70 class outputStream;
71
72 class markWord {
73 private:
74 uintptr_t _value;
75
76 public:
77 explicit markWord(uintptr_t value) : _value(value) {}
78
79 markWord() = default; // Doesn't initialize _value.
80
81 // It is critical for performance that this class be trivially
82 // destructable, copyable, and assignable.
83 ~markWord() = default;
84 markWord(const markWord&) = default;
85 markWord& operator=(const markWord&) = default;
86
87 static markWord from_pointer(void* ptr) {
88 return markWord((uintptr_t)ptr);
89 }
90 void* to_pointer() const {
91 return (void*)_value;
92 }
93
94 bool operator==(const markWord& other) const {
95 return _value == other._value;
96 }
97 bool operator!=(const markWord& other) const {
98 return !operator==(other);
99 }
100
101 // Conversion
102 uintptr_t value() const { return _value; }
103
104 // Constants
105 static const int age_bits = 4;
106 static const int lock_bits = 2;
107 static const int first_unused_gap_bits = 1;
108 static const int max_hash_bits = BitsPerWord - age_bits - lock_bits - first_unused_gap_bits;
109 static const int hash_bits = max_hash_bits > 31 ? 31 : max_hash_bits;
110 static const int second_unused_gap_bits = LP64_ONLY(1) NOT_LP64(0);
111
112 static const int lock_shift = 0;
113 static const int age_shift = lock_bits + first_unused_gap_bits;
114 static const int hash_shift = age_shift + age_bits + second_unused_gap_bits;
115
116 static const uintptr_t lock_mask = right_n_bits(lock_bits);
117 static const uintptr_t lock_mask_in_place = lock_mask << lock_shift;
118 static const uintptr_t age_mask = right_n_bits(age_bits);
119 static const uintptr_t age_mask_in_place = age_mask << age_shift;
120 static const uintptr_t hash_mask = right_n_bits(hash_bits);
121 static const uintptr_t hash_mask_in_place = hash_mask << hash_shift;
122
123 static const uintptr_t locked_value = 0;
124 static const uintptr_t unlocked_value = 1;
125 static const uintptr_t monitor_value = 2;
126 static const uintptr_t marked_value = 3;
127
128 static const uintptr_t no_hash = 0 ; // no hash value assigned
129 static const uintptr_t no_hash_in_place = (uintptr_t)no_hash << hash_shift;
130 static const uintptr_t no_lock_in_place = unlocked_value;
131
132 static const uint max_age = age_mask;
133
134 // Creates a markWord with all bits set to zero.
135 static markWord zero() { return markWord(uintptr_t(0)); }
136
137 // lock accessors (note that these assume lock_shift == 0)
138 bool is_locked() const {
139 return (mask_bits(value(), lock_mask_in_place) != unlocked_value);
140 }
141 bool is_unlocked() const {
142 return (mask_bits(value(), lock_mask_in_place) == unlocked_value);
143 }
144 bool is_marked() const {
145 return (mask_bits(value(), lock_mask_in_place) == marked_value);
146 }
147 bool is_forwarded() const {
148 return (mask_bits(value(), lock_mask_in_place) == marked_value);
149 }
150 bool is_neutral() const { // Not locked, or marked - a "clean" neutral state
151 return (mask_bits(value(), lock_mask_in_place) == unlocked_value);
152 }
153
154 // Special temporary state of the markWord while being inflated.
155 // Code that looks at mark outside a lock need to take this into account.
156 bool is_being_inflated() const { return (value() == 0); }
157
158 // Distinguished markword value - used when inflating over
159 // an existing stack-lock. 0 indicates the markword is "BUSY".
160 // Lockword mutators that use a LD...CAS idiom should always
161 // check for and avoid overwriting a 0 value installed by some
162 // other thread. (They should spin or block instead. The 0 value
163 // is transient and *should* be short-lived).
164 // Fast-locking does not use INFLATING.
165 static markWord INFLATING() { return zero(); } // inflate-in-progress
166
167 // Should this header be preserved during GC?
168 bool must_be_preserved(const oopDesc* obj) const {
169 return (!is_unlocked() || !has_no_hash());
170 }
171
172 // WARNING: The following routines are used EXCLUSIVELY by
173 // synchronization functions. They are not really gc safe.
174 // They must get updated if markWord layout get changed.
175 markWord set_unlocked() const {
176 return markWord(value() | unlocked_value);
177 }
178 bool has_locker() const {
179 assert(LockingMode == LM_LEGACY, "should only be called with legacy stack locking");
180 return (value() & lock_mask_in_place) == locked_value;
181 }
182 BasicLock* locker() const {
183 assert(has_locker(), "check");
184 return (BasicLock*) value();
185 }
186
187 bool is_fast_locked() const {
188 assert(LockingMode == LM_LIGHTWEIGHT, "should only be called with new lightweight locking");
189 return (value() & lock_mask_in_place) == locked_value;
243 // age operations
244 markWord set_marked() { return markWord((value() & ~lock_mask_in_place) | marked_value); }
245 markWord set_unmarked() { return markWord((value() & ~lock_mask_in_place) | unlocked_value); }
246
247 uint age() const { return (uint) mask_bits(value() >> age_shift, age_mask); }
248 markWord set_age(uint v) const {
249 assert((v & ~age_mask) == 0, "shouldn't overflow age field");
250 return markWord((value() & ~age_mask_in_place) | ((v & age_mask) << age_shift));
251 }
252 markWord incr_age() const { return age() == max_age ? markWord(_value) : set_age(age() + 1); }
253
254 // hash operations
255 intptr_t hash() const {
256 return mask_bits(value() >> hash_shift, hash_mask);
257 }
258
259 bool has_no_hash() const {
260 return hash() == no_hash;
261 }
262
263 // Prototype mark for initialization
264 static markWord prototype() {
265 return markWord( no_hash_in_place | no_lock_in_place );
266 }
267
268 // Debugging
269 void print_on(outputStream* st, bool print_monitor_info = true) const;
270
271 // Prepare address of oop for placement into mark
272 inline static markWord encode_pointer_as_mark(void* p) { return from_pointer(p).set_marked(); }
273
274 // Recover address of oop from encoded form used in mark
275 inline void* decode_pointer() const { return (void*)clear_lock_bits().value(); }
276
277 inline oop forwardee() const {
278 return cast_to_oop(decode_pointer());
279 }
280 };
281
282 // Support atomic operations.
283 template<>
284 struct PrimitiveConversions::Translate<markWord> : public std::true_type {
285 typedef markWord Value;
286 typedef uintptr_t Decayed;
287
288 static Decayed decay(const Value& x) { return x.value(); }
289 static Value recover(Decayed x) { return Value(x); }
290 };
291
292 #endif // SHARE_OOPS_MARKWORD_HPP
|
43 // --------
44 // unused:25 hash:31 -->| unused_gap:1 age:4 unused_gap:1 lock:2 (normal object)
45 //
46 // - hash contains the identity hash value: largest value is
47 // 31 bits, see os::random(). Also, 64-bit vm's require
48 // a hash value no bigger than 32 bits because they will not
49 // properly generate a mask larger than that: see library_call.cpp
50 //
51 // - the two lock bits are used to describe three states: locked/unlocked and monitor.
52 //
53 // [ptr | 00] locked ptr points to real header on stack (stack-locking in use)
54 // [header | 00] locked locked regular object header (fast-locking in use)
55 // [header | 01] unlocked regular object header
56 // [ptr | 10] monitor inflated lock (header is swapped out, UseObjectMonitorTable == false)
57 // [header | 10] monitor inflated lock (UseObjectMonitorTable == true)
58 // [ptr | 11] marked used to mark an object
59 // [0 ............ 0| 00] inflating inflation in progress (stack-locking in use)
60 //
61 // We assume that stack/thread pointers have the lowest two bits cleared.
62 //
63 //
64 // - INFLATING() is a distinguished markword value of all zeros that is
65 // used when inflating an existing stack-lock into an ObjectMonitor.
66 // See below for is_being_inflated() and INFLATING().
67 //
68 //
69 //
70 // Valhalla
71 //
72 // <CMH: merge this doc into the text above>
73 //
74 // Project Valhalla has mark word encoding requirements for the following oops:
75 //
76 // * inline types: have alternative bytecode behavior, e.g. can not be locked
77 // - "larval state": mutable state, but only during object init, observable
78 // by only by a single thread (generally do not mutate markWord)
79 //
80 // * flat arrays: load/decode of klass layout helper is expensive for aaload
81 //
82 // * "null free" arrays: load/decode of klass layout helper again for aaload
83 //
84 // EnableValhalla
85 //
86 // Formerly known as "biased lock bit", "unused_gap" is free to use: using this
87 // bit to indicate inline type, combined with "unlocked" lock bits, means we
88 // will not interfere with lock encodings (displaced, inflating, and monitor),
89 // since inline types can't be locked.
90 //
91 // Further state encoding
92 //
93 // 32 bit plaforms currently have no further room for encoding. No room for
94 // "denormalized layout helper bits", these fast mark word tests can only be made on
95 // 64 bit platforms. 32-bit platforms need to load the klass->_layout_helper. This
96 // said, the larval state bit is still required for operation, stealing from the hash
97 // code is simplest mechanism.
98 //
99 // Valhalla specific encodings
100 //
101 // Revised Bit-format of an object header (most significant first, big endian layout below):
102 //
103 // 32 bits:
104 // --------
105 // hash:24 ------------>| larval:1 age:4 inline_type:1 lock:2
106 //
107 // 64 bits:
108 // --------
109 // unused:1 | <-- hash:31 -->| unused:22 larval:1 age:4 flat_array:1 null_free_array:1 inline_type:1 lock:2
110 //
111 // The "fast" static type bits (flat_array, null_free_array, and inline_type)
112 // are placed lowest next to lock bits to more easily decode forwarding pointers.
113 // G1 for example, implicitly clears age bits ("G1FullGCCompactionPoint::forward()")
114 // using "oopDesc->forwardee()", so it necessary for "markWord::decode_pointer()"
115 // to return a non-nullptr for this case, but not confuse the static type bits for
116 // a pointer.
117 //
118 // Static types bits are recorded in the "klass->prototype_header()", displaced
119 // mark should simply use the prototype header as "slow path", rather chasing
120 // monitor or stack lock races.
121 //
122 // Lock patterns (note inline types can't be locked/monitor/inflating)...
123 //
124 // [ptr | 000] locked ptr points to real header on stack
125 // [header | ?01] unlocked regular object header
126 // [ptr | 010] monitor inflated lock (header is wapped out)
127 // [ptr | ?11] marked used to mark an object
128 // [0 ............ | 000] inflating inflation in progress
129 //
130 //
131
132 class BasicLock;
133 class ObjectMonitor;
134 class JavaThread;
135 class outputStream;
136
137 class markWord {
138 private:
139 uintptr_t _value;
140
141 public:
142 explicit markWord(uintptr_t value) : _value(value) {}
143
144 markWord() = default; // Doesn't initialize _value.
145
146 // It is critical for performance that this class be trivially
147 // destructable, copyable, and assignable.
148 ~markWord() = default;
149 markWord(const markWord&) = default;
150 markWord& operator=(const markWord&) = default;
151
152 static markWord from_pointer(void* ptr) {
153 return markWord((uintptr_t)ptr);
154 }
155 void* to_pointer() const {
156 return (void*)_value;
157 }
158
159 bool operator==(const markWord& other) const {
160 return _value == other._value;
161 }
162 bool operator!=(const markWord& other) const {
163 return !operator==(other);
164 }
165
166 // Conversion
167 uintptr_t value() const { return _value; }
168
169 // Constants, in least significant bit order
170 static const int lock_bits = 2;
171 static const int first_unused_gap_bits = 1; // When !EnableValhalla
172 // EnableValhalla: static prototype header bits (fast path instead of klass layout_helper)
173 static const int inline_type_bits = 1;
174 static const int null_free_array_bits = LP64_ONLY(1) NOT_LP64(0);
175 static const int flat_array_bits = LP64_ONLY(1) NOT_LP64(0);
176 // instance state
177 static const int age_bits = 4;
178 static const int larval_bits = 1;
179 static const int max_hash_bits = BitsPerWord - age_bits - lock_bits - inline_type_bits - larval_bits - flat_array_bits - null_free_array_bits;
180 static const int hash_bits = max_hash_bits > 31 ? 31 : max_hash_bits;
181 static const int second_unused_gap_bits = LP64_ONLY(1) NOT_LP64(0); // !EnableValhalla: unused
182
183 static const int lock_shift = 0;
184 static const int inline_type_shift = lock_bits;
185 static const int null_free_array_shift = inline_type_shift + inline_type_bits;
186 static const int flat_array_shift = null_free_array_shift + null_free_array_bits;
187 static const int age_shift = flat_array_shift + flat_array_bits;
188 static const int unused_gap_shift = age_shift + age_bits; // !EnableValhalla: unused
189 static const int larval_shift = age_shift + age_bits;
190 static const int hash_shift = LP64_ONLY(32) NOT_LP64(larval_shift + larval_bits);
191
192 static const uintptr_t lock_mask = right_n_bits(lock_bits);
193 static const uintptr_t lock_mask_in_place = lock_mask << lock_shift;
194 static const uintptr_t inline_type_mask = right_n_bits(lock_bits + inline_type_bits);
195 static const uintptr_t inline_type_mask_in_place = inline_type_mask << lock_shift;
196 static const uintptr_t inline_type_bit_in_place = 1 << inline_type_shift;
197 static const uintptr_t null_free_array_mask = right_n_bits(null_free_array_bits);
198 static const uintptr_t null_free_array_mask_in_place = (null_free_array_mask << null_free_array_shift) | lock_mask_in_place;
199 static const uintptr_t null_free_array_bit_in_place = (1 << null_free_array_shift);
200 static const uintptr_t flat_array_mask = right_n_bits(flat_array_bits);
201 static const uintptr_t flat_array_mask_in_place = (flat_array_mask << flat_array_shift) | null_free_array_mask_in_place | lock_mask_in_place;
202 static const uintptr_t flat_array_bit_in_place = (1 << flat_array_shift);
203
204 static const uintptr_t age_mask = right_n_bits(age_bits);
205 static const uintptr_t age_mask_in_place = age_mask << age_shift;
206
207 static const uintptr_t larval_mask = right_n_bits(larval_bits);
208 static const uintptr_t larval_mask_in_place = (larval_mask << larval_shift) | inline_type_mask_in_place;
209 static const uintptr_t larval_bit_in_place = (1 << larval_shift);
210
211 static const uintptr_t hash_mask = right_n_bits(hash_bits);
212 static const uintptr_t hash_mask_in_place = hash_mask << hash_shift;
213
214 static const uintptr_t locked_value = 0;
215 static const uintptr_t unlocked_value = 1;
216 static const uintptr_t monitor_value = 2;
217 static const uintptr_t marked_value = 3;
218
219 static const uintptr_t inline_type_pattern = inline_type_bit_in_place | unlocked_value;
220 static const uintptr_t null_free_array_pattern = null_free_array_bit_in_place | unlocked_value;
221 static const uintptr_t flat_array_pattern = flat_array_bit_in_place | null_free_array_pattern;
222 // Has static klass prototype, used for decode/encode pointer
223 static const uintptr_t static_prototype_mask = LP64_ONLY(right_n_bits(inline_type_bits + flat_array_bits + null_free_array_bits)) NOT_LP64(right_n_bits(inline_type_bits));
224 static const uintptr_t static_prototype_mask_in_place = static_prototype_mask << lock_bits;
225 static const uintptr_t static_prototype_value_max = (1 << age_shift) - 1;
226
227 static const uintptr_t larval_pattern = larval_bit_in_place | inline_type_pattern;
228
229 static const uintptr_t no_hash = 0 ; // no hash value assigned
230 static const uintptr_t no_hash_in_place = (uintptr_t)no_hash << hash_shift;
231 static const uintptr_t no_lock_in_place = unlocked_value;
232
233 static const uint max_age = age_mask;
234
235 // Creates a markWord with all bits set to zero.
236 static markWord zero() { return markWord(uintptr_t(0)); }
237
238 bool is_inline_type() const {
239 return (mask_bits(value(), inline_type_mask_in_place) == inline_type_pattern);
240 }
241
242 // lock accessors (note that these assume lock_shift == 0)
243 bool is_locked() const {
244 return (mask_bits(value(), lock_mask_in_place) != unlocked_value);
245 }
246 bool is_unlocked() const {
247 return (mask_bits(value(), lock_mask_in_place) == unlocked_value);
248 }
249 bool is_marked() const {
250 return (mask_bits(value(), lock_mask_in_place) == marked_value);
251 }
252
253 // is unlocked and not an inline type (which cannot be involved in locking, displacement or inflation)
254 // i.e. test both lock bits and the inline type bit together
255 bool is_neutral() const { // Not locked, or marked - a "clean" neutral state
256 return (mask_bits(value(), inline_type_mask_in_place) == unlocked_value);
257 }
258
259 bool is_forwarded() const {
260 return (mask_bits(value(), lock_mask_in_place) == marked_value);
261 }
262
263 // Special temporary state of the markWord while being inflated.
264 // Code that looks at mark outside a lock need to take this into account.
265 bool is_being_inflated() const { return (value() == 0); }
266
267 // Distinguished markword value - used when inflating over
268 // an existing stack-lock. 0 indicates the markword is "BUSY".
269 // Lockword mutators that use a LD...CAS idiom should always
270 // check for and avoid overwriting a 0 value installed by some
271 // other thread. (They should spin or block instead. The 0 value
272 // is transient and *should* be short-lived).
273 // Fast-locking does not use INFLATING.
274 static markWord INFLATING() { return zero(); } // inflate-in-progress
275
276 // Should this header be preserved during GC?
277 bool must_be_preserved(const oopDesc* obj) const {
278 return (!is_unlocked() || !has_no_hash() || (EnableValhalla && is_larval_state()));
279 }
280
281 // WARNING: The following routines are used EXCLUSIVELY by
282 // synchronization functions. They are not really gc safe.
283 // They must get updated if markWord layout get changed.
284 markWord set_unlocked() const {
285 return markWord(value() | unlocked_value);
286 }
287 bool has_locker() const {
288 assert(LockingMode == LM_LEGACY, "should only be called with legacy stack locking");
289 return (value() & lock_mask_in_place) == locked_value;
290 }
291 BasicLock* locker() const {
292 assert(has_locker(), "check");
293 return (BasicLock*) value();
294 }
295
296 bool is_fast_locked() const {
297 assert(LockingMode == LM_LIGHTWEIGHT, "should only be called with new lightweight locking");
298 return (value() & lock_mask_in_place) == locked_value;
352 // age operations
353 markWord set_marked() { return markWord((value() & ~lock_mask_in_place) | marked_value); }
354 markWord set_unmarked() { return markWord((value() & ~lock_mask_in_place) | unlocked_value); }
355
356 uint age() const { return (uint) mask_bits(value() >> age_shift, age_mask); }
357 markWord set_age(uint v) const {
358 assert((v & ~age_mask) == 0, "shouldn't overflow age field");
359 return markWord((value() & ~age_mask_in_place) | ((v & age_mask) << age_shift));
360 }
361 markWord incr_age() const { return age() == max_age ? markWord(_value) : set_age(age() + 1); }
362
363 // hash operations
364 intptr_t hash() const {
365 return mask_bits(value() >> hash_shift, hash_mask);
366 }
367
368 bool has_no_hash() const {
369 return hash() == no_hash;
370 }
371
372 // private buffered value operations
373 markWord enter_larval_state() const {
374 return markWord(value() | larval_bit_in_place);
375 }
376 markWord exit_larval_state() const {
377 return markWord(value() & ~larval_bit_in_place);
378 }
379 bool is_larval_state() const {
380 return (mask_bits(value(), larval_mask_in_place) == larval_pattern);
381 }
382
383 #ifdef _LP64 // 64 bit encodings only
384 bool is_flat_array() const {
385 return (mask_bits(value(), flat_array_mask_in_place) == flat_array_pattern);
386 }
387
388 bool is_null_free_array() const {
389 return (mask_bits(value(), null_free_array_mask_in_place) == null_free_array_pattern);
390 }
391 #else
392 bool is_flat_array() const {
393 fatal("Should not ask this for mark word, ask oopDesc");
394 return false;
395 }
396
397 bool is_null_free_array() const {
398 fatal("Should not ask this for mark word, ask oopDesc");
399 return false;
400 }
401 #endif
402 // Prototype mark for initialization
403 static markWord prototype() {
404 return markWord( no_hash_in_place | no_lock_in_place );
405 }
406
407 static markWord inline_type_prototype() {
408 return markWord(inline_type_pattern);
409 }
410
411 #ifdef _LP64 // 64 bit encodings only
412 static markWord flat_array_prototype() {
413 return markWord(flat_array_pattern);
414 }
415
416 static markWord null_free_array_prototype() {
417 return markWord(null_free_array_pattern);
418 }
419 #endif
420
421 // Debugging
422 void print_on(outputStream* st, bool print_monitor_info = true) const;
423
424 // Prepare address of oop for placement into mark
425 inline static markWord encode_pointer_as_mark(void* p) { return from_pointer(p).set_marked(); }
426
427 // Recover address of oop from encoded form used in mark
428 inline void* decode_pointer() const {
429 return (EnableValhalla && _value < static_prototype_value_max) ? nullptr :
430 (void*) (clear_lock_bits().value());
431 }
432
433 inline oop forwardee() const {
434 return cast_to_oop(decode_pointer());
435 }
436 };
437
438 // Support atomic operations.
439 template<>
440 struct PrimitiveConversions::Translate<markWord> : public std::true_type {
441 typedef markWord Value;
442 typedef uintptr_t Decayed;
443
444 static Decayed decay(const Value& x) { return x.value(); }
445 static Value recover(Decayed x) { return Value(x); }
446 };
447
448 #endif // SHARE_OOPS_MARKWORD_HPP
|