filemutex_windows.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Copyright 2013 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package filemutex
  5. import (
  6. "golang.org/x/sys/windows"
  7. )
  8. // FileMutex is similar to sync.RWMutex, but also synchronizes across processes.
  9. // This implementation is based on flock syscall.
  10. type FileMutex struct {
  11. fd windows.Handle
  12. }
  13. func New(filename string) (*FileMutex, error) {
  14. fd, err := windows.CreateFile(&(windows.StringToUTF16(filename)[0]), windows.GENERIC_READ|windows.GENERIC_WRITE,
  15. windows.FILE_SHARE_READ|windows.FILE_SHARE_WRITE, nil, windows.OPEN_ALWAYS, windows.FILE_ATTRIBUTE_NORMAL, 0)
  16. if err != nil {
  17. return nil, err
  18. }
  19. return &FileMutex{fd: fd}, nil
  20. }
  21. func (m *FileMutex) TryLock() error {
  22. if err := windows.LockFileEx(m.fd, windows.LOCKFILE_FAIL_IMMEDIATELY|windows.LOCKFILE_EXCLUSIVE_LOCK, 0, 1, 0, &windows.Overlapped{}); err != nil {
  23. if errno, ok := err.(windows.Errno); ok {
  24. if errno == windows.ERROR_LOCK_VIOLATION {
  25. return AlreadyLocked
  26. }
  27. }
  28. return err
  29. }
  30. return nil
  31. }
  32. func (m *FileMutex) Lock() error {
  33. return windows.LockFileEx(m.fd, windows.LOCKFILE_EXCLUSIVE_LOCK, 0, 1, 0, &windows.Overlapped{})
  34. }
  35. func (m *FileMutex) Unlock() error {
  36. return windows.UnlockFileEx(m.fd, 0, 1, 0, &windows.Overlapped{})
  37. }
  38. func (m *FileMutex) RLock() error {
  39. return windows.LockFileEx(m.fd, 0, 0, 1, 0, &windows.Overlapped{})
  40. }
  41. func (m *FileMutex) RUnlock() error {
  42. return windows.UnlockFileEx(m.fd, 0, 1, 0, &windows.Overlapped{})
  43. }
  44. // Close unlocks the lock and closes the underlying file descriptor.
  45. func (m *FileMutex) Close() error {
  46. // See comment section of https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-lockfileex
  47. // It's recommended to unlock a file explicitly before closing in order to
  48. // avoid delays, but all locks are definitly unlocked when closing a file.
  49. // So any unlocking error can be ignored.
  50. _ = windows.UnlockFileEx(m.fd, 0, 1, 0, &windows.Overlapped{})
  51. return windows.Close(m.fd)
  52. }