bolt_unix_aix.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. //go:build aix
  2. // +build aix
  3. package bbolt
  4. import (
  5. "fmt"
  6. "syscall"
  7. "time"
  8. "unsafe"
  9. "golang.org/x/sys/unix"
  10. )
  11. // flock acquires an advisory lock on a file descriptor.
  12. func flock(db *DB, exclusive bool, timeout time.Duration) error {
  13. var t time.Time
  14. if timeout != 0 {
  15. t = time.Now()
  16. }
  17. fd := db.file.Fd()
  18. var lockType int16
  19. if exclusive {
  20. lockType = syscall.F_WRLCK
  21. } else {
  22. lockType = syscall.F_RDLCK
  23. }
  24. for {
  25. // Attempt to obtain an exclusive lock.
  26. lock := syscall.Flock_t{Type: lockType}
  27. err := syscall.FcntlFlock(fd, syscall.F_SETLK, &lock)
  28. if err == nil {
  29. return nil
  30. } else if err != syscall.EAGAIN {
  31. return err
  32. }
  33. // If we timed out then return an error.
  34. if timeout != 0 && time.Since(t) > timeout-flockRetryTimeout {
  35. return ErrTimeout
  36. }
  37. // Wait for a bit and try again.
  38. time.Sleep(flockRetryTimeout)
  39. }
  40. }
  41. // funlock releases an advisory lock on a file descriptor.
  42. func funlock(db *DB) error {
  43. var lock syscall.Flock_t
  44. lock.Start = 0
  45. lock.Len = 0
  46. lock.Type = syscall.F_UNLCK
  47. lock.Whence = 0
  48. return syscall.FcntlFlock(uintptr(db.file.Fd()), syscall.F_SETLK, &lock)
  49. }
  50. // mmap memory maps a DB's data file.
  51. func mmap(db *DB, sz int) error {
  52. // Map the data file to memory.
  53. b, err := unix.Mmap(int(db.file.Fd()), 0, sz, syscall.PROT_READ, syscall.MAP_SHARED|db.MmapFlags)
  54. if err != nil {
  55. return err
  56. }
  57. // Advise the kernel that the mmap is accessed randomly.
  58. if err := unix.Madvise(b, syscall.MADV_RANDOM); err != nil {
  59. return fmt.Errorf("madvise: %s", err)
  60. }
  61. // Save the original byte slice and convert to a byte array pointer.
  62. db.dataref = b
  63. db.data = (*[maxMapSize]byte)(unsafe.Pointer(&b[0]))
  64. db.datasz = sz
  65. return nil
  66. }
  67. // munmap unmaps a DB's data file from memory.
  68. func munmap(db *DB) error {
  69. // Ignore the unmap if we have no mapped data.
  70. if db.dataref == nil {
  71. return nil
  72. }
  73. // Unmap using the original byte slice.
  74. err := unix.Munmap(db.dataref)
  75. db.dataref = nil
  76. db.data = nil
  77. db.datasz = 0
  78. return err
  79. }