authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-06-27 22:32:52-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-06-29 14:25:04-07:00
log6ba6b98b72efcc0468ef7c3fc0cc6499e5d38ea5
tree035757190a1f13ac64a21668315ade21fff5c12c
parent2c385e58f96ea080bd6c732de422f84c6c38c2a2

std add fs.File.setLock

Windows implementation is still missing.

1 files changed, 20 insertions(+), 0 deletions(-)

lib/std/fs/file.zig+20
...@@ -829,4 +829,24 @@ pub const File = struct {...@@ -829,4 +829,24 @@ pub const File = struct {
829 pub fn seekableStream(file: File) SeekableStream {829 pub fn seekableStream(file: File) SeekableStream {
830 return .{ .context = file };830 return .{ .context = file };
831 }831 }
832
833 pub const SetLockError = os.FlockError;
834
835 /// Blocks when an incompatible lock is held by another process.
836 /// `non_blocking` may be used to make a non-blocking request,
837 /// causing this function to possibly return `error.WouldBlock`.
838 /// A process may hold only one type of lock (shared or exclusive) on
839 /// a file. When a process terminates in any way, the lock is released.
840 /// TODO: integrate with async I/O
841 pub fn setLock(file: File, lock: Lock, non_blocking: bool) SetLockError!void {
842 if (is_windows) {
843 @compileError("TODO implement fs.File.setLock for Windows");
844 }
845 const non_blocking_flag = if (non_blocking) os.LOCK_NB else @as(i32, 0);
846 try os.flock(file.handle, switch (lock) {
847 .None => os.LOCK_UN,
848 .Shared => os.LOCK_SH | non_blocking_flag,
849 .Exclusive => os.LOCK_EX | non_blocking_flag,
850 });
851 }
832};852};