navid/haze / commit

13ba2f33894c

Navid M <navidmomtahen@gmail.com> · 2026-12-02

parents: dd80043cbfde

Browse files at this commit · Download source

all: multithreaded runtime and collector
--- a/CMakeLists.txt+++ b/CMakeLists.txt@@ -119,7 +119,7 @@ target_link_libraries(hz PRIVATE hazert)  find_package(Threads REQUIRED)-add_executable(hazert_test EXCLUDE_FROM_ALL tests/runtime_test.cpp)+add_executable(hazert_test tests/runtime_test.cpp) target_link_libraries(hazert_test PRIVATE hazert Threads::Threads)  add_custom_target(clean-samples@@ -183,6 +183,10 @@     add_haze_sample_test("${source}") endforeach() add_haze_sample_test("${CMAKE_CURRENT_SOURCE_DIR}/samples/baseline_2/main.hz")++add_test(NAME runtime COMMAND hazert_test)+set_tests_properties(runtime PROPERTIES+    ENVIRONMENT "HAZE_GC_WORKERS=4;HAZE_GC_VERIFY=1")  add_custom_target(check-samples     COMMAND "${CMAKE_CTEST_COMMAND}" --output-on-failure--- a/runtime/hazert.cpp+++ b/runtime/hazert.cpp@@ -8,7 +8,6 @@ #include <cstdlib> #include <cstring> #include <new>-#include <thread>  #if defined(_WIN32) #ifndef WIN32_LEAN_AND_MEAN@@ -16,6 +15,8 @@ #endif #include <windows.h> #else+#include <pthread.h>+#include <sched.h> #include <sys/mman.h> #include <unistd.h> #if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)@@ -38,6 +39,7 @@ constexpr uint32_t MAX_PINNED = 8192; constexpr uint32_t MAX_LARGE = 65536; constexpr uint32_t MAX_MUTATORS = 256;+constexpr uint32_t MAX_WORKERS = 8; constexpr uint32_t MARK_STACK = 1u << 20; constexpr uint64_t DEFAULT_HEAP = 64ull << 30; constexpr uint64_t MIN_HEAP = 16ull << 20;@@ -54,6 +56,10 @@     PINNED = 1u << 0,     REMEMBERED = 1u << 1,     LARGE = 1u << 2,+};++enum MutatorFlags : uint32_t {+    IN_NATIVE = 1u << 0, };  enum class BlockState : uint8_t {@@ -181,8 +187,128 @@ void os_free(void* p, size_t n) {     if (p)         munmap(p, n);+}+#endif++struct ThreadStart {+    void (*fn)(void*);+    void* arg;+};++#if defined(_WIN32)+using OsThread = HANDLE;+DWORD WINAPI os_thread_trampoline(LPVOID p) {+    ThreadStart* start = static_cast<ThreadStart*>(p);+    start->fn(start->arg);+    return 0;+}+bool os_thread_start(OsThread& thread, ThreadStart* start) {+    thread = CreateThread(nullptr, 0, os_thread_trampoline, start, 0, nullptr);+    return thread != nullptr;+}+void os_thread_join(OsThread thread) {+    if (thread) {+        WaitForSingleObject(thread, INFINITE);+        CloseHandle(thread);+    }+}+void os_yield() {+    SwitchToThread();+}+#else+using OsThread = pthread_t;+void* os_thread_trampoline(void* p) {+    ThreadStart* start = static_cast<ThreadStart*>(p);+    start->fn(start->arg);+    return nullptr;+}+bool os_thread_start(OsThread& thread, ThreadStart* start) {+    return pthread_create(&thread, nullptr, os_thread_trampoline, start) == 0;+}+void os_thread_join(OsThread thread) {+    pthread_join(thread, nullptr);+}+void os_yield() {+    sched_yield(); }+#endif++uint32_t detect_workers() {+    uint64_t configured = env_u64("HAZE_GC_WORKERS", 0);+    if (configured)+        return static_cast<uint32_t>(configured > MAX_WORKERS ? MAX_WORKERS : configured);+#if defined(_WIN32)+    uint32_t cpus = GetActiveProcessorCount(ALL_PROCESSOR_GROUPS);+#else+    long cpus = sysconf(_SC_NPROCESSORS_ONLN); #endif+    if (cpus < 1)+        cpus = 1;+    return static_cast<uint32_t>(cpus > MAX_WORKERS ? MAX_WORKERS : cpus);+}++void spin_lock(std::atomic_flag& lock);+void spin_unlock(std::atomic_flag& lock);++struct Heap;++struct MarkWork {+    void** items = nullptr;+    uint32_t capacity = 0;+    uint32_t top = 0;+    uint32_t bottom = 0;+    std::atomic_flag lock = ATOMIC_FLAG_INIT;++    bool push(void* item) {+        spin_lock(lock);+        if (bottom >= capacity) {+            spin_unlock(lock);+            return false;+        }+        items[bottom++] = item;+        spin_unlock(lock);+        return true;+    }++    bool pop(void*& item) {+        spin_lock(lock);+        if (bottom <= top) {+            spin_unlock(lock);+            return false;+        }+        item = items[--bottom];+        spin_unlock(lock);+        return true;+    }++    bool steal(void*& item) {+        spin_lock(lock);+        if (bottom <= top) {+            spin_unlock(lock);+            return false;+        }+        item = items[top++];+        spin_unlock(lock);+        return true;+    }+};++struct MarkWorkerCtx {+    Heap* heap = nullptr;+    MarkWork* work = nullptr;+    MarkWork* all = nullptr;+    uint32_t count = 0;+    uint32_t index = 0;+    std::atomic<uint32_t>* busy = nullptr;+};++struct SweepCtx {+    Heap* heap = nullptr;+    std::atomic<uint32_t>* next = nullptr;+    std::atomic<uint64_t>* live_total = nullptr;+};++void* g_mark_items[MARK_STACK] = {};  struct Heap {     uint8_t* base = nullptr;@@ -228,6 +354,9 @@     std::atomic_flag block_lock = ATOMIC_FLAG_INIT;     std::atomic_flag type_lock = ATOMIC_FLAG_INIT;     std::atomic_flag root_lock = ATOMIC_FLAG_INIT;+    std::atomic_flag pinned_lock = ATOMIC_FLAG_INIT;++    uint32_t workers = 1;      Mutator* mutators[MAX_MUTATORS] = {};     uint32_t nmutators = 0;@@ -271,9 +400,14 @@     bool find_hole(Mutator* m, uint32_t total);      void collect();-    void mark_roots();-    void mark_object(void* obj);-    void sweep();+    void parallel_mark();+    void scan_object(void* obj, MarkWorkerCtx& ctx);+    void mark_worker(MarkWorkerCtx& ctx);+    static void mark_worker_entry(void* arg);+    void parallel_sweep();+    void sweep_block(uint32_t block, std::atomic<uint64_t>& live_total);+    void sweep_worker(SweepCtx& ctx);+    static void sweep_worker_entry(void* arg);     void reset_mutator(Mutator* m);     void verify(bool after); @@ -285,7 +419,7 @@  void spin_lock(std::atomic_flag& lock) {     while (lock.test_and_set(std::memory_order_acquire))-        std::this_thread::yield();+        os_yield(); }  void spin_unlock(std::atomic_flag& lock) {@@ -345,6 +479,9 @@     verify_mode = env_on("HAZE_GC_VERIFY");     poison = env_on("HAZE_GC_POISON");     stress_n = static_cast<uint32_t>(env_u64("HAZE_GC_STRESS", 0));+    workers = detect_workers();+    if (workers < 1)+        workers = 1;      uint64_t wanted = env_u64("HAZE_HEAP_SIZE", DEFAULT_HEAP);     if (wanted < MIN_HEAP)@@ -423,7 +560,7 @@     if (!m || !m->safepoint_state)         return;     while (safepoint_request.load(std::memory_order_acquire))-        std::this_thread::yield();+        os_yield();     m->safepoint_state = 0;     safepoint_arrived.fetch_sub(1, std::memory_order_acq_rel); }@@ -741,56 +878,119 @@     }     return p; }--void Heap::mark_object(void* obj) {-    static void* stack[MARK_STACK];-    size_t sp = 0; +void Heap::scan_object(void* obj, MarkWorkerCtx& ctx) {     if (!obj)         return;-    stack[sp++] = obj;+    bool large_object = is_large_object(obj);+    if (!in_heap(obj) && !large_object)+        return;+    if (reinterpret_cast<uintptr_t>(obj) & (GRANULE - 1))+        return; -    while (sp) {-        void* p = stack[--sp];-        bool large_object = is_large_object(p);-        if (!in_heap(p) && !large_object)-            continue;-        if (reinterpret_cast<uintptr_t>(p) & (GRANULE - 1))-            continue;+    Header* h = reinterpret_cast<Header*>(obj);+    uint8_t previous = h->mark;+    if (previous == epoch)+        return;+    std::atomic_ref<uint8_t> mark(h->mark);+    uint8_t expected = previous;+    if (!mark.compare_exchange_strong(expected, epoch, std::memory_order_acq_rel,+                                      std::memory_order_acquire))+        return; -        Header* h = reinterpret_cast<Header*>(p);-        if (h->mark == epoch)-            continue;-        h->mark = epoch;+    uint8_t header_flags =+        std::atomic_ref<uint8_t>(h->flags).load(std::memory_order_acquire);+    if (!(header_flags & LARGE) && !large_object) {+        uint32_t b = block_index(obj);+        uint8_t* block = base + static_cast<size_t>(b) * BLOCK_SIZE;+        uint32_t first = static_cast<uint32_t>(static_cast<uint8_t*>(obj) - block) / LINE_SIZE;+        uint32_t total = types[h->type_id].size;+        if (total == 0)+            return;+        uint32_t last =+            static_cast<uint32_t>((static_cast<uint8_t*>(obj) + total - 1) - block) / LINE_SIZE;+        uint32_t base_line = b * LINES_PER_BLOCK;+        for (uint32_t line = first; line <= last; line++)+            std::atomic_ref<uint8_t>(line_marks[base_line + line])+                .store(epoch, std::memory_order_relaxed);+    } -        if (!(h->flags & LARGE) && !large_object) {-            uint32_t b = block_index(p);-            uint8_t* block = base + static_cast<size_t>(b) * BLOCK_SIZE;-            uint32_t first = static_cast<uint32_t>(static_cast<uint8_t*>(p) - block) / LINE_SIZE;-            uint32_t total = types[h->type_id].size;-            if (total == 0)-                continue;-            uint32_t last =-                static_cast<uint32_t>((static_cast<uint8_t*>(p) + total - 1) - block) / LINE_SIZE;-            uint32_t base_line = b * LINES_PER_BLOCK;-            for (uint32_t line = first; line <= last; line++)-                line_marks[base_line + line] = epoch;+    const TypeDesc& td = types[h->type_id];+    if (td.pointer_count && td.pointer_offsets) {+        for (uint32_t i = 0; i < td.pointer_count; i++) {+            uint32_t off = td.pointer_offsets[i];+            void* child = nullptr;+            std::memcpy(&child, static_cast<uint8_t*>(obj) + off, sizeof(child));+            if (child && !ctx.work->push(child)) {+                report("mark work queue exhausted");+                std::abort();+            }         }+    }+} -        const TypeDesc& td = types[h->type_id];-        if (td.pointer_count && td.pointer_offsets) {-            for (uint32_t i = 0; i < td.pointer_count; i++) {-                uint32_t off = td.pointer_offsets[i];-                void* child = nullptr;-                std::memcpy(&child, static_cast<uint8_t*>(p) + off, sizeof(child));-                if (child && sp < MARK_STACK)-                    stack[sp++] = child;+void Heap::mark_worker(MarkWorkerCtx& ctx) {+    for (;;) {+        void* obj = nullptr;+        bool got = ctx.work->pop(obj);+        if (!got) {+            for (uint32_t i = 1; i < ctx.count; i++) {+                MarkWork& victim = ctx.all[(ctx.index + i) % ctx.count];+                if (victim.steal(obj)) {+                    got = true;+                    break;+                }             }+        }+        if (got) {+            ctx.busy->fetch_add(1, std::memory_order_acq_rel);+            scan_object(obj, ctx);+            ctx.busy->fetch_sub(1, std::memory_order_acq_rel);+            continue;         }+        if (ctx.busy->load(std::memory_order_acquire) == 0)+            break;+        os_yield();     } } -void Heap::mark_roots() {+void Heap::mark_worker_entry(void* arg) {+    MarkWorkerCtx* ctx = static_cast<MarkWorkerCtx*>(arg);+    ctx->heap->mark_worker(*ctx);+}++void Heap::parallel_mark() {+    uint32_t count = workers;+    if (count < 1)+        count = 1;+    if (count > MAX_WORKERS)+        count = MAX_WORKERS;++    MarkWork works[MAX_WORKERS];+    MarkWorkerCtx contexts[MAX_WORKERS];+    std::atomic<uint32_t> busy{0};+    const uint32_t chunk = MARK_STACK / MAX_WORKERS;+    for (uint32_t i = 0; i < count; i++) {+        works[i].items = g_mark_items + i * chunk;+        works[i].capacity = chunk;+        works[i].top = 0;+        works[i].bottom = 0;+        contexts[i].heap = this;+        contexts[i].work = &works[i];+        contexts[i].all = works;+        contexts[i].count = count;+        contexts[i].index = i;+        contexts[i].busy = &busy;+    }++    uint32_t seed_index = 0;+    auto seed = [&](void* obj) {+        if (!obj)+            return;+        scan_object(obj, contexts[seed_index]);+        seed_index = (seed_index + 1) % count;+    };+     spin_lock(mutator_lock);     for (uint32_t mi = 0; mi < nmutators; mi++) {         Mutator* m = mutators[mi];@@ -799,66 +999,149 @@                 continue;             const RootMap* map = reinterpret_cast<const RootMap*>(f->descriptor);             for (uint32_t i = 0; i < map->count; i++)-                mark_object(f->slots[i]);+                seed(f->slots[i]);         }     }     spin_unlock(mutator_lock);      spin_lock(root_lock);-    for (uint32_t i = 0; i < nroots; i++) {+    for (uint32_t i = 0; i < nroots; i++)         if (global_roots[i])-            mark_object(*global_roots[i]);-    }+            seed(*global_roots[i]);     spin_unlock(root_lock); +    spin_lock(pinned_lock);     for (uint32_t i = 0; i < npinned; i++)-        mark_object(pinned[i]);-}+        seed(pinned[i]);+    spin_unlock(pinned_lock); -void Heap::sweep() {-    uint64_t live_total = 0;+    if (count == 1) {+        mark_worker(contexts[0]);+        return;+    } -    for (uint32_t b = 0; b < nblocks; b++) {-        uint8_t state = meta[b].state;-        if (state == static_cast<uint8_t>(BlockState::FREE) ||-            state == static_cast<uint8_t>(BlockState::LOS_RESERVED))-            continue;+    ThreadStart starts[MAX_WORKERS];+    OsThread threads[MAX_WORKERS];+    bool started[MAX_WORKERS] = {};+    for (uint32_t i = 1; i < count; i++) {+        starts[i].fn = &Heap::mark_worker_entry;+        starts[i].arg = &contexts[i];+        started[i] = os_thread_start(threads[i], &starts[i]);+    } -        uint32_t base_line = b * LINES_PER_BLOCK;-        uint32_t live = 0;-        for (uint32_t i = 0; i < LINES_PER_BLOCK; i++)-            if (line_marks[base_line + i] == epoch)-                live++;+    mark_worker(contexts[0]); -        live_total += live;+    for (uint32_t i = 1; i < count; i++)+        if (started[i])+            os_thread_join(threads[i]);+} -        if (live == 0) {-            if (poison) {-                uint8_t* block = base + static_cast<size_t>(b) * BLOCK_SIZE;-                std::memset(block, 0xDD, BLOCK_SIZE);-            }-            push_free(b);-        } else if (live == LINES_PER_BLOCK) {-            meta[b].state = static_cast<uint8_t>(BlockState::FULL);-            meta[b].marked_lines = LINES_PER_BLOCK;-            meta[b].holes = 0;-        } else {-            uint32_t holes = 0;-            bool in_hole = false;-            for (uint32_t i = 0; i < LINES_PER_BLOCK; i++) {-                bool dead = line_marks[base_line + i] != epoch;-                if (dead && !in_hole) {-                    holes++;-                    in_hole = true;-                } else if (!dead) {-                    in_hole = false;-                }+static uint32_t count_live_lines(const uint8_t* marks, uint8_t epoch) {+    uint32_t live = 0;+    uint32_t i = 0;+    const uint64_t target = 0x0101010101010101ull * epoch;+    const uint64_t high_bits = 0x8080808080808080ull;+    for (; i + 8 <= LINES_PER_BLOCK; i += 8) {+        uint64_t word = 0;+        std::memcpy(&word, marks + i, sizeof(word));+        uint64_t equal = ~(word ^ target) & high_bits;+#if defined(__GNUC__) || defined(__clang__)+        live += static_cast<uint32_t>(__builtin_popcountll(equal));+#else+        while (equal) {+            live += static_cast<uint32_t>(equal & 1);+            equal >>= 1;+        }+#endif+    }+    for (; i < LINES_PER_BLOCK; i++)+        if (marks[i] == epoch)+            live++;+    return live;+}++void Heap::sweep_block(uint32_t b, std::atomic<uint64_t>& live_total) {+    uint8_t state = meta[b].state;+    if (state == static_cast<uint8_t>(BlockState::FREE) ||+        state == static_cast<uint8_t>(BlockState::LOS_RESERVED))+        return;++    uint32_t base_line = b * LINES_PER_BLOCK;+    uint32_t live = count_live_lines(line_marks + base_line, epoch);+    live_total.fetch_add(live, std::memory_order_relaxed);++    if (live == 0) {+        if (poison) {+            uint8_t* block = base + static_cast<size_t>(b) * BLOCK_SIZE;+            std::memset(block, 0xDD, BLOCK_SIZE);+        }+        push_free(b);+    } else if (live == LINES_PER_BLOCK) {+        meta[b].state = static_cast<uint8_t>(BlockState::FULL);+        meta[b].marked_lines = LINES_PER_BLOCK;+        meta[b].holes = 0;+    } else {+        uint32_t holes = 0;+        bool in_hole = false;+        for (uint32_t i = 0; i < LINES_PER_BLOCK; i++) {+            bool dead = line_marks[base_line + i] != epoch;+            if (dead && !in_hole) {+                holes++;+                in_hole = true;+            } else if (!dead) {+                in_hole = false;             }-            meta[b].marked_lines = static_cast<uint16_t>(live);-            meta[b].holes = static_cast<uint16_t>(holes);-            push_recyclable(b);         }+        meta[b].marked_lines = static_cast<uint16_t>(live);+        meta[b].holes = static_cast<uint16_t>(holes);+        push_recyclable(b);+    }+}++void Heap::sweep_worker(SweepCtx& ctx) {+    for (;;) {+        uint32_t b = ctx.next->fetch_add(1, std::memory_order_relaxed);+        if (b >= nblocks)+            break;+        sweep_block(b, *ctx.live_total);+    }+}++void Heap::sweep_worker_entry(void* arg) {+    SweepCtx* ctx = static_cast<SweepCtx*>(arg);+    ctx->heap->sweep_worker(*ctx);+}++void Heap::parallel_sweep() {+    uint32_t count = workers;+    if (count < 1)+        count = 1;+    if (count > MAX_WORKERS)+        count = MAX_WORKERS;++    std::atomic<uint32_t> next{0};+    std::atomic<uint64_t> live_total{0};+    SweepCtx contexts[MAX_WORKERS];+    for (uint32_t i = 0; i < count; i++) {+        contexts[i].heap = this;+        contexts[i].next = &next;+        contexts[i].live_total = &live_total;+    }++    ThreadStart starts[MAX_WORKERS];+    OsThread threads[MAX_WORKERS];+    bool started[MAX_WORKERS] = {};+    for (uint32_t i = 1; i < count; i++) {+        starts[i].fn = &Heap::sweep_worker_entry;+        starts[i].arg = &contexts[i];+        started[i] = os_thread_start(threads[i], &starts[i]);     }++    sweep_worker(contexts[0]);++    for (uint32_t i = 1; i < count; i++)+        if (started[i])+            os_thread_join(threads[i]);      for (uint32_t i = 0; i < nlarge;) {         Header* h = static_cast<Header*>(large[i].base);@@ -870,7 +1153,7 @@         }     } -    live_lines_last = live_total;+    live_lines_last = live_total.load(std::memory_order_relaxed); }  void Heap::reset_mutator(Mutator* m) {@@ -909,8 +1192,8 @@     recyclable_head = NO_BLOCK;     spin_unlock(block_lock); -    mark_roots();-    sweep();+    parallel_mark();+    parallel_sweep();      gc_requested.store(false, std::memory_order_release); @@ -933,7 +1216,7 @@             enter_safepoint();             leave_safepoint();         }-        std::this_thread::yield();+        os_yield();     }      safepoint_request.store(1, std::memory_order_release);@@ -941,11 +1224,16 @@      for (;;) {         spin_lock(mutator_lock);-        uint32_t need = nmutators;+        uint32_t need = 0;+        for (uint32_t i = 0; i < nmutators; i++)+            if (!(std::atomic_ref<uint32_t>(mutators[i]->flags)+                      .load(std::memory_order_acquire) &+                  IN_NATIVE))+                need++;         spin_unlock(mutator_lock);         if (safepoint_arrived.load(std::memory_order_acquire) >= need)             break;-        std::this_thread::yield();+        os_yield();     }      collect_locked();@@ -1149,16 +1437,36 @@     spin_unlock(g_heap.root_lock); } -void haze_native_enter(void) {}-void haze_native_exit(void) {}+void haze_native_enter(void) {+    Mutator* m = g_heap.current();+    if (!m)+        return;+    std::atomic_ref<uint32_t>(m->flags).fetch_or(IN_NATIVE, std::memory_order_acq_rel);+}++void haze_native_exit(void) {+    Mutator* m = g_heap.current();+    if (!m)+        return;+    std::atomic_ref<uint32_t>(m->flags).fetch_and(~IN_NATIVE, std::memory_order_acq_rel);+    if (g_heap.safepoint_request.load(std::memory_order_acquire)) {+        g_heap.enter_safepoint();+        g_heap.leave_safepoint();+    }+}  void haze_pin(void* object) {-    if (!object || !g_heap.in_heap(object))+    if (!object || (!g_heap.in_heap(object) && !g_heap.is_large_object(object)))         return;     Header* h = static_cast<Header*>(object);-    h->flags |= PINNED;-    if (g_heap.npinned < MAX_PINNED)-        g_heap.pinned[g_heap.npinned++] = object;+    spin_lock(g_heap.pinned_lock);+    uint8_t flags = std::atomic_ref<uint8_t>(h->flags).load(std::memory_order_acquire);+    if (!(flags & PINNED)) {+        std::atomic_ref<uint8_t>(h->flags).fetch_or(PINNED, std::memory_order_acq_rel);+        if (g_heap.npinned < MAX_PINNED)+            g_heap.pinned[g_heap.npinned++] = object;+    }+    spin_unlock(g_heap.pinned_lock); }  void haze_unpin(void* object) {@@ -1165,13 +1473,15 @@     if (!object)         return;     Header* h = static_cast<Header*>(object);-    h->flags &= ~PINNED;+    spin_lock(g_heap.pinned_lock);+    std::atomic_ref<uint8_t>(h->flags).fetch_and(~PINNED, std::memory_order_acq_rel);     for (uint32_t i = 0; i < g_heap.npinned; i++) {         if (g_heap.pinned[i] == object) {             g_heap.pinned[i] = g_heap.pinned[--g_heap.npinned];-            return;+            break;         }     }+    spin_unlock(g_heap.pinned_lock); }  void haze_wb(void* object, void** slot, void* value) {@@ -1179,7 +1489,7 @@         return;     *slot = value;     Header* h = static_cast<Header*>(object);-    h->flags |= REMEMBERED;+    std::atomic_ref<uint8_t>(h->flags).fetch_or(REMEMBERED, std::memory_order_acq_rel); }  void haze_print_i32(int32_t value) {--- a/src/driver.cpp+++ b/src/driver.cpp@@ -515,6 +515,7 @@ #if !defined(_WIN32)         if (!opt.dynamic)             cmd.push_back("-static");+        cmd.push_back("-pthread"); #endif         cmd.push_back(asmpath.string());         cmd.push_back(runtime);--- a/tests/runtime_test.cpp+++ b/tests/runtime_test.cpp@@ -111,6 +111,40 @@         thread_slot[0] = nullptr;         haze_gc_collect();         check(!haze_gc_object_alive(detached), "object collected after other mutator detach");+    }++    /* M5: pinned object in a native region survives while the thread is parked. */+    {+        std::atomic<bool> ready{false};+        std::atomic<bool> release{false};+        void* pinned_object = nullptr;++        std::thread worker([&] {+            haze_thread_attach();+            pinned_object = haze_alloc_slow(64, 0);+            haze_pin(pinned_object);+            haze_native_enter();+            ready.store(true, std::memory_order_release);+            while (!release.load(std::memory_order_acquire))+                std::this_thread::yield();+            haze_native_exit();+            haze_unpin(pinned_object);+            haze_thread_detach();+        });++        while (!ready.load(std::memory_order_acquire))+            std::this_thread::yield();++        haze_gc_collect();+        haze_gc_verify();+        check(haze_gc_object_alive(pinned_object), "pinned native object survived collection");++        release.store(true, std::memory_order_release);+        worker.join();+        void* detached = pinned_object;+        pinned_object = nullptr;+        haze_gc_collect();+        check(!haze_gc_object_alive(detached), "unpinned native object reclaimed");     }      /* Epoch wraparound smoke test. */