Repository navigation
[cpp] Fix field access on AtomicObject results - #13095
Conversation
load, store, exchange and compareExchange are inline and return untyped __global__ calls, which return hx::Object in C++. After inlining, the expression has the concrete type, so hxcpp doesn't convert it, and a field access directly on the result, like a.load().field, generated a member access on hx::Object that failed to compile. The cast makes hxcpp convert the result. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
@Aidan63 Don't we have much nicer ways to expressing this stuff nowadays? |
|
Yeah, |
|
That would be great, thanks! |
|
Once HaxeFoundation/hxcpp#1441 is merged you should be able to bung the following two files into this merge to get the typed version of the API, seems to work fine for me. I assume there are other areas of the haxe std which has the same issue for cpp. AtomicInt.hx package haxe.atomic;
@:include("hx/atomic/AtomicInt.hpp")
@:cpp.ManagedType({ type : "AtomicInt", namespace : [ "hx", "atomic" ], flags : [ StandardNaming ] })
private extern class AtomicIntImpl {
function new(value:Int);
function fetchAdd(value:Int):Int;
function fetchSub(value:Int):Int;
function fetchAnd(value:Int):Int;
function fetchOr(value:Int):Int;
function fetchXor(value:Int):Int;
function load():Int;
function store(value:Int):Int;
function exchange(value:Int):Int;
function compareExchange(expected:Int, replacement:Int):Int;
}
private typedef AtomicIntData = AtomicIntImpl;
abstract AtomicInt(AtomicIntData) {
public #if !scriptable inline #end function new(value:Int):Void {
this = new AtomicIntImpl(value);
}
public #if !scriptable inline #end function add(b:Int):Int {
return this.fetchAdd(b);
}
public #if !scriptable inline #end function sub(b:Int):Int {
return this.fetchSub(b);
}
public #if !scriptable inline #end function and(b:Int):Int {
return this.fetchAnd(b);
}
public #if !scriptable inline #end function or(b:Int):Int {
return this.fetchOr(b);
}
public #if !scriptable inline #end function xor(b:Int):Int {
return this.fetchXor(b);
}
public #if !scriptable inline #end function compareExchange(expected:Int, replacement:Int):Int {
return this.compareExchange(expected, replacement);
}
public #if !scriptable inline #end function exchange(value:Int):Int {
return this.exchange(value);
}
public #if !scriptable inline #end function load():Int {
return this.load();
}
public #if !scriptable inline #end function store(value:Int):Int {
return this.store(value);
}
}AtomicObject.hx package haxe.atomic;
@:include("hx/atomic/AtomicObject.hpp")
@:cpp.ManagedType({ type : "AtomicObject", namespace : [ "hx", "atomic" ], flags : [ StandardNaming ] })
private extern class AtomicObjectImpl {
function new(value:Dynamic);
function load():Dynamic;
function store(value:Dynamic):Dynamic;
function exchange(value:Dynamic):Dynamic;
function compareExchange(expected:Dynamic, replacement:Dynamic):Dynamic;
}
private typedef AtomicObjectData<T:{}> = AtomicObjectImpl;
abstract AtomicObject<T:{}>(AtomicObjectData<T>) {
public #if !scriptable inline #end function new(value:T):Void {
this = new AtomicObjectImpl(value);
}
public #if !scriptable inline #end function compareExchange(expected:T, replacement:T):T {
return this.compareExchange(expected, replacement);
}
public #if !scriptable inline #end function exchange(value:T):T {
return this.exchange(value);
}
public #if !scriptable inline #end function load():T {
return this.load();
}
public #if !scriptable inline #end function store(value:T):T {
return this.store(value);
}
} |
Instead of casting the results of the untyped __global__ calls, bind hx::atomic::AtomicObject and hx::atomic::AtomicInt (added in HaxeFoundation/hxcpp#1441) as @:cpp.ManagedType externs. The calls are typed now, so hxcpp converts the results itself and a field access on the result works. AtomicInt also uses the typed class instead of _hx_atomic_* calls on a field of a wrapper class. The cppia externs are unchanged. Co-Authored-By: Aidan Lee <10246746+Aidan63@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
That looks so much better, thanks! Claude was happy to go with your implementation, the only minor difference is this:
|
Since HaxeFoundation/haxe#13095, Haxe 5's haxe.atomic.AtomicInt and AtomicObject on C++ are hx::atomic::AtomicInt and AtomicObject from hx/atomic/AtomicInt.hpp and AtomicObject.hpp, which stock added in HaxeFoundation/hxcpp#1441. Without them a program using haxe.atomic did not compile with the current Haxe 5. Haxe 4 and 5.0.0-preview.1 keep using the atomic intrinsics, which are unchanged. They take the form stock settled on in HaxeFoundation/hxcpp#1442 and HaxeFoundation/hxcpp#1443: compareExchange returns the value it found, which equals expected only when it stored, and each operation is a GC safe point, not a GC free zone, which was 15 to 25 times slower and let a collection move an object a load had just read. Unlike stock's, each holds its std::atomic itself instead of behind an Impl it allocated and never freed, so no native block leaks per atomic. Both are header only, so no build file changes, and AtomicInt is allocated as data, since it holds no pointers. Tests: HaxeAtomics checks every AtomicInt operation and four threads adding at once; CppiaHaxeAtomics uses AtomicInt from a script through the host, interpreted and with the JIT; TestAtomicObject in the native suite checks AtomicObject's operations, strings comparing by value, and values only it holds surviving collections. C++ has AtomicObject only from the Haxe 5 with HaxeFoundation/haxe#13095, so that test runs where the native suite's other Haxe 5 tests do. openfl#416.
On C++, accessing a field directly on the result of
AtomicObject.load,store,exchangeorcompareExchangefails to compile:Cause. These methods are
inlineand return anuntyped __global__.__hxcpp_atomic_object_*call, which returns anhx::Objectin C++. After inlining, the expression has the concrete type,Chere, so hxcpp doesn't insert a conversion and generates->xon thehx::Object.Assigning the result to a variable or passing it to a function happens to work, because those paths convert it, so this went unnoticed. hxcoro ran into it when it accessed a field on a loaded value (HaxeFoundation/hxcoro#192).
Fix. As @Aidan63 suggested,
AtomicObjectandAtomicIntnow bind the typedhx::atomic::AtomicObjectandhx::atomic::AtomicIntclasses from HaxeFoundation/hxcpp#1441 as@:cpp.ManagedTypeexterns, instead of using untyped calls. With typed calls, hxcpp converts the results itself. The methods stay inline, and the cppia externs are unchanged. This requires hxcpp with #1441.Test.
TestAtomicObject.testFieldAccessOnResultaccesses a field on the result of each method. With current hxcpp master, the cpp and cppia unit tests pass locally on Windows.🤖 Generated with Claude Code