Function lock [src]

Blocks when an incompatible lock is held by another process. A process may hold only one type of lock (shared or exclusive) on a file. When a process terminates in any way, the lock is released. Assumes the file is unlocked. TODO: integrate with async I/O

Prototype

pub fn lock(file: File, l: Lock) LockError!void

Parameters

file: Filel: Lock

Possible Errors

FileLocksNotSupported
SystemResources
Unexpected UnexpectedError

The Operating System returned an undocumented error code.

This error is in theory not possible, but it would be better to handle this error than to invoke undefined behavior.

When this error code is observed, it usually means the Zig Standard Library needs a small patch to add the error code to the error set for the respective function.

Source

pub fn lock(file: File, l: Lock) LockError!void { if (is_windows) { var io_status_block: windows.IO_STATUS_BLOCK = undefined; const exclusive = switch (l) { .none => return, .shared => false, .exclusive => true, }; return windows.LockFile( file.handle, null, null, null, &io_status_block, &range_off, &range_len, null, windows.FALSE, // non-blocking=false @intFromBool(exclusive), ) catch |err| switch (err) { error.WouldBlock => unreachable, // non-blocking=false else => |e| return e, }; } else { return posix.flock(file.handle, switch (l) { .none => posix.LOCK.UN, .shared => posix.LOCK.SH, .exclusive => posix.LOCK.EX, }) catch |err| switch (err) { error.WouldBlock => unreachable, // non-blocking=false else => |e| return e, }; } }