FerroUI/ docs

Search guides, controls, Rust APIs, and XAML.

    GitHub ↗

    ferro_thread_local

    module · ferroui-system · v0.5.0

    module ferro_thread_local
    

    Thread-local variables that do not take a key of the C library each.

    ferro_thread_local! is declared and used exactly as std::thread_local! is, and on every target with thread-local statics it is std::thread_local!: the declaration is handed on unchanged, so nothing differs in behaviour or cost.

    The Android targets of Rust have no thread-local statics: the standard library keeps each thread_local! variable under a key of its own (pthread_key_create), and bionic has 128 keys for a whole process. The class model of the port keeps its definitions per thread, a cell per property, routed event and class, which is far more than that (docs/porting/android-platform.md, section 5.1). On Android the macro therefore declares a slot of one table per thread instead (LocalKey):

    • Every declaration takes a number once, from a static counter, the first time it is used on any thread. The number is its index in the table.
    • The table of a thread is a vector of boxed values, created with the first use on the thread and found through one key of the C library for the whole process. A value is created by its initialiser the first time it is used on the thread, as with thread_local!.
    • No borrow of the table is held while an initialiser or a destructor runs, so an initialiser may use other variables (and its own: as with thread_local!, the value the outer initialiser returns then replaces the one the inner one stored). A value is boxed, so the references a with hands out stay valid while the table grows.
    • When the thread ends, the values whose type has a destructor are destroyed in the reverse order of their creation; a value created while that runs is destroyed too. A destroyed variable cannot be used again on the thread: LocalKey::try_with returns an AccessError and LocalKey::with panics with the message of the standard library. A variable whose type has no destructor stays usable until the last destructor has run, as a variable of such a type does with thread_local! on targets with thread-local statics, and its memory is freed last.

    The table is compiled on hosts as well, for its tests, and with --cfg ferro_tls_table every ferro_thread_local! of the build uses it there (on Linux and macOS), so that the test suites of the framework can run against it without a device.

    View source

    Contents

    Availability

    Extracted for aarch64-apple-darwin. Cargo features are listed in the crate index.