-
Notifications
You must be signed in to change notification settings - Fork 2
/
lock_unix.go
33 lines (26 loc) · 1.04 KB
/
lock_unix.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
//go:build darwin || dragonfly || freebsd || linux || netbsd || openbsd
package file
import (
"os"
"syscall"
)
// LockSH places a shared lock on the file. If the file is already locked, waits until the file is released.
func LockSH(fp *os.File) error {
return syscall.Flock(int(fp.Fd()), syscall.LOCK_SH)
}
// LockEX places an exclusive lock on the file. If the file is already locked, waits until the file is released.
func LockEX(fp *os.File) error {
return syscall.Flock(int(fp.Fd()), syscall.LOCK_EX)
}
// TryLockSH places a shared lock on the file. If the file is already locked, returns an error immediately.
func TryLockSH(fp *os.File) error {
return syscall.Flock(int(fp.Fd()), syscall.LOCK_SH|syscall.LOCK_NB)
}
// TryLockEX places an exclusive lock on the file. If the file is already locked, returns an error immediately.
func TryLockEX(fp *os.File) error {
return syscall.Flock(int(fp.Fd()), syscall.LOCK_EX|syscall.LOCK_NB)
}
// Unlock the file.
func Unlock(fp *os.File) error {
return syscall.Flock(int(fp.Fd()), syscall.LOCK_UN)
}