authorgravatar for PecoraInPannaCotta@gmail.comGiuseppe Cesarano <PecoraInPannaCotta@gmail.com> 2026-10-10 19:11:35+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-10-10 19:11:35+02:00
log44e5319107f5d5f7b5bd4cc9917b038196999857
tree7adc5175602b28d8e5245aad738a55daa6795600
parent712055f32444d8ba74650591d82524130be7dfd1

Remove extern from Io.Mutex (#37081)

An `Io.Mutex` is bound to the Io implementation used to lock and unlock it: having it cross Io implementations could already be an abuse of the API. C code is even further removed, since it has no `Io` at all and could never meaningfully interact with an `Io.Mutex`, so making it `extern` doesn't buy us anything. The only thing we actually gain is less boilerplate when the user needs to protect a struct that is also used from C, where the C side doesn't need the synchronization. I would argue that's not a strong enough reason to keep it `extern`, since the same behavior can be achieved with composition. Being `extern` also conflicts with adding safety features to the implementation: their size would change with the compilation configuration, which defeats the purpose of `extern`. Reviewed-on: https://codeberg.org/ziglang/zig/pulls/37081 Reviewed-by: Andrew Kelley <andrew@ziglang.org>

1 files changed, 5 insertions(+), 3 deletions(-)

lib/std/Io.zig+5-3
......@@ -1740,9 +1740,11 @@ pub fn futexWake(io: Io, comptime T: type, ptr: *align(@alignOf(u32)) const T, m
17401740/// Mutex is a synchronization primitive which enforces atomic access to a
17411741/// shared region of code known as the "critical section".
17421742///
1743/// Mutex is an extern struct so that it may be used as a field inside another
1744/// extern struct.
1745pub const Mutex = extern struct {
1743/// Mutex is not an extern struct because locking and unlocking go through the
1744/// futex calls of `Io`, which are not required to work across processes or
1745/// compilation units. Code that needs a fixed layout can build its own lock on
1746/// a `u32` with the same futex calls.
1747pub const Mutex = struct {
17461748 state: std.atomic.Value(State),
17471749
17481750 pub const init: Mutex = .{ .state = .init(.unlocked) };