zig icon indicating copy to clipboard operation
zig copied to clipboard

Support _Atomic in translate-c

Open iacore opened this issue 2 years ago • 4 comments

_Atomic data types are available in C11 that ensures atomic read and load. I'm not sure how to implement this in Zig.

Zig Version

0.10.0-dev.1427+ad5770eba

Steps to Reproduce

Save the text below as test.c:

_Atomic(int) hi;

Then run zig translate-c test.c.

Expected Behavior

No error

Actual Behavior

> zig translate-c test.c | grep hi
pub const hi = @compileError("unable to resolve variable type"); // test.c:1:14

iacore avatar Apr 11 '22 21:04 iacore

https://ziglang.org/documentation/master/#atomicLoad https://ziglang.org/documentation/master/#atomicStore

nektro avatar Apr 12 '22 05:04 nektro

The problem is that _Atomic is a type attribute, where @atomicLoad is usage of a term. If _Atomic(uint8_t) is translated to u8, then assignments to atomic variables need to use @atomicStore.

This is fine on x86 and some CPU architectures where load/store of register-sized integer is always atomic, but what problem will C non-compilance cause down the road? Should we even care about this? Maybe generate compile-time warnings is fine.

iacore avatar Apr 18 '22 17:04 iacore

Partially related https://github.com/ziglang/zig/issues/4248

daurnimator avatar Apr 22 '22 14:04 daurnimator

Probably related #5101

lorislibralato avatar Dec 23 '23 16:12 lorislibralato

This affects Nuitka (the Python compiler) and Python 3.11 or higher. We as a project want to use zig as a backend C compiler, with long term hope of being able to use it exclusively. I am commenting here more follow. Writing a compiler myself, I know how hard this issue can be to solve for you, seeing a 0.15 milestone probably means, this will take a while.

Nuitka requires C11 more or less with modern Python.

kayhayen avatar Mar 04 '24 13:03 kayhayen