| author | |
| committer | |
| log | 44e5319107f5d5f7b5bd4cc9917b038196999857 |
| tree | 7adc5175602b28d8e5245aad738a55daa6795600 |
| parent | 712055f32444d8ba74650591d82524130be7dfd1 |
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 |
| 1740 | 1740 | /// Mutex is a synchronization primitive which enforces atomic access to a |
| 1741 | 1741 | /// shared region of code known as the "critical section". |
| 1742 | 1742 | /// |
| 1743 | /// Mutex is an extern struct so that it may be used as a field inside another | |
| 1744 | /// extern struct. | |
| 1745 | pub 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. | |
| 1747 | pub const Mutex = struct { | |
| 1746 | 1748 | state: std.atomic.Value(State), |
| 1747 | 1749 | |
| 1748 | 1750 | pub const init: Mutex = .{ .state = .init(.unlocked) }; |