本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

142 lines
3.7KB

  1. package bbolt
  2. import (
  3. "fmt"
  4. "os"
  5. "syscall"
  6. "time"
  7. "unsafe"
  8. )
  9. // LockFileEx code derived from golang build filemutex_windows.go @ v1.5.1
  10. var (
  11. modkernel32 = syscall.NewLazyDLL("kernel32.dll")
  12. procLockFileEx = modkernel32.NewProc("LockFileEx")
  13. procUnlockFileEx = modkernel32.NewProc("UnlockFileEx")
  14. )
  15. const (
  16. // see https://msdn.microsoft.com/en-us/library/windows/desktop/aa365203(v=vs.85).aspx
  17. flagLockExclusive = 2
  18. flagLockFailImmediately = 1
  19. // see https://msdn.microsoft.com/en-us/library/windows/desktop/ms681382(v=vs.85).aspx
  20. errLockViolation syscall.Errno = 0x21
  21. )
  22. func lockFileEx(h syscall.Handle, flags, reserved, locklow, lockhigh uint32, ol *syscall.Overlapped) (err error) {
  23. r, _, err := procLockFileEx.Call(uintptr(h), uintptr(flags), uintptr(reserved), uintptr(locklow), uintptr(lockhigh), uintptr(unsafe.Pointer(ol)))
  24. if r == 0 {
  25. return err
  26. }
  27. return nil
  28. }
  29. func unlockFileEx(h syscall.Handle, reserved, locklow, lockhigh uint32, ol *syscall.Overlapped) (err error) {
  30. r, _, err := procUnlockFileEx.Call(uintptr(h), uintptr(reserved), uintptr(locklow), uintptr(lockhigh), uintptr(unsafe.Pointer(ol)), 0)
  31. if r == 0 {
  32. return err
  33. }
  34. return nil
  35. }
  36. // fdatasync flushes written data to a file descriptor.
  37. func fdatasync(db *DB) error {
  38. return db.file.Sync()
  39. }
  40. // flock acquires an advisory lock on a file descriptor.
  41. func flock(db *DB, exclusive bool, timeout time.Duration) error {
  42. var t time.Time
  43. if timeout != 0 {
  44. t = time.Now()
  45. }
  46. var flag uint32 = flagLockFailImmediately
  47. if exclusive {
  48. flag |= flagLockExclusive
  49. }
  50. for {
  51. // Fix for https://github.com/etcd-io/bbolt/issues/121. Use byte-range
  52. // -1..0 as the lock on the database file.
  53. var m1 uint32 = (1 << 32) - 1 // -1 in a uint32
  54. err := lockFileEx(syscall.Handle(db.file.Fd()), flag, 0, 1, 0, &syscall.Overlapped{
  55. Offset: m1,
  56. OffsetHigh: m1,
  57. })
  58. if err == nil {
  59. return nil
  60. } else if err != errLockViolation {
  61. return err
  62. }
  63. // If we timed oumercit then return an error.
  64. if timeout != 0 && time.Since(t) > timeout-flockRetryTimeout {
  65. return ErrTimeout
  66. }
  67. // Wait for a bit and try again.
  68. time.Sleep(flockRetryTimeout)
  69. }
  70. }
  71. // funlock releases an advisory lock on a file descriptor.
  72. func funlock(db *DB) error {
  73. var m1 uint32 = (1 << 32) - 1 // -1 in a uint32
  74. err := unlockFileEx(syscall.Handle(db.file.Fd()), 0, 1, 0, &syscall.Overlapped{
  75. Offset: m1,
  76. OffsetHigh: m1,
  77. })
  78. return err
  79. }
  80. // mmap memory maps a DB's data file.
  81. // Based on: https://github.com/edsrzf/mmap-go
  82. func mmap(db *DB, sz int) error {
  83. if !db.readOnly {
  84. // Truncate the database to the size of the mmap.
  85. if err := db.file.Truncate(int64(sz)); err != nil {
  86. return fmt.Errorf("truncate: %s", err)
  87. }
  88. }
  89. // Open a file mapping handle.
  90. sizelo := uint32(sz >> 32)
  91. sizehi := uint32(sz) & 0xffffffff
  92. h, errno := syscall.CreateFileMapping(syscall.Handle(db.file.Fd()), nil, syscall.PAGE_READONLY, sizelo, sizehi, nil)
  93. if h == 0 {
  94. return os.NewSyscallError("CreateFileMapping", errno)
  95. }
  96. // Create the memory map.
  97. addr, errno := syscall.MapViewOfFile(h, syscall.FILE_MAP_READ, 0, 0, uintptr(sz))
  98. if addr == 0 {
  99. return os.NewSyscallError("MapViewOfFile", errno)
  100. }
  101. // Close mapping handle.
  102. if err := syscall.CloseHandle(syscall.Handle(h)); err != nil {
  103. return os.NewSyscallError("CloseHandle", err)
  104. }
  105. // Convert to a byte array.
  106. db.data = ((*[maxMapSize]byte)(unsafe.Pointer(addr)))
  107. db.datasz = sz
  108. return nil
  109. }
  110. // munmap unmaps a pointer from a file.
  111. // Based on: https://github.com/edsrzf/mmap-go
  112. func munmap(db *DB) error {
  113. if db.data == nil {
  114. return nil
  115. }
  116. addr := (uintptr)(unsafe.Pointer(&db.data[0]))
  117. if err := syscall.UnmapViewOfFile(addr); err != nil {
  118. return os.NewSyscallError("UnmapViewOfFile", err)
  119. }
  120. return nil
  121. }
上海开阖软件有限公司 沪ICP备12045867号-1