synchronization on non-escaping objects
Vijay Kandy
vkandy at gmail.com
Thu Nov 19 11:54:13 PST 2009
Hello,
I am trying to understand synchronization on non-escaping objects. The
JIT'd code isn't very clear to me so I am looking for some help here.
When getX() in Test class gets compiled, Hotspot (fastdebug jdk6
update 18) generates the following code:
public class Test {
private final char X = 'x';
public char getX() {
synchronized (this) {
return X;
}
}
}
getX() method when +XX:+DoEscapeAnalysis is used:
00c B1: # B6 B2 <- BLOCK HEAD IS JUNK Freq: 1
00c # stack bang
PUSHL EBP
SUB ESP,24 # Create frame
01a MOV EBP,ECX
01c MOV EAX,[ECX] # int
01e MOV EBX,EAX
020 AND EBX,#7
023 CMP EBX,#5
026 Jne,s B6 P=0.000001 C=-1.000000
026
028 B2: # B10 B3 <- B1 Freq: 0.999999
028 MOV ECX,FS:[0x00]
02f MOV ESI,[ECX + TLS::thread_ptr_offset()]
035 MOV EBX,precise klass Test: 0x0406c610:Constant:exact *
03a MOV ECX,[EBX + #104] # int
03d MOV EDI,ECX
03f OR EDI,ESI
041 MOV EBX,EDI
043 XOR EBX,EAX
045 TEST EBX,#-121
04b Jne B10 P=0.000001 C=-1.000000
04b
051 B3: # B8 B4 <- B7 B6 B2 B14 Freq: 1
051 MEMBAR-acquire (prior CMPXCHG in FastLock so empty encoding)
051 MEMBAR-release ! (empty encoding)
051 MOV ECX,#7
056 AND ECX,[EBP]
059 CMP ECX,#5
05c Jne,s B8 P=0.000001 C=-1.000000
05c
05e B4: # N152 <- B9 B8 B3 Freq: 1
05e MOV EAX,#120
063 ADD ESP,24 # Destroy frame
POPL EBP
TEST PollPage,EAX ! Poll Safepoint
06d RET
The label B6 does a FASTLOCK and B8 does a FASTUNLOCK. As there are no
instructions between MEMBAR-acquire and MEMBAR-release, does that mean
synchronization is elided? What purpose do they serve in this
scenario?
However, when I re-write the method as shown below:
public char getX() {
synchronized (new Object()) {
return X; // final field
}
}
the method is compiled as follows:
00c B1: # N14 <- BLOCK HEAD IS JUNK Freq: 1
00c PUSHL EBP
SUB ESP,8 # Create frame
013 MOV EAX,#120
018 ADD ESP,8 # Destroy frame
POPL EBP
TEST PollPage,EAX ! Poll Safepoint
022 RET
Also, I'd deeply appreciate the kindness if someone can compare the 2
method bodies and explain why they are compiled differently.
Regards,
Vijay
More information about the hotspot-dev
mailing list