Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 27 additions & 18 deletions std/cpp/_std/haxe/atomic/AtomicInt.hx
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
package haxe.atomic;

private final class Data {
public var value:Int;
public function new(value:Int) {
this.value = value;
}
@: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 = Data;
private typedef AtomicIntData = AtomicIntImpl;

#if cppia
extern
Expand All @@ -34,44 +43,44 @@ abstract AtomicInt(AtomicIntData) {

public function store(value:Int):Int;
#else
public #if !scriptable inline #end function new(value:Int) {
this = new Data(value);
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 untyped __cpp__("_hx_atomic_add(&{0}, {1})", this.value, b);
return this.fetchAdd(b);
}

public #if !scriptable inline #end function sub(b:Int):Int {
return untyped __cpp__("_hx_atomic_sub(&{0}, {1})", this.value, b);
return this.fetchSub(b);
}

public #if !scriptable inline #end function and(b:Int):Int {
return untyped __cpp__("_hx_atomic_and(&{0}, {1})", this.value, b);
return this.fetchAnd(b);
}

public #if !scriptable inline #end function or(b:Int):Int {
return untyped __cpp__("_hx_atomic_or(&{0}, {1})", this.value, b);
return this.fetchOr(b);
}

public #if !scriptable inline #end function xor(b:Int):Int {
return untyped __cpp__("_hx_atomic_xor(&{0}, {1})", this.value, b);
return this.fetchXor(b);
}

public #if !scriptable inline #end function compareExchange(expected:Int, replacement:Int):Int {
return untyped __cpp__("_hx_atomic_compare_exchange(&{0}, {1}, {2})", this.value, expected, replacement);
return this.compareExchange(expected, replacement);
}

public #if !scriptable inline #end function exchange(value:Int):Int {
return untyped __cpp__("_hx_atomic_exchange(&{0}, {1})", this.value, value);
return this.exchange(value);
}

public #if !scriptable inline #end function load():Int {
return untyped __cpp__("_hx_atomic_load(&{0})", this.value);
return this.load();
}

public #if !scriptable inline #end function store(value:Int):Int {
return untyped __cpp__("_hx_atomic_store(&{0}, {1})", this.value, value);
return this.store(value);
}
#end
}
}
29 changes: 20 additions & 9 deletions std/cpp/_std/haxe/atomic/AtomicObject.hx
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
package haxe.atomic;

private typedef AtomicObjectData<T:{}> = Dynamic;
@: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;

#if cppia
extern
#end
abstract AtomicObject<T: {}>(AtomicObjectData<T>) {
abstract AtomicObject<T:{}>(AtomicObjectData<T>) {
#if cppia
public function new(value:T):Void;

Expand All @@ -17,24 +28,24 @@ abstract AtomicObject<T: {}>(AtomicObjectData<T>) {

public function store(value:T):T;
#else
public #if !scriptable inline #end function new(value:T) {
this = untyped __global__.__hxcpp_atomic_object_create(value);
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 untyped __global__.__hxcpp_atomic_object_compare_exchange(this, expected, replacement);
return this.compareExchange(expected, replacement);
}

public #if !scriptable inline #end function exchange(value:T):T {
return untyped __global__.__hxcpp_atomic_object_exchange(this, value);
return this.exchange(value);
}

public #if !scriptable inline #end function load():T {
return untyped __global__.__hxcpp_atomic_object_load(this);
return this.load();
}

public #if !scriptable inline #end function store(value:T):T {
return untyped __global__.__hxcpp_atomic_object_store(this, value);
return this.store(value);
}
#end
}
}
17 changes: 17 additions & 0 deletions tests/unit/src/unit/teststd/haxe/atomic/TestAtomicObject.hx
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
package unit.teststd.haxe.atomic;

private class Box {
public final value:Int;

public function new(value:Int) {
this.value = value;
}
}

class TestAtomicObject extends unit.Test {
public function testFieldAccessOnResult() {
final a = new haxe.atomic.AtomicObject(new Box(1));
eq(a.load().value, 1);
eq(a.store(new Box(2)).value, 2);
eq(a.exchange(new Box(3)).value, 2);
eq(a.compareExchange(a.load(), new Box(4)).value, 3);
eq(a.load().value, 4);
}

public function test() {
var a = new haxe.atomic.AtomicObject("Hey World!");
eq(a.load(), "Hey World!");
Expand Down
Loading