Skip to content

Commit 24d5416

Browse files
Simnclaude
andauthored
Don't enter GC free zones in atomic operations (#1443)
* Don't enter GC free zones in atomic operations Operations on std::atomic never block, so they don't need to let a collection run alongside them. Entering and leaving a zone is expensive: on Windows it costs two kernel calls (SetEvent/ResetEvent) and takes the global gThreadStateChangeLock, which made each atomic operation take roughly 650 ns instead of a few. For AtomicObject, the zone was also unsafe: a collection running while the thread is in the zone can move the referenced object, while load() is still handing out the raw pointer it read. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Make atomic operations GC safe points The GC free zones also made every atomic operation a GC safe point. A loop that spins on atomics without allocating relies on that: without it, a collection started by another thread waits for the spinning thread forever. Check for a pending collection at the start of each operation instead, which costs a read of gPauseForCollect. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent d3d6dea commit 24d5416

2 files changed

Lines changed: 22 additions & 22 deletions

File tree

‎src/hx/atomic/AtomicInt.cpp‎

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
#include "hx/atomic/AtomicInt.hpp"
33
#include <atomic>
44

5+
// Atomic operations never block, so they don't enter a GC free zone. Each one is still a GC
6+
// safe point, so that a loop which spins on atomics without allocating can't stall a collection.
7+
58
struct hx::atomic::AtomicInt_obj::Impl
69
{
710
std::atomic_int atomic;
@@ -14,42 +17,42 @@ hx::atomic::AtomicInt_obj::AtomicInt_obj(int val) : impl(new Impl())
1417

1518
int hx::atomic::AtomicInt_obj::fetchAdd(int val)
1619
{
17-
hx::AutoGCFreeZone _;
20+
__hxcpp_gc_safe_point();
1821

1922
return impl->atomic.fetch_add(val);
2023
}
2124

2225
int hx::atomic::AtomicInt_obj::fetchSub(int val)
2326
{
24-
hx::AutoGCFreeZone _;
27+
__hxcpp_gc_safe_point();
2528

2629
return impl->atomic.fetch_sub(val);
2730
}
2831

2932
int hx::atomic::AtomicInt_obj::fetchAnd(int val)
3033
{
31-
hx::AutoGCFreeZone _;
34+
__hxcpp_gc_safe_point();
3235

3336
return impl->atomic.fetch_and(val);
3437
}
3538

3639
int hx::atomic::AtomicInt_obj::fetchXor(int val)
3740
{
38-
hx::AutoGCFreeZone _;
41+
__hxcpp_gc_safe_point();
3942

4043
return impl->atomic.fetch_xor(val);
4144
}
4245

4346
int hx::atomic::AtomicInt_obj::fetchOr(int val)
4447
{
45-
hx::AutoGCFreeZone _;
48+
__hxcpp_gc_safe_point();
4649

4750
return impl->atomic.fetch_or(val);
4851
}
4952

5053
int hx::atomic::AtomicInt_obj::store(int val)
5154
{
52-
hx::AutoGCFreeZone _;
55+
__hxcpp_gc_safe_point();
5356

5457
impl->atomic.store(val);
5558

@@ -58,21 +61,21 @@ int hx::atomic::AtomicInt_obj::store(int val)
5861

5962
int hx::atomic::AtomicInt_obj::load()
6063
{
61-
hx::AutoGCFreeZone _;
64+
__hxcpp_gc_safe_point();
6265

6366
return impl->atomic.load();
6467
}
6568

6669
int hx::atomic::AtomicInt_obj::exchange(int val)
6770
{
68-
hx::AutoGCFreeZone _;
71+
__hxcpp_gc_safe_point();
6972

7073
return impl->atomic.exchange(val);
7174
}
7275

7376
int hx::atomic::AtomicInt_obj::compareExchange(int expected, int replacement)
7477
{
75-
hx::AutoGCFreeZone _;
78+
__hxcpp_gc_safe_point();
7679

7780
// On failure, compare_exchange_strong stores the current value in `original`.
7881
int original{ expected };

‎src/hx/atomic/AtomicObject.cpp‎

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
#include "hx/atomic/AtomicObject.hpp"
33
#include <atomic>
44

5+
// Atomic operations never block, so they don't enter a GC free zone. Each one is still a GC
6+
// safe point, so that a loop which spins on atomics without allocating can't stall a collection.
7+
58
struct hx::atomic::AtomicObject_obj::Impl
69
{
710
std::atomic<hx::Object*> atomic;
@@ -34,9 +37,9 @@ void hx::atomic::AtomicObject_obj::__Visit(HX_VISIT_PARAMS)
3437

3538
Dynamic hx::atomic::AtomicObject_obj::store(Dynamic val)
3639
{
37-
hx::EnterGCFreeZone();
40+
__hxcpp_gc_safe_point();
41+
3842
impl->atomic.store(val);
39-
hx::ExitGCFreeZone();
4043

4144
HX_OBJ_WB_GET(this, val.mPtr);
4245

@@ -45,16 +48,16 @@ Dynamic hx::atomic::AtomicObject_obj::store(Dynamic val)
4548

4649
Dynamic hx::atomic::AtomicObject_obj::load()
4750
{
48-
hx::AutoGCFreeZone _;
51+
__hxcpp_gc_safe_point();
4952

5053
return impl->atomic.load();
5154
}
5255

5356
Dynamic hx::atomic::AtomicObject_obj::exchange(Dynamic val)
5457
{
55-
hx::EnterGCFreeZone();
58+
__hxcpp_gc_safe_point();
59+
5660
Dynamic old{ impl->atomic.exchange(val) };
57-
hx::ExitGCFreeZone();
5861

5962
HX_OBJ_WB_GET(this, val.mPtr);
6063

@@ -63,25 +66,19 @@ Dynamic hx::atomic::AtomicObject_obj::exchange(Dynamic val)
6366

6467
Dynamic hx::atomic::AtomicObject_obj::compareExchange(Dynamic expected, Dynamic replacement)
6568
{
69+
__hxcpp_gc_safe_point();
70+
6671
// Note: using Dynamic instead of hx::Object* is important
6772
// Dynamic has an overloaded == operator, a raw pointer to hx::Object does not.
6873
Dynamic original{ impl->atomic.load() };
6974
while (original == expected)
7075
{
71-
hx::EnterGCFreeZone();
7276
if (impl->atomic.compare_exchange_strong(original.mPtr, replacement.mPtr))
7377
{
74-
hx::ExitGCFreeZone();
75-
7678
HX_OBJ_WB_GET(this, replacement.mPtr);
7779

7880
return original;
7981
}
80-
else
81-
{
82-
hx::ExitGCFreeZone();
83-
continue;
84-
}
8582
}
8683

8784
return original;

0 commit comments

Comments
 (0)