Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wishlist: Implement atomics #436

Open
chrysn opened this issue Jun 8, 2022 · 0 comments
Open

wishlist: Implement atomics #436

chrysn opened this issue Jun 8, 2022 · 0 comments
Labels
enhancement New feature or request

Comments

@chrysn
Copy link
Contributor

chrysn commented Jun 8, 2022

I Would Be Great If C2Rust could just translate C11 atomics (extern _Atomic int foo; foo = 4;).

While personally I prefer to go through the explicitly ordering functions (atomic_fetch_and_explicity etc), atomic types are often part of existing libraries, especially stdatomic.h.

I've started toying with these at https://github.com/chrysn-pull-requests/c2rust/tree/atomics (building on the latest state of #302), and found a few aspects that I couldn't immediately solve:

  • Atomics behave a lot like qualifiers, but LLVM doesn't seem to store them that way -- and adding a bit to the qualifiers set doesn't seem to be planned there (as the qualifiers are mixed in with the identifier). Thus they probably need to go through a new CTypeKind, which the referenced branch contains a commit to introduce.
  • Eventually (didn't get to that point yet), we'd end up with something that looks like Atomic<short>. It's bad enough that it's hard to tell which sizes the target platform actually has atomics support for (not that we could do anything about it here), but it's worse that the information about the actual size (and thus concrete type, such as AtomicU16) to use is unavailable without knowing the target. I think the best option will be to use a trait (Atomic<T>) that relies on the wrapped elements being type aliases for any actually atomicable type, and let things err out at compilation time (when the target is known) otherwise. We might use the atomig crate or roll our own (but I don't see why we should).

I think that the primary goal here should be to support simple numeric atomics in their C11 form; _Atomic struct foo is something I wouldn't put much effort in (but of course it's nice if it just happens to work).

@kkysen kkysen added the enhancement New feature or request label Jun 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants