[concurrency-interest] JLS 17.7 Non-atomic treatment of double and long : Android

David Dice david.dice at gmail.com
Tue Apr 30 13:51:40 EDT 2013


Regarding 64-bit volatile long accesses for IA32 mode, long ago hotspot
used the x87 FPU.   We'd load values atomically via floating point load
instructions and then deposit the value into a temp on the stack, and then
re-load into integer registers.   We've since switched to XMM which is
somewhat cleaner.

At the time, there were no formal guarantees that the floating point
accesses would actually be atomic.  I discussed this with various vendors,
and all noted that the references would be atomic in their implementations
as long as natural alignment was provided.

-Dave
https://blogs.oracle.com/dave/




Message: 1
> Date: Tue, 30 Apr 2013 20:31:29 +0300
> From: Stanimir Simeonoff <stanimir at riflexo.com>
> To: Vitaly Davidovich <vitalyd at gmail.com>
> Cc: concurrency-interest at cs.oswego.edu
> Subject: Re: [concurrency-interest] JLS 17.7 Non-atomic treatment of
>         double and long : Android
> Message-ID:
>         <
> CAEJX8oqZA6d1u_4QxLaagr_kZTGzxStseFJqrYntoo+w-hTy7g at mail.gmail.com>
> Content-Type: text/plain; charset="iso-8859-1"
>
> Here is some proof.
>
> Stanimir
> -----------
> package t1;
>
> public class TearLong {
>     private volatile long x;
>     long test(){
>         for (int i=0;i<20000;i++){
>             long n=x;
>             n+=System.currentTimeMillis()&0xff;
>             x=n;
>         }
>         return x;
>     }
>     public static void main(String[] args) {
>         System.out.println(new TearLong().test());
>         System.out.println(new TearLong().test());
>     }
> }
>
> Decoding compiled method 0x00938a08:
> Code:
> [Disassembling for mach='i386']
> [Entry Point]
> [Verified Entry Point]
> [Constants]
>   # {method} 'test' '()J' in 't1/TearLong'
>   0x00938b00: int3
>   0x00938b01: xchg   %ax,%ax
>   0x00938b04: mov    %eax,0xffffd000(%esp)
>   0x00938b0b: push   %ebp
>   0x00938b0c: sub    $0x18,%esp
>   0x00938b12: mov    0x8(%ecx),%ebx
>   0x00938b15: mov    0xc(%ecx),%esi
>   0x00938b18: mov    %ecx,(%esp)
>   0x00938b1b: call   0x6dbeed90         ;   {runtime_call}
>   0x00938b20: mov    0x4(%esi),%ebp     ; implicit exception: dispatches to
> 0x00938bdd
>   0x00938b23: cmp    $0x3b6bd38,%ebp    ;   {oop('t1/TearLong')}
>   0x00938b29: jne    0x00938bcb         ;*aload_0
>                                         ; - t1.TearLong::test at 5 (line 7)
>   0x00938b2f: inc    %ebx               ;*iinc
>                                         ; - t1.TearLong::test at 25 (line 6)
>   0x00938b30: movsd  0x8(%esi),%xmm0
>   0x00938b35: movd   %xmm0,%ebp
>   0x00938b39: psrlq  $0x20,%xmm0
>   0x00938b3e: movd   %xmm0,%edi         ;*getfield x
>                                         ; - t1.TearLong::test at 6 (line 7)
>   0x00938b42: call   0x6dce22f0         ;   {runtime_call}
>   0x00938b47: and    $0xff,%eax
>   0x00938b4d: and    $0x0,%edx
>   0x00938b50: add    %ebp,%eax
>   0x00938b52: adc    %edi,%edx
>   0x00938b54: cmp    0x8(%esi),%eax
>   0x00938b57: movd   %eax,%xmm1
>   0x00938b5b: movd   %edx,%xmm0
>   0x00938b5f: punpckldq %xmm0,%xmm1
>   0x00938b63: movsd  %xmm1,0x8(%esi)
>   0x00938b68: lock addl $0x0,(%esp)     ;*putfield x
>                                         ; - t1.TearLong::test at 22 (line 9)
>   0x00938b6d: jmp    0x00938b9c
>   0x00938b6f: nop                       ;*getfield x
>                                         ; - t1.TearLong::test at 6 (line 7)
>   0x00938b70: call   0x6dce22f0         ;*putfield x
>                                         ; - t1.TearLong::test at 22 (line 9)
>                                         ;   {runtime_call}
>   0x00938b75: inc    %ebx               ;*iinc
>                                         ; - t1.TearLong::test at 25 (line 6)
>   0x00938b76: and    $0xff,%eax
>   0x00938b7c: and    $0x0,%edx
>   0x00938b7f: add    %ebp,%eax
>   0x00938b81: adc    %edi,%edx
>   0x00938b83: cmp    0x8(%esi),%eax
>   0x00938b86: movd   %eax,%xmm1
>   0x00938b8a: movd   %edx,%xmm0
>   0x00938b8e: punpckldq %xmm0,%xmm1
>   0x00938b92: movsd  %xmm1,0x8(%esi)
>   0x00938b97: lock addl $0x0,(%esp)     ; OopMap{esi=Oop off=156}
>                                         ;*if_icmplt
>                                         ; - t1.TearLong::test at 32 (line 6)
>   0x00938b9c: test   %edi,0x8c0000      ;*if_icmplt
>                                         ; - t1.TearLong::test at 32 (line 6)
>                                         ;   {poll}
>   0x00938ba2: movsd  0x8(%esi),%xmm0
>   0x00938ba7: movd   %xmm0,%ebp
>   0x00938bab: psrlq  $0x20,%xmm0
>   0x00938bb0: movd   %xmm0,%edi
>   0x00938bb4: cmp    $0x4e20,%ebx
>   0x00938bba: jl     0x00938b70         ;*getfield x
>                                         ; - t1.TearLong::test at 36 (line 11)
>   0x00938bbc: mov    %ebp,%eax
>   0x00938bbe: mov    %edi,%edx
>   0x00938bc0: add    $0x18,%esp
>   0x00938bc3: pop    %ebp
>   0x00938bc4: test   %eax,0x8c0000      ;   {poll_return}
>   0x00938bca: ret
>   0x00938bcb: mov    $0xffffffad,%ecx
>   0x00938bd0: mov    %esi,%ebp
>   0x00938bd2: mov    %ebx,0x4(%esp)
>   0x00938bd6: nop
>   0x00938bd7: call   0x0091c700         ; OopMap{ebp=Oop off=220}
>                                         ;*aload_0
>                                         ; - t1.TearLong::test at 5 (line 7)
>                                         ;   {runtime_call}
>   0x00938bdc: int3                      ;*getfield x
>                                         ; - t1.TearLong::test at 6 (line 7)
>   0x00938bdd: mov    $0xfffffff6,%ecx
>   0x00938be2: nop
>   0x00938be3: call   0x0091c700         ; OopMap{off=232}
>                                         ;*getfield x
>                                         ; - t1.TearLong::test at 6 (line 7)
>                                         ;   {runtime_call}
>   0x00938be8: int3                      ;*getfield x
>                                         ; - t1.TearLong::test at 6 (line 7)
> ....
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20130430/8cb0175a/attachment.html>


More information about the Concurrency-interest mailing list