4 ms·
Since this release is focused on stabilizing libcore for embedded use, it seems a good time to ask a question that's been on my mind. C11 (and C++11) defined a
by haberman 11y ago
Since this release is focused on stabilizing libcore for embedded use, it seems a good time to ask a question that's been on my mind.
C11 (and C++11) defined a memory model and atomic operations for shared-state lock-free concurrency. But that model and the atomic operations aren't being used by Linux, because they didn't match up with the semantics of the operations that Linux uses. (See https://lwn.net/Articles/586838/ https://lwn.net/Articles/586838/ and http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/p0124r0.html http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/p012...).
I'm curious what Rust says about this. Does Rust have a memory model like C11/C++11? I'm curious whether Rust (and C11/C++11 for that matter) will evolve to have primitives like what the Linux kernel currently defines and uses.
- steveklabnik 11y agoWe do not have a formal spec yet, and so do not technically have a memory model like C or C++ do. However, since we're built on LLVM, we do have some semantics that are pretty much based on it. Our atomics stuff is pretty much LLVM's. A lot of the work going into forming said models is trying to determine how much of LLVM we've let pollute into the language in the meantime and where it's feasible to diverge. https://github.com/rust-lang/rfcs/issues/1447 https://github.com/rust-lang/rfcs/issues/1447 is a good thread if you care about this topic.
- johnbender 11y agohttp://plv.mpi-sws.org/rustbelt/ http://plv.mpi-sws.org/rustbelt/ As part of defining the Rust semantics they will certainly tackle the question of the memory model. Dreyer and company have a track record of providing semantics and reasoning principles for weak memory models (like C11). You might have to wait a while for a rigorous semantics though.
- hellofunk 11y agoI'm curious if what you mean is that C++11 has a standard spec for the memory model you describe, but if Linux doesn't use it, then Linux does not actually adhere to the C++11 standard? Does that mean that Linux C++11 compilers are non-standard in general?
- Manishearth 11y agoI don't think there are any (modern) C++ compilers out there which follow the standard 100% and perfectly. ICBW.
- hellofunk 11y agoI think you're mistaken: http://clang.llvm.org/cxx_status.html#cxx11 http://clang.llvm.org/cxx_status.html#cxx11
- Manishearth 11y agoThat's a list of features. My point was that there are some slight variations induced by optimizations or otherwise which are against the spec.
- hellofunk 11y agoI think you are confusing specification with implementation. The C++ standard does not outline how a compiler implements a feature, just how it should behave to match the standard. There are several compilers that 100% adhere to the full standard, including the guarantees specified by the standard in the form of memory and time complexity (optimization).
- Manishearth 11y agoOh, I know that specification != implementation, but IIRC both clang and GCC have certain behaviour which do not conform to the standard. But I can't find these right now, so I'll drop it here :)
- haberman 11y agoLet me clarify first that when I say "Linux" in this post, I'm specifically referring to the Linux kernel, not user-space software that runs on Linux. The Linux kernel is written in C, but uses lots of GCC-specific features and inline assembly to address things that aren't covered by the C standard. Among these, the Linux kernel has its own set of atomic types and memory barriers. It documents them here: https://www.kernel.org/doc/Documentation/memory-barriers.txt https://www.kernel.org/doc/Documentation/memory-barriers.txt So even if Linux is being compiled on a conforming C11 compiler (the Linux kernel is written in C, not C++), it uses its own atomic primitives instead of the ones provided by the language. Hope that helps.