haxe icon indicating copy to clipboard operation
haxe copied to clipboard

Atomic operations.

Open Apprentice-Alchemist opened this issue 3 years ago • 4 comments

Even JavaScript has atomics now, no reason why Haxe shouldn't.

Supported targets:

  • Hxcpp (https://github.com/HaxeFoundation/hxcpp/pull/985)
  • Hashlink (https://github.com/HaxeFoundation/hashlink/pull/523)
  • Java/JVM
  • JavaScript

The hashlink unit tests fail in TestException when setting hl_ver to 1.12.0, because of https://github.com/HaxeFoundation/haxe/commit/98df21758045580f7370469c9ffa464e794e80f3

~~Currently haxe.Atomic<T> is only implemented for Int, but it should be possible to implement atomic operations for "all" types, at least on Hxcpp/Hashlink and probably Java and C# too.~~ haxe.atomic.AtomicObject is implemented for Hashlink and Java.

Apprentice-Alchemist avatar Mar 01 '22 12:03 Apprentice-Alchemist

That cas_loop stuff looks wild... how can that be atomic without any synchronization?

Simn avatar Mar 01 '22 14:03 Simn

That cas_loop stuff is an attempt at reproducing what Clang/GCC generate for their atomic intrinsics

        mov     eax, dword ptr [rdi]
.LBB3_1:                                # =>This Inner Loop Header: Depth=1
        mov     ecx, eax
        or      ecx, esi
        lock            cmpxchg dword ptr [rdi], ecx
        jne     .LBB3_1
        ret

Apprentice-Alchemist avatar Mar 01 '22 14:03 Apprentice-Alchemist

I read a bit about CAS loops and I think this is fine. The function application freaks me out a bit, but I suppose inlining is going to take care of everything here.

However, that loop really looks like it wants to become a do ... while loop.

Simn avatar Mar 26 '22 07:03 Simn

Alright, the loop has been changed to a do .. while loop. I also changed the Java compareExchange implementation to also use a CAS loop, otherwise the original value can't be returned without a race condition.

The only problem remaining is the TestExceptions.testExceptions test breaking when using -D hl-ver=1.12.0. (I assume this is due to https://github.com/HaxeFoundation/haxe/commit/98df21758045580f7370469c9ffa464e794e80f3)

The exact error:

unit.TestExceptions
  testExceptionStack: FAILURE .FFFF.FFFFF
    line: 255, _with_ throws: {file : /usr/local/share/haxe/std/hl/_std/haxe/Exception.hx, line : 24, method : __constructor__} is expected, but got {file : /usr/local/share/haxe/std/haxe/ValueException.hx, line : 24, method : __constructor__}
    line: 255, _with_ throws: {file : /usr/local/share/haxe/std/hl/_std/haxe/Exception.hx, line : 6, method : __constructor__} is expected, but got {file : unit/TestExceptions.hx, line : 46, method : __constructor__}
    line: 255, _with_ throws: {file : /usr/local/share/haxe/std/hl/_std/haxe/Exception.hx, line : -12, method : __constructor__} is expected, but got {file : unit/TestExceptions.hx, line : 42, method : __constructor__}
    line: 255, _with_ throws: {file : /usr/local/share/haxe/std/hl/_std/haxe/Exception.hx, line : -30, method : __constructor__} is expected, but got {file : /usr/local/share/haxe/std/hl/_std/haxe/Exception.hx, line : 29, method : thrown}
    line: 255, _without_ throws: {file : /usr/local/share/haxe/std/haxe/CallStack.hx, line : 42, method : callStack} is expected, but got {file : /usr/local/share/haxe/std/hl/_std/haxe/Exception.hx, line : 42, method : __constructor__}
    line: 255, _without_ throws: {file : /usr/local/share/haxe/std/haxe/CallStack.hx, line : 32, method : callStack} is expected, but got {file : /usr/local/share/haxe/std/haxe/ValueException.hx, line : 24, method : __constructor__}
    line: 255, _without_ throws: {file : /usr/local/share/haxe/std/haxe/CallStack.hx, line : 22, method : callStack} is expected, but got {file : unit/TestExceptions.hx, line : 46, method : __constructor__}
    line: 255, _without_ throws: {file : /usr/local/share/haxe/std/haxe/CallStack.hx, line : 12, method : callStack} is expected, but got {file : unit/TestExceptions.hx, line : 42, method : __constructor__}
    line: 255, _without_ throws: {file : /usr/local/share/haxe/std/haxe/CallStack.hx, line : 2, method : callStack} is expected, but got {file : /usr/local/share/haxe/std/hl/_std/haxe/Exception.hx, line : 29, method : thrown}

EDIT: I think I've fixed it? It seems to behave like other targets again, and the test passes.

Apprentice-Alchemist avatar Mar 26 '22 08:03 Apprentice-Alchemist

I've added an AtomicBool type (this is just a wrapper around AtomicInt, except on Java). I've also implemented atomics for C# again (this time simply using __cs__).

Apprentice-Alchemist avatar Oct 16 '22 20:10 Apprentice-Alchemist

Could you resolve the conflict?

Simn avatar Nov 21 '22 10:11 Simn

Resolved the conflict, everything should be working now.

Hashlink test failure is due to the new native Int64 impl. (Which was not tested previously due to hl-ver being set to 1.11.0)

Apprentice-Alchemist avatar Nov 21 '22 11:11 Apprentice-Alchemist

Fixed the issues with the new Int64 stuff in https://github.com/HaxeFoundation/haxe/pull/10860 and https://github.com/HaxeFoundation/hashlink/pull/574.

Apprentice-Alchemist avatar Nov 21 '22 12:11 Apprentice-Alchemist

Thank you for your work and patience!

Simn avatar Nov 24 '22 09:11 Simn