Skip to main content

sync_scope

Function sync_scope 

Source
pub fn sync_scope<P, B, F, R>(f: F) -> R
where P: AllocPolicy, B: HasSegmentPool + LocalAllocatorSelector<B>, F: for<'brand> FnOnce(Heap<'brand, P, B>, SyncRegionToken<'brand>) -> R,
Expand description

Executes a closure with a fresh, thread-portable branded heap and token.

The heap remains one exclusive allocator owner. A caller may move crate::BrandedCell handles and SyncRegionToken into scoped workers, then return them to the owner before reclaiming their blocks through the heap. Payload access is mediated by Melinoe permits and does not allocate or synchronize at runtime.

ยงExamples

use mnemosyne_core::StandardPolicy;
use mnemosyne_backend::MemoryBackendWrapper;
use mnemosyne_heap::{sync_scope, BrandedCell};

sync_scope::<StandardPolicy, MemoryBackendWrapper, _, _>(|heap, mut token| {
    let block = heap.alloc_init(&token, 41).expect("allocation failed");
    // SAFETY: `alloc_init` returned an initialized block owned by `heap`.
    let cell = unsafe { BrandedCell::from_block(block) };
    let (cell, mut token) = std::thread::scope(|scope| {
        scope
            .spawn(move || {
                *cell.borrow_mut(&mut token) += 1;
                (cell, token)
            })
            .join()
            .expect("scoped worker panicked")
    });
    assert_eq!(*cell.borrow(&token), 42);
    heap.free(&mut token, unsafe { cell.into_block() });
});