2016-01-15 18:48:03 +00:00
|
|
|
// +build !windows,!plan9,!solaris
|
2015-03-04 16:38:10 +00:00
|
|
|
|
|
|
|
package bolt
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
|
|
|
// flock acquires an advisory lock on a file descriptor.
|
2016-01-15 18:48:03 +00:00
|
|
|
func flock(f *os.File, exclusive bool, timeout time.Duration) error {
|
2015-03-04 16:38:10 +00:00
|
|
|
var t time.Time
|
|
|
|
for {
|
|
|
|
// If we're beyond our timeout then return an error.
|
|
|
|
// This can only occur after we've attempted a flock once.
|
|
|
|
if t.IsZero() {
|
|
|
|
t = time.Now()
|
|
|
|
} else if timeout > 0 && time.Since(t) > timeout {
|
|
|
|
return ErrTimeout
|
|
|
|
}
|
2016-01-15 18:48:03 +00:00
|
|
|
flag := syscall.LOCK_SH
|
|
|
|
if exclusive {
|
|
|
|
flag = syscall.LOCK_EX
|
|
|
|
}
|
2015-03-04 16:38:10 +00:00
|
|
|
|
|
|
|
// Otherwise attempt to obtain an exclusive lock.
|
2016-01-15 18:48:03 +00:00
|
|
|
err := syscall.Flock(int(f.Fd()), flag|syscall.LOCK_NB)
|
2015-03-04 16:38:10 +00:00
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
} else if err != syscall.EWOULDBLOCK {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for a bit and try again.
|
|
|
|
time.Sleep(50 * time.Millisecond)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// funlock releases an advisory lock on a file descriptor.
|
|
|
|
func funlock(f *os.File) error {
|
|
|
|
return syscall.Flock(int(f.Fd()), syscall.LOCK_UN)
|
|
|
|
}
|
|
|
|
|
|
|
|
// mmap memory maps a DB's data file.
|
|
|
|
func mmap(db *DB, sz int) error {
|
|
|
|
// Map the data file to memory.
|
2016-01-15 18:48:03 +00:00
|
|
|
b, err := syscall.Mmap(int(db.file.Fd()), 0, sz, syscall.PROT_READ, syscall.MAP_SHARED|db.MmapFlags)
|
2015-03-04 16:38:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-01-15 18:48:03 +00:00
|
|
|
// Advise the kernel that the mmap is accessed randomly.
|
|
|
|
if err := madvise(b, syscall.MADV_RANDOM); err != nil {
|
|
|
|
return fmt.Errorf("madvise: %s", err)
|
|
|
|
}
|
|
|
|
|
2015-03-04 16:38:10 +00:00
|
|
|
// Save the original byte slice and convert to a byte array pointer.
|
|
|
|
db.dataref = b
|
|
|
|
db.data = (*[maxMapSize]byte)(unsafe.Pointer(&b[0]))
|
|
|
|
db.datasz = sz
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// munmap unmaps a DB's data file from memory.
|
|
|
|
func munmap(db *DB) error {
|
|
|
|
// Ignore the unmap if we have no mapped data.
|
|
|
|
if db.dataref == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmap using the original byte slice.
|
|
|
|
err := syscall.Munmap(db.dataref)
|
|
|
|
db.dataref = nil
|
|
|
|
db.data = nil
|
|
|
|
db.datasz = 0
|
|
|
|
return err
|
|
|
|
}
|
2016-01-15 18:48:03 +00:00
|
|
|
|
|
|
|
// NOTE: This function is copied from stdlib because it is not available on darwin.
|
|
|
|
func madvise(b []byte, advice int) (err error) {
|
|
|
|
_, _, e1 := syscall.Syscall(syscall.SYS_MADVISE, uintptr(unsafe.Pointer(&b[0])), uintptr(len(b)), uintptr(advice))
|
|
|
|
if e1 != 0 {
|
|
|
|
err = e1
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|