Skip to main content

Module tls

Module tls 

Source
Expand description

TLS-backed convenience API for GC protection.

Provides an optional convenience layer that maintains a thread-local stack of scope pointers, allowing tls::protect(x) without passing &ProtectScope explicitly.

Explicit &ProtectScope is recommended for most use cases. It’s simpler, clearer about lifetimes, and doesn’t rely on runtime state. Use this TLS convenience only when threading scope references through deep call stacks would be excessively verbose.

§Example

use miniextendr_api::gc_protect::tls;

unsafe fn deep_helper(x: SEXP) -> SEXP {
    let y = tls::protect(allocate_something());
    combine(x, y.get())
}

unsafe fn call_body(x: SEXP) -> SEXP {
    tls::with_protect_scope(|| {
        let x = tls::protect(x);
        deep_helper(x.get())
    })
}

Structs§

TlsRoot
A rooted SEXP from TLS protection.
TlsScopeGuard 🔒
Execute a closure with a new protection scope as the current TLS scope.

Constants§

SCOPE_STACK 🔒
Stack of active protection scopes on this thread.

Functions§

current_count
Get the current scope’s protection count.
has_active_scope
Check if there is an active TLS scope.
protect
Protect a value using the current TLS scope.
protect_raw
Protect a value, returning the raw SEXP.
scope_depth
Get the nesting depth of TLS scopes.
with_protect_scope
Execute a closure with a protect scope that is accessible via TLS.