Zig 0.17.0-dev (Split by item) This is an example of documentation generated by
ZigDoc , an alternative to Zig's built-in
Auto Doc feature. See also
examples in other modes/formats . The project being documented here (as the example) is the Zig standard library.
std › Io › Threaded › ParkingMutex › lock Private Function lock Doc Source Private Function std.Io.Threaded.ParkingMutex.lock
fn lock (m : *ParkingMutex ) void Params File Io/Threaded.zig: 17912
17912
17913
17914
17915
17916
17917
17918
17919
17920
17921
17922
17923
17924
17925
17926
17927
17928
17929
17930
17931
17932
17933
17934
17935
17936
17937
17938
17939
17940
17941
17942
17943
17944
17945
17946
17947 fn lock (m : *ParkingMutex ) void {
state : switch (State .unlocked ) { // assume 'unlocked' to optimize for uncontended case
.unlocked => continue :state m .state .cmpxchgWeak (
.unlocked ,
.locked_once ,
.acquire , // acquire lock
.monotonic ,
) orelse {
@branchHint (.likely );
return ;
},
.locked_once , _ => |last_state | {
const old_waiter = last_state .waiter ();
const self_tid = getParkTid ();
var waiter : Waiter = .{
.next = old_waiter ,
.unpark_flag = unpark_flag_init ,
.tid = self_tid ,
};
if (m .state .cmpxchgWeak (
.fromWaiter (old_waiter ),
.fromWaiter (&waiter ),
.release , // release `waiter`
.monotonic ,
)) |new_state | {
continue :state new_state ;
}
// We're now in the list of waiters---park until we're given the lock.
park (.none , m , if (need_unpark_flag ) &waiter .unpark_flag ) catch |err | switch (err ) {
error .Timeout => unreachable ,
};
return ;
},
}
}