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