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