# ferro_thread_local

Version: 0.5.0
Framework commit: d503df2befa756e681fcdc6250e9552dc7388bea
Canonical URL: /docs/0.5.0/api/ferroui-system/ferro_thread_local/

**module** · [`ferroui-system`](/docs/0.5.0/api/ferroui-system/) · v0.5.0

```rust
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](https://github.com/wieslawsoltes/FerroUI/blob/d503df2befa756e681fcdc6250e9552dc7388bea/src/FerroUI.System/ferro_thread_local.rs#L1)

## Contents

- [`AccessError`](/docs/0.5.0/api/ferroui-system/ferro_thread_local/use-accesserror/) — use
- [`LocalKey`](/docs/0.5.0/api/ferroui-system/ferro_thread_local/use-localkey/) — use

## Availability

Extracted for `aarch64-apple-darwin`. Cargo features are listed in the [crate index](/docs/0.5.0/api/).
