Skip to content

Commit 0a656d7

Browse files
Add the typed atomics Haxe 5's haxe.atomic is built on
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.
1 parent b42f880 commit 0a656d7

13 files changed

Lines changed: 310 additions & 0 deletions

File tree

‎include/hx/atomic/AtomicInt.hpp‎

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#pragma once
2+
3+
#ifndef HXCPP_H
4+
#include <hxcpp.h>
5+
#endif
6+
7+
#include <atomic>
8+
9+
HX_DECLARE_CLASS2(hx, atomic, AtomicInt)
10+
11+
namespace hx
12+
{
13+
namespace atomic
14+
{
15+
// What Haxe 5's haxe.atomic.AtomicInt holds. The atomic lives in the object, so
16+
// nothing outside the GC heap outlives it. Atomic operations never block, so they
17+
// don't enter a GC free zone; each one is a GC safe point instead, so a loop that
18+
// spins on atomics without allocating can't stall a collection.
19+
class AtomicInt_obj final : public ::hx::Object
20+
{
21+
std::atomic<int> atomic;
22+
23+
public:
24+
// Holds no pointers, so the collector has nothing to scan in it
25+
inline void *operator new(size_t inSize)
26+
{
27+
return ::hx::Object::operator new(inSize, false, "hx.atomic.AtomicInt");
28+
}
29+
30+
inline AtomicInt_obj(int val) : atomic(val) {}
31+
32+
inline int fetchAdd(int val)
33+
{
34+
__hxcpp_gc_safe_point();
35+
return atomic.fetch_add(val);
36+
}
37+
38+
inline int fetchSub(int val)
39+
{
40+
__hxcpp_gc_safe_point();
41+
return atomic.fetch_sub(val);
42+
}
43+
44+
inline int fetchAnd(int val)
45+
{
46+
__hxcpp_gc_safe_point();
47+
return atomic.fetch_and(val);
48+
}
49+
50+
inline int fetchOr(int val)
51+
{
52+
__hxcpp_gc_safe_point();
53+
return atomic.fetch_or(val);
54+
}
55+
56+
inline int fetchXor(int val)
57+
{
58+
__hxcpp_gc_safe_point();
59+
return atomic.fetch_xor(val);
60+
}
61+
62+
inline int store(int val)
63+
{
64+
__hxcpp_gc_safe_point();
65+
atomic.store(val);
66+
return val;
67+
}
68+
69+
inline int load()
70+
{
71+
__hxcpp_gc_safe_point();
72+
return atomic.load();
73+
}
74+
75+
inline int exchange(int val)
76+
{
77+
__hxcpp_gc_safe_point();
78+
return atomic.exchange(val);
79+
}
80+
81+
// Returns the value it found, which equals expected only when it stored
82+
inline int compareExchange(int expected, int replacement)
83+
{
84+
__hxcpp_gc_safe_point();
85+
// On failure, compare_exchange_strong writes the current value into expected
86+
atomic.compare_exchange_strong(expected, replacement);
87+
return expected;
88+
}
89+
};
90+
}
91+
}

‎include/hx/atomic/AtomicObject.hpp‎

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#pragma once
2+
3+
#ifndef HXCPP_H
4+
#include <hxcpp.h>
5+
#endif
6+
7+
#include <atomic>
8+
9+
HX_DECLARE_CLASS2(hx, atomic, AtomicObject)
10+
11+
namespace hx
12+
{
13+
namespace atomic
14+
{
15+
// What Haxe 5's haxe.atomic.AtomicObject holds. Like AtomicInt, each operation is a
16+
// GC safe point rather than a GC free zone: a collection that ran inside a free zone
17+
// could move the object a load had just read.
18+
class AtomicObject_obj final : public ::hx::Object
19+
{
20+
std::atomic<::hx::Object *> atomic;
21+
22+
public:
23+
inline AtomicObject_obj(::Dynamic val) : atomic(val.mPtr)
24+
{
25+
HX_OBJ_WB_NEW_MARKED_OBJECT(this);
26+
}
27+
28+
void __Mark(::hx::MarkContext *__inCtx) HXCPP_OVERRIDE
29+
{
30+
::Dynamic ptr = atomic.load();
31+
HX_MARK_MEMBER(ptr);
32+
}
33+
34+
#ifdef HXCPP_VISIT_ALLOCS
35+
void __Visit(::hx::VisitContext *__inCtx) HXCPP_OVERRIDE
36+
{
37+
::hx::Object *ptr = atomic.load();
38+
HX_VISIT_MEMBER(ptr);
39+
atomic.store(ptr);
40+
}
41+
#endif
42+
43+
inline ::Dynamic store(::Dynamic val)
44+
{
45+
__hxcpp_gc_safe_point();
46+
atomic.store(val.mPtr);
47+
HX_OBJ_WB_GET(this, val.mPtr);
48+
return val;
49+
}
50+
51+
inline ::Dynamic load()
52+
{
53+
__hxcpp_gc_safe_point();
54+
return atomic.load();
55+
}
56+
57+
inline ::Dynamic exchange(::Dynamic val)
58+
{
59+
__hxcpp_gc_safe_point();
60+
::Dynamic old = atomic.exchange(val.mPtr);
61+
HX_OBJ_WB_GET(this, val.mPtr);
62+
return old;
63+
}
64+
65+
// Compares with Dynamic's ==, as the intrinsics Haxe 4 uses do, so boxed numbers
66+
// and strings compare by value. Returns the value it found.
67+
inline ::Dynamic compareExchange(::Dynamic expected, ::Dynamic replacement)
68+
{
69+
__hxcpp_gc_safe_point();
70+
::Dynamic original = atomic.load();
71+
while (original == expected)
72+
{
73+
// On failure, this writes the current value into original and tries again
74+
if (atomic.compare_exchange_weak(original.mPtr, replacement.mPtr))
75+
{
76+
HX_OBJ_WB_GET(this, replacement.mPtr);
77+
return original;
78+
}
79+
}
80+
return original;
81+
}
82+
};
83+
}
84+
}

‎test/native/Native.hx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class Native
2121

2222
#if (haxe_ver>=5 && !haxe_5_preview_1)
2323
new tests.TestScratch(),
24+
new tests.TestAtomicObject(),
2425

2526
new tests.marshalling.classes.TestLocalValueType(),
2627
new tests.marshalling.classes.TestClassValueType(),
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package tests;
2+
3+
import haxe.atomic.AtomicObject;
4+
import utest.Assert;
5+
import utest.Test;
6+
7+
// C++ has haxe.atomic.AtomicObject from the Haxe 5 that builds it on
8+
// hx::atomic::AtomicObject
9+
class TestAtomicObject extends Test {
10+
static function churn() {
11+
var kept = 0;
12+
for (i in 0...50000)
13+
kept += [i, i + 1].length;
14+
cpp.vm.Gc.run(false);
15+
cpp.vm.Gc.run(true);
16+
}
17+
18+
function test_operations() {
19+
final o = new AtomicObject<Array<Int>>([1, 2, 3]);
20+
final first = o.load();
21+
22+
Assert.same([1, 2, 3], first);
23+
// A failed exchange returns the value it found and changes nothing
24+
Assert.equals(first, o.compareExchange([1, 2, 3], [9]));
25+
Assert.equals(first, o.load());
26+
27+
final second = [4, 5];
28+
Assert.equals(first, o.compareExchange(first, second));
29+
Assert.equals(second, o.load());
30+
31+
final third = [6];
32+
Assert.equals(second, o.exchange(third));
33+
final fourth = [7];
34+
Assert.equals(fourth, o.store(fourth));
35+
Assert.equals(fourth, o.load());
36+
}
37+
38+
function test_strings_compare_by_value() {
39+
final o = new AtomicObject<String>("a" + Std.string(1));
40+
41+
Assert.equals("a1", o.compareExchange("a1", "b"));
42+
Assert.equals("b", o.load());
43+
}
44+
45+
// Values only an AtomicObject holds survive collections, including values
46+
// stored after the AtomicObject itself was collected into the old generation
47+
function test_values_survive_collections() {
48+
final o = new AtomicObject<Array<Int>>([1, 2, 3]);
49+
churn();
50+
Assert.same([1, 2, 3], o.load());
51+
52+
o.compareExchange(o.load(), [4, 5]);
53+
churn();
54+
Assert.same([4, 5], o.load());
55+
56+
o.exchange([6]);
57+
churn();
58+
Assert.same([6], o.load());
59+
60+
o.store([7]);
61+
churn();
62+
Assert.same([7], o.load());
63+
}
64+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import haxe.atomic.AtomicInt;
2+
3+
function main() {
4+
// Scripts reach haxe.atomic.AtomicInt through the host's copy of the abstract
5+
new AtomicInt(0).add(1);
6+
7+
var script = haxe.io.Path.join([haxe.io.Path.directory(Sys.programPath()), "script.cppia"]);
8+
for (jit in [false, true]) {
9+
cpp.cppia.Host.enableJit(jit);
10+
try {
11+
cpp.cppia.Host.runFile(script);
12+
} catch (e:Dynamic) {
13+
Sys.println("error: " + e);
14+
}
15+
}
16+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import haxe.atomic.AtomicInt;
2+
3+
class Script {
4+
static function main() {
5+
final a = new AtomicInt(1);
6+
Sys.println([a.add(2), a.compareExchange(5, 9), a.compareExchange(3, 9), a.load()]);
7+
}
8+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
--main Script
2+
-cppia bin/script.cppia
3+
--next
4+
--main Main
5+
-cpp bin
6+
-D scriptable
7+
--dce no
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[1,3,3,9]
2+
[1,3,3,9]
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import haxe.atomic.AtomicInt;
2+
import sys.thread.Thread;
3+
4+
// haxe.atomic.AtomicInt: Haxe 4 and 5.0.0-preview.1 build it on the runtime's
5+
// atomic intrinsics, later Haxe 5 on hx::atomic::AtomicInt. test/native checks
6+
// AtomicObject, which C++ only has from that later Haxe 5 on
7+
class Main {
8+
static function main() {
9+
final a = new AtomicInt(1);
10+
Sys.println([a.add(2), a.sub(1), a.and(6), a.or(1), a.xor(4), a.load()]);
11+
// A failed exchange returns the value it found, not the one expected
12+
Sys.println([a.compareExchange(5, 9), a.load(), a.compareExchange(7, 9), a.load()]);
13+
Sys.println([a.exchange(11), a.store(12), a.load()]);
14+
15+
final counter = new AtomicInt(0);
16+
final finished = new AtomicInt(0);
17+
for (t in 0...4) {
18+
Thread.create(() -> {
19+
for (i in 0...20000)
20+
counter.add(1);
21+
finished.add(1);
22+
});
23+
}
24+
while (finished.load() < 4)
25+
Sys.sleep(0.001);
26+
Sys.println(counter.load());
27+
}
28+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-m Main
2+
--cpp bin

0 commit comments

Comments
 (0)