263
264 // CASPTR() uses the canonical argument order that dominates in the literature.
265 // Our internal cmpxchg_ptr() uses a bastardized ordering to accommodate Sun .il templates.
266
267 #define CASPTR(a,c,s) intptr_t(Atomic::cmpxchg_ptr ((void *)(s),(void *)(a),(void *)(c)))
268 #define UNS(x) (uintptr_t(x))
269 #define TRACE(m) { static volatile int ctr = 0 ; int x = ++ctr ; if ((x & (x-1))==0) { ::printf ("%d:%s\n", x, #m); ::fflush(stdout); }}
270
271 // Simplistic low-quality Marsaglia SHIFT-XOR RNG.
272 // Bijective except for the trailing mask operation.
273 // Useful for spin loops as the compiler can't optimize it away.
274
275 static inline jint MarsagliaXORV (jint x) {
276 if (x == 0) x = 1|os::random() ;
277 x ^= x << 6;
278 x ^= ((unsigned)x) >> 21;
279 x ^= x << 7 ;
280 return x & 0x7FFFFFFF ;
281 }
282
283 static inline jint MarsagliaXOR (jint * const a) {
284 jint x = *a ;
285 if (x == 0) x = UNS(a)|1 ;
286 x ^= x << 6;
287 x ^= ((unsigned)x) >> 21;
288 x ^= x << 7 ;
289 *a = x ;
290 return x & 0x7FFFFFFF ;
291 }
292
293 static int Stall (int its) {
294 static volatile jint rv = 1 ;
295 volatile int OnFrame = 0 ;
296 jint v = rv ^ UNS(OnFrame) ;
297 while (--its >= 0) {
298 v = MarsagliaXORV (v) ;
299 }
300 // Make this impossible for the compiler to optimize away,
301 // but (mostly) avoid W coherency sharing on MP systems.
302 if (v == 0x12345) rv = v ;
303 return v ;
304 }
305
306 int Monitor::TryLock () {
307 intptr_t v = _LockWord.FullWord ;
308 for (;;) {
309 if ((v & _LBIT) != 0) return 0 ;
310 const intptr_t u = CASPTR (&_LockWord, v, v|_LBIT) ;
311 if (v == u) return 1 ;
312 v = u ;
|
263
264 // CASPTR() uses the canonical argument order that dominates in the literature.
265 // Our internal cmpxchg_ptr() uses a bastardized ordering to accommodate Sun .il templates.
266
267 #define CASPTR(a,c,s) intptr_t(Atomic::cmpxchg_ptr ((void *)(s),(void *)(a),(void *)(c)))
268 #define UNS(x) (uintptr_t(x))
269 #define TRACE(m) { static volatile int ctr = 0 ; int x = ++ctr ; if ((x & (x-1))==0) { ::printf ("%d:%s\n", x, #m); ::fflush(stdout); }}
270
271 // Simplistic low-quality Marsaglia SHIFT-XOR RNG.
272 // Bijective except for the trailing mask operation.
273 // Useful for spin loops as the compiler can't optimize it away.
274
275 static inline jint MarsagliaXORV (jint x) {
276 if (x == 0) x = 1|os::random() ;
277 x ^= x << 6;
278 x ^= ((unsigned)x) >> 21;
279 x ^= x << 7 ;
280 return x & 0x7FFFFFFF ;
281 }
282
283 static int Stall (int its) {
284 static volatile jint rv = 1 ;
285 volatile int OnFrame = 0 ;
286 jint v = rv ^ UNS(OnFrame) ;
287 while (--its >= 0) {
288 v = MarsagliaXORV (v) ;
289 }
290 // Make this impossible for the compiler to optimize away,
291 // but (mostly) avoid W coherency sharing on MP systems.
292 if (v == 0x12345) rv = v ;
293 return v ;
294 }
295
296 int Monitor::TryLock () {
297 intptr_t v = _LockWord.FullWord ;
298 for (;;) {
299 if ((v & _LBIT) != 0) return 0 ;
300 const intptr_t u = CASPTR (&_LockWord, v, v|_LBIT) ;
301 if (v == u) return 1 ;
302 v = u ;
|