Hacker News new | ask | show | jobs
by paulddraper 1065 days ago
Yeah, I've only seen that term used in Rust, though the concept is very common in C++.

They explain it there: "No RTTI, memory allocation, nor exceptions."

Basically, nothing that has significant "runtime" cost....dynamic_cast, new/malloc, throwing exceptions.

3 comments

This is generally known as "freestanding" in the C/C++ world -- https://en.cppreference.com/w/cpp/freestanding
In Rust there's actually deliberately a core library, and so no_std actually makes sense - you get the core library, not the larger std which re-exports all of core plus a lot more. There's quite a lot of stuff in that core library, just nothing that requires an operating system or an allocator. So Rust's Mutex isn't available (how can we provide a mutual exclusion mechanism on the bare hardware?) but Rust's Option<(core::net::IpAddr, core::time::Duration) is available everywhere, it's either None or a pair of an IP (v4 or v6) address and a duration, perhaps some sort of address lease because maybe we're an embedded device which has, or gives out, address leases.

In C++ this makes less sense because their standard library does have a defined "freestanding" subset but it doesn't make a very coherent whole, it's roughly the bits that seemed obviously to just not need any other components to work. It changes from version to version. And this stx library replaces what you might think of as core ideas, like an optional type, so you don't keep the shared vocabulary benefit.

[Edited to correct "standalone" to "freestanding"]

> how can we provide a mutual exclusion mechanism on the bare hardware?

This is a bit misleading. It's easier to provide mutual exclusion on bare hardware than in an OS; just do a spinlock, except with modern ISAs the core doesn't even spin.

Presumably core doesn't offer this because it's a massive footgun for devs new to multithreading who would not understand why this is unacceptable to use in an OS-hosted process.

> with modern ISAs the core doesn't even spin.

Although tricks like PAUSE are much cheaper than a naive spinlock, they are still spinning as I understand it, just not as frantically because that's pointless and wasteful.

It's even simpler than that, PAUSE just prevents the CPU from trying to speculatively execute across iterations.
I'm not familiar with a pause instruction in a modern isa. Sounds like x86 crap.

I'm talking about wfi, wfe, and friends.

IMO the typical use case is for portability to platforms that don't have those features, or implementing a lower-layer: bootloader/OS kernel/etc. Regardless of the runtime cost - you don't have a C/C++ library to rely on (or you're implementing one).

I'd always assumed rust may have taken this from C compiler arguments like -nostdlib / -nostdinc (and corresponding C++ ones -nostdlib++, -nostdinc++).