diff options
| author | Paul Buetow <paul@buetow.org> | 2025-09-19 22:56:10 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2025-09-19 22:56:10 +0300 |
| commit | 1065d7a991d3b5c103bcc986a84867db28cb4720 (patch) | |
| tree | 00824c2b7b7b3016f235205bed9483b638340de6 /internal/stats | |
| parent | 6feda08653a80a7609df2e8b80e98ede15f86a61 (diff) | |
morev0.11.6
Diffstat (limited to 'internal/stats')
| -rw-r--r-- | internal/stats/lock_posix.go | 23 | ||||
| -rw-r--r-- | internal/stats/lock_windows.go | 24 |
2 files changed, 47 insertions, 0 deletions
diff --git a/internal/stats/lock_posix.go b/internal/stats/lock_posix.go new file mode 100644 index 0000000..2c41d31 --- /dev/null +++ b/internal/stats/lock_posix.go @@ -0,0 +1,23 @@ +//go:build !windows + +package stats + +import ( + "errors" + + "golang.org/x/sys/unix" +) + +func tryLockFile(fd uintptr) error { + if err := unix.Flock(int(fd), unix.LOCK_EX|unix.LOCK_NB); err != nil { + if errors.Is(err, unix.EWOULDBLOCK) { + return errLockWouldBlock + } + return err + } + return nil +} + +func unlockFile(fd uintptr) error { + return unix.Flock(int(fd), unix.LOCK_UN) +} diff --git a/internal/stats/lock_windows.go b/internal/stats/lock_windows.go new file mode 100644 index 0000000..2ec5e90 --- /dev/null +++ b/internal/stats/lock_windows.go @@ -0,0 +1,24 @@ +//go:build windows + +package stats + +import ( + "golang.org/x/sys/windows" +) + +func tryLockFile(fd uintptr) error { + var ol windows.Overlapped + err := windows.LockFileEx(windows.Handle(fd), windows.LOCKFILE_EXCLUSIVE_LOCK|windows.LOCKFILE_FAIL_IMMEDIATELY, 0, 1, 0, &ol) + if err == nil { + return nil + } + if err == windows.ERROR_LOCK_VIOLATION { + return errLockWouldBlock + } + return err +} + +func unlockFile(fd uintptr) error { + var ol windows.Overlapped + return windows.UnlockFileEx(windows.Handle(fd), 0, 1, 0, &ol) +} |
