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.

90 lines
1.9 KiB

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