ring icon indicating copy to clipboard operation
ring copied to clipboard

Avoid using `spin` if `OnceCell` is available

Open briansmith opened this issue 3 years ago • 1 comments

The vast majority of the time we are not building for a no_std environment when targeting x86, x86-64, ARM, or AAarch64. If we're not targeting a no_std environment then we can and should use OnceCell and avoid using spin. We should only use spin as a last resort.

Perhaps we should assume that if there is an OS, then OnceCell is available, and if there isn't an OS then assume it isn't available.

briansmith avatar Jul 13 '21 23:07 briansmith

How about using the newly (1.60) stabilized target_has_atomic?

#[cfg(all(std, target_has_atomic = "ptr"))]

From this old RFC:

After a quick survey of the LLVM and Clang code, architectures can be classified into 3 categories:

  1. The architecture does not support any form of atomics (mainly microcontroller architectures).
  2. The architecture supports all atomic operations for integers from i8 to iN (where N is the architecture >word/pointer size).
  3. The architecture supports all atomic operations for integers from i8 to i(N*2).

OnceCell appears to use atomics: https://github.com/matklad/once_cell/blob/090caea92f21ecba71b743c1c636f3fe9d14b598/src/imp_pl.rs#L5

and

https://github.com/rust-lang/rfcs/pull/2788/commits/07c3c59a969d32b3b30075122887f3de8e29f00c#diff-fefae16108333b139ec1fa67533a9e0fc44fb05f01a1f92c19f09bb6d5168a23R375

LegNeato avatar Apr 20 '22 23:04 LegNeato