Skip to content

Commit b78c416

Browse files
committed
Mention that spinlocks are deliberately omitted
1 parent 19f6961 commit b78c416

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

text/0000-standard-lazy-types.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ The thread-safe variant is implemented similarly to `std::sync::Once`.
249249
Crucially, it has support for blocking: if many threads call `get_or_init` concurrently, only one will be able to execute the closure, while all other threads will block.
250250
For this reason, most of `std::sync::OnceCell` API can not be provided in `core`.
251251
In the `sync` case, reliably panicking on re-entrant initialization is not trivial.
252-
For this reason, the implementaion would simply deadlock, with a note that a deadlock might be elevated to panic in the future.
252+
For this reason, the implementation would simply deadlock, with a note that a deadlock might be elevated to a panic in the future.
253253

254254
# Drawbacks
255255
[drawbacks]: #drawbacks
@@ -376,10 +376,14 @@ impl<T> OnceCell<T> {
376376
}
377377
```
378378

379-
It is possible because, while `OnceCell` needs block for full API, its internal state can be implemented as a single `AtomicUsize`, so the `core` part does not need to know about blocking.
379+
It is possible because, while `OnceCell` needs blocking for full API, its internal state can be implemented as a single `AtomicUsize`, so the `core` part does not need to know about blocking.
380380
It is unclear if this API would be significantly useful.
381381
In particular, the guarantees of non-blocking `set` are pretty weak, and are not enough to implement the `Lazy` wrapper.
382382

383+
While it is possible to implement blocking in `#[no_std]` via a spin lock, we explicitly choose not to do so.
384+
Spin locks are a sharp tool, which should only be used in specific circumstances (namely, when you have full control over thread scheduling).
385+
`#[no_std]` code might end up in user space applications with preemptive scheduling, where unbounded spin locks are inappropriate.
386+
383387
## Poisoning
384388

385389
As a cell can be empty or fully initialized, the proposed API does not use poisoning.

0 commit comments

Comments
 (0)