-
-
Notifications
You must be signed in to change notification settings - Fork 658
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Atomic operations. * Fix hashlink's atomic operations. * Add a target.atomics define. * Prevent complaint about "no assertions". * Fix doc gen. * Try to fix things... * Rework api a bit. * Add threaded atomic test, fix C#, fix a method signature. * Make AtomicInt work on cppia, add JS implementation. * Remove atomics on C#. * Fix doc gen, again. * `do ... while` loop for Java. Add some `inline`, and remove some unneeded `extern`. * Fix a potential race condition in the Java impl. * Fix NativeStackTrace.callStack for hl 1.12+. * Use cpp.AtomicInt instead of Int. * Update AtomicInt.hx * [hashlink] use library functions instead opcodes * Fix typo and add warning note to AtomicObject. * [hl] Fix AtomicObject, and bump hl-ver to make tests run. * Add AtomicBool * Inline AtomicBool * Add C# atomics back. * Fix doc gen for atomic bool. * Fix typo. * Add AtomicBool unit test and fix typo. * Fix hxcpp. * Remove leftover debug printf. * Try to fix the C# CI failure.
- Loading branch information
1 parent
7980171
commit 7c7f284
Showing
27 changed files
with
812 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package haxe.atomic; | ||
|
||
#if cppia | ||
extern | ||
#end | ||
abstract AtomicInt(cpp.Pointer<Int>) { | ||
public #if !(scriptable || cppia) inline #end function new(value:Int) { | ||
this = cpp.Pointer.ofArray([value]); | ||
} | ||
|
||
public #if !(scriptable || cppia) inline #end function add(b:Int):Int { | ||
return untyped __cpp__("_hx_atomic_add({0}, {1})", this, b); | ||
} | ||
|
||
public #if !(scriptable || cppia) inline #end function sub(b:Int):Int { | ||
return untyped __cpp__("_hx_atomic_sub({0}, {1})", this, b); | ||
} | ||
|
||
public #if !(scriptable || cppia) inline #end function and(b:Int):Int { | ||
return untyped __cpp__("_hx_atomic_and({0}, {1})", this, b); | ||
} | ||
|
||
public #if !(scriptable || cppia) inline #end function or(b:Int):Int { | ||
return untyped __cpp__("_hx_atomic_or({0}, {1})", this, b); | ||
} | ||
|
||
public #if !(scriptable || cppia) inline #end function xor(b:Int):Int { | ||
return untyped __cpp__("_hx_atomic_xor({0}, {1})", this, b); | ||
} | ||
|
||
public #if !(scriptable || cppia) inline #end function compareExchange(expected:Int, replacement:Int):Int { | ||
return untyped __cpp__("_hx_atomic_compare_exchange({0}, {1}, {2})", this, expected, replacement); | ||
} | ||
|
||
public #if !(scriptable || cppia) inline #end function exchange(value:Int):Int { | ||
return untyped __cpp__("_hx_atomic_exchange({0}, {1})", this, value); | ||
} | ||
|
||
public #if !(scriptable || cppia) inline #end function load():Int { | ||
return untyped __cpp__("_hx_atomic_load({0})", this); | ||
} | ||
|
||
public #if !(scriptable || cppia) inline #end function store(value:Int):Int { | ||
return untyped __cpp__("_hx_atomic_store({0}, {1})", this, value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package haxe.atomic; | ||
|
||
private class IntWrapper { | ||
public var value:Int; | ||
|
||
public function new(value:Int) { | ||
this.value = value; | ||
} | ||
} | ||
|
||
abstract AtomicInt(IntWrapper) to IntWrapper { | ||
public inline function new(value:Int) { | ||
this = new IntWrapper(value); | ||
} | ||
|
||
private inline function cas_loop(value:Int, op:(a:Int, b:Int) -> Int):Int { | ||
var oldValue; | ||
var newValue; | ||
do { | ||
oldValue = load(); | ||
newValue = op(oldValue, value); | ||
} while(compareExchange(oldValue, newValue) != oldValue); | ||
return oldValue; | ||
} | ||
|
||
public inline function add(b:Int):Int { | ||
return cas_loop(b, (a, b) -> a + b); | ||
} | ||
|
||
public inline function sub(b:Int):Int { | ||
return cas_loop(b, (a, b) -> a - b); | ||
} | ||
|
||
public inline function and(b:Int):Int { | ||
return cas_loop(b, (a, b) -> cast a & b); | ||
} | ||
|
||
public inline function or(b:Int):Int { | ||
return cas_loop(b, (a, b) -> cast a | b); | ||
} | ||
|
||
public inline function xor(b:Int):Int { | ||
return cas_loop(b, (a, b) -> cast a ^ b); | ||
} | ||
|
||
public inline function compareExchange(expected:Int, replacement:Int):Int { | ||
return cs.Syntax.code("System.Threading.Interlocked.CompareExchange(ref ({0}).value, {1}, {2})", this, replacement, expected); | ||
} | ||
|
||
public inline function exchange(value:Int):Int { | ||
return cs.Syntax.code("System.Threading.Interlocked.Exchange(ref ({0}).value, {1})", this, value); | ||
} | ||
|
||
public inline function load():Int { | ||
return this.value; // according to the CLI spec reads and writes are atomic | ||
} | ||
|
||
public inline function store(value:Int):Int { | ||
return this.value = value; // according to the CLI spec reads and writes are atomic | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package haxe.atomic; | ||
|
||
import cs.system.threading.Interlocked.*; | ||
|
||
private class ObjectWrapper<T:{}> { | ||
public var value:T; | ||
|
||
public function new(value:T) { | ||
this.value = value; | ||
} | ||
} | ||
|
||
extern abstract AtomicObject<T:{}>(ObjectWrapper<T>) { | ||
public inline function new(value:T) { | ||
this = new ObjectWrapper(value); | ||
} | ||
|
||
public inline function compareExchange(expected:T, replacement:T):T { | ||
return cs.Syntax.code("System.Threading.Interlocked.CompareExchange(ref ({0}).value, {1}, {2})", this, replacement, expected); | ||
} | ||
|
||
public inline function exchange(value:T):T { | ||
return cs.Syntax.code("System.Threading.Interlocked.Exchange(ref ({0}).value, {1})", this, value); | ||
} | ||
|
||
public inline function load():T { | ||
return this.value; // according to the CLI spec reads and writes are atomic | ||
} | ||
|
||
public inline function store(value:T):T { | ||
return this.value = value; // according to the CLI spec reads and writes are atomic | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package haxe.atomic; | ||
|
||
#if !(target.atomics || core_api) | ||
#error "Atomic operations are not supported on this target!" | ||
#end | ||
|
||
/** | ||
Atomic boolean. | ||
(js) The Atomics and SharedArrayBuffer objects need to be available. Errors will be thrown if this is not the case. | ||
**/ | ||
@:coreApi | ||
abstract AtomicBool(AtomicInt) { | ||
private inline function toInt(v:Bool):Int { | ||
return v ? 1 : 0; | ||
} | ||
|
||
private inline function toBool(v:Int):Bool { | ||
return v == 1; | ||
} | ||
|
||
public inline function new(value:Bool):Void { | ||
this = new AtomicInt(toInt(value)); | ||
} | ||
|
||
/** | ||
Atomically compares the value of `a` with `expected` and replaces `a` with `replacement` if they are equal.. | ||
Returns the original value of `a`. | ||
**/ | ||
public inline function compareExchange(expected:Bool, replacement:Bool):Bool { | ||
return toBool(this.compareExchange(toInt(expected), toInt(replacement))); | ||
} | ||
|
||
/** | ||
Atomically exchanges `a` with `value`. | ||
Returns the original value of `a`. | ||
**/ | ||
public inline function exchange(value:Bool):Bool { | ||
return toBool(this.exchange(toInt(value))); | ||
} | ||
|
||
/** | ||
Atomically fetches the value of `a`. | ||
**/ | ||
public inline function load():Bool { | ||
return toBool(this.load()); | ||
} | ||
|
||
/** | ||
Atomically stores `value` into `a`. | ||
Returns the value that has been stored. | ||
**/ | ||
public inline function store(value:Bool):Bool { | ||
return toBool(this.store(toInt(value))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package haxe.atomic; | ||
|
||
#if !(target.atomics || core_api) | ||
#error "This target does not support atomic operations." | ||
#end | ||
|
||
/** | ||
Atomic integer. | ||
(js) The Atomics and SharedArrayBuffer objects need to be available. Errors will be thrown if this is not the case. | ||
**/ | ||
@:coreType | ||
abstract AtomicInt { | ||
public function new(value:Int):Void; | ||
|
||
/** | ||
Atomically adds `b` to `a`. | ||
Returns the original value of `a`. | ||
**/ | ||
public function add(b:Int):Int; | ||
|
||
/** | ||
Atomically substracts `b` from `a`. | ||
Returns the original value of `a`. | ||
**/ | ||
public function sub(b:Int):Int; | ||
|
||
/** | ||
Atomically computes the bitwise and of `a` and `b` and stores it in `a`. | ||
Returns the original value of `a`. | ||
**/ | ||
public function and(b:Int):Int; | ||
|
||
/** | ||
Atomically computes the bitwise or of `a` and `b` and stores it in `a`. | ||
Returns the original value of `a`. | ||
**/ | ||
public function or(b:Int):Int; | ||
|
||
/** | ||
Atomically computes the bitwise xor of `a` and `b` and stores it in `a`. | ||
Returns the original value of `a`. | ||
**/ | ||
public function xor(b:Int):Int; | ||
|
||
/** | ||
Atomically compares the value of `a` with `expected` and replaces `a` with `replacement` if they are equal.. | ||
Returns the original value of `a`. | ||
**/ | ||
public function compareExchange(expected:Int, replacement:Int):Int; | ||
|
||
/** | ||
Atomically exchanges `a` with `value`. | ||
Returns the original value of `a`. | ||
**/ | ||
public function exchange(value:Int):Int; | ||
|
||
/** | ||
Atomically fetches the value of `a`. | ||
**/ | ||
public function load():Int; | ||
|
||
/** | ||
Atomically stores `value` into `a`. | ||
Returns the value that has been stored. | ||
**/ | ||
public function store(value:Int):Int; | ||
} |
Oops, something went wrong.