haxe
haxe copied to clipboard
Atomic operations.
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.
That cas_loop stuff looks wild... how can that be atomic without any synchronization?
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
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.
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.
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__).
Could you resolve the conflict?
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)
Fixed the issues with the new Int64 stuff in https://github.com/HaxeFoundation/haxe/pull/10860 and https://github.com/HaxeFoundation/hashlink/pull/574.
Thank you for your work and patience!