Function downgradeLock [src]

Assumes the file is already locked in exclusive mode. Atomically modifies the lock to be in shared mode, without releasing it. TODO: integrate with async I/O

Prototype

pub fn downgradeLock(file: File) LockError!void

Parameters

file: File

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 downgradeLock(file: File) LockError!void { if (is_windows) { // On Windows it works like a semaphore + exclusivity flag. To implement this // function, we first obtain another lock in shared mode. This changes the // exclusivity flag, but increments the semaphore to 2. So we follow up with // an NtUnlockFile which decrements the semaphore but does not modify the // exclusivity flag. var io_status_block: windows.IO_STATUS_BLOCK = undefined; windows.LockFile( file.handle, null, null, null, &io_status_block, &range_off, &range_len, null, windows.TRUE, // non-blocking=true windows.FALSE, // exclusive=false ) catch |err| switch (err) { error.WouldBlock => unreachable, // File was not locked in exclusive mode. else => |e| return e, }; return windows.UnlockFile( file.handle, &io_status_block, &range_off, &range_len, null, ) catch |err| switch (err) { error.RangeNotLocked => unreachable, // File was not locked. error.Unexpected => unreachable, // Resource deallocation must succeed. }; } else { return posix.flock(file.handle, posix.LOCK.SH | posix.LOCK.NB) catch |err| switch (err) { error.WouldBlock => unreachable, // File was not locked in exclusive mode. else => |e| return e, }; } }