Skip to content
Open
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
59 changes: 59 additions & 0 deletions std/cpp/_std/haxe/atomic/AtomicObject.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package haxe.atomic;

private class AtomicObjectImpl<T> {
public final lock : sys.thread.Mutex;

public var obj : T;

public function new(obj) {
this.lock = new sys.thread.Mutex();
this.obj = obj;
}
}

abstract AtomicObject<T:{}>(AtomicObjectImpl<T>) {
public function new(value:T) {
this = new AtomicObjectImpl(value);
}

public function compareExchange(expected:T, replacement:T):T {
this.lock.acquire();
return if (this.obj == expected) {
final current = this.obj;
this.obj = replacement;
this.lock.release();

current;
} else {
final current = this.obj;
this.lock.release();

current;
}
}

public function exchange(value:T):T {
this.lock.acquire();
final current = this.obj;
this.obj = value;
this.lock.release();

return current;
}

public function load():T {
this.lock.acquire();
final current = this.obj;
this.lock.release();

return current;
}

public function store(value:T):T {
this.lock.acquire();
this.obj = value;
this.lock.release();

return value;
}
}
4 changes: 2 additions & 2 deletions std/haxe/atomic/AtomicObject.hx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ package haxe.atomic;
#error "This target does not support atomic operations."
#end

#if (js || cpp)
#error "JavaScript and Hxcpp do not support AtomicObject"
#if js
#error "JavaScript does not support AtomicObject"
#end

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/src/unitstd/haxe/atomic/AtomicObject.unit.hx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#if (target.atomics && !(js || cpp))
#if (target.atomics && !js)
var a = new haxe.atomic.AtomicObject("Hey World!");

a.load() == "Hey World!";
Expand Down
Loading