bolt_unix.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // +build !windows,!plan9,!solaris
  2. package bolt
  3. import (
  4. "fmt"
  5. "os"
  6. "syscall"
  7. "time"
  8. "unsafe"
  9. )
  10. // flock acquires an advisory lock on a file descriptor.
  11. func flock(db *DB, mode os.FileMode, exclusive bool, timeout time.Duration) error {
  12. var t time.Time
  13. for {
  14. // If we're beyond our timeout then return an error.
  15. // This can only occur after we've attempted a flock once.
  16. if t.IsZero() {
  17. t = time.Now()
  18. } else if timeout > 0 && time.Since(t) > timeout {
  19. return ErrTimeout
  20. }
  21. flag := syscall.LOCK_SH
  22. if exclusive {
  23. flag = syscall.LOCK_EX
  24. }
  25. // Otherwise attempt to obtain an exclusive lock.
  26. err := syscall.Flock(int(db.file.Fd()), flag|syscall.LOCK_NB)
  27. if err == nil {
  28. return nil
  29. } else if err != syscall.EWOULDBLOCK {
  30. return err
  31. }
  32. // Wait for a bit and try again.
  33. time.Sleep(50 * time.Millisecond)
  34. }
  35. }
  36. // funlock releases an advisory lock on a file descriptor.
  37. func funlock(db *DB) error {
  38. return syscall.Flock(int(db.file.Fd()), syscall.LOCK_UN)
  39. }
  40. // mmap memory maps a DB's data file.
  41. func mmap(db *DB, sz int) error {
  42. // Map the data file to memory.
  43. b, err := syscall.Mmap(int(db.file.Fd()), 0, sz, syscall.PROT_READ, syscall.MAP_SHARED|db.MmapFlags)
  44. if err != nil {
  45. return err
  46. }
  47. // Advise the kernel that the mmap is accessed randomly.
  48. if err := madvise(b, syscall.MADV_RANDOM); err != nil {
  49. return fmt.Errorf("madvise: %s", err)
  50. }
  51. // Save the original byte slice and convert to a byte array pointer.
  52. db.dataref = b
  53. db.data = (*[maxMapSize]byte)(unsafe.Pointer(&b[0]))
  54. db.datasz = sz
  55. return nil
  56. }
  57. // munmap unmaps a DB's data file from memory.
  58. func munmap(db *DB) error {
  59. // Ignore the unmap if we have no mapped data.
  60. if db.dataref == nil {
  61. return nil
  62. }
  63. // Unmap using the original byte slice.
  64. err := syscall.Munmap(db.dataref)
  65. db.dataref = nil
  66. db.data = nil
  67. db.datasz = 0
  68. return err
  69. }
  70. // NOTE: This function is copied from stdlib because it is not available on darwin.
  71. func madvise(b []byte, advice int) (err error) {
  72. _, _, e1 := syscall.Syscall(syscall.SYS_MADVISE, uintptr(unsafe.Pointer(&b[0])), uintptr(len(b)), uintptr(advice))
  73. if e1 != 0 {
  74. err = e1
  75. }
  76. return
  77. }