package bbolt
import (
"fmt"
"syscall"
"time"
"unsafe"
"golang.org/x/sys/unix"
)
func flock(db *DB , exclusive bool , timeout time .Duration ) error {
var t time .Time
if timeout != 0 {
t = time .Now ()
}
fd := db .file .Fd ()
flag := syscall .LOCK_NB
if exclusive {
flag |= syscall .LOCK_EX
} else {
flag |= syscall .LOCK_SH
}
for {
err := syscall .Flock (int (fd ), flag )
if err == nil {
return nil
} else if err != syscall .EWOULDBLOCK {
return err
}
if timeout != 0 && time .Since (t ) > timeout -flockRetryTimeout {
return ErrTimeout
}
time .Sleep (flockRetryTimeout )
}
}
func funlock(db *DB ) error {
return syscall .Flock (int (db .file .Fd ()), syscall .LOCK_UN )
}
func mmap(db *DB , sz int ) error {
b , err := unix .Mmap (int (db .file .Fd ()), 0 , sz , syscall .PROT_READ , syscall .MAP_SHARED |db .MmapFlags )
if err != nil {
return err
}
err = unix .Madvise (b , syscall .MADV_RANDOM )
if err != nil && err != syscall .ENOSYS {
return fmt .Errorf ("madvise: %s" , err )
}
db .dataref = b
db .data = (*[maxMapSize ]byte )(unsafe .Pointer (&b [0 ]))
db .datasz = sz
return nil
}
func munmap(db *DB ) error {
if db .dataref == nil {
return nil
}
err := unix .Munmap (db .dataref )
db .dataref = nil
db .data = nil
db .datasz = 0
return err
}
The pages are generated with Golds v0.8.2 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .