diff options
author | Ben Johnson <benbjohnson@yahoo.com> | 2015-08-24 19:23:40 -0600 |
---|---|---|
committer | Ben Johnson <benbjohnson@yahoo.com> | 2015-08-24 19:23:40 -0600 |
commit | 90fef389f98027ca55594edd7dbd6e7f3926fdad (patch) | |
tree | 3618ac037610f5f9aebb9686ec55e9aa1bf4d50d /bolt_unix_solaris.go | |
parent | Merge pull request #405 from artyom/bolt-command-prints-fix (diff) | |
parent | solaris: fix issues with mmap, munmap, madvise and flock (diff) | |
download | dedo-90fef389f98027ca55594edd7dbd6e7f3926fdad.tar.gz dedo-90fef389f98027ca55594edd7dbd6e7f3926fdad.tar.xz |
Merge pull request #411 from Apsalar/solaris-mmap-flock-fixes
solaris: fix issues with mmap, munmap, madvise and flock
Diffstat (limited to 'bolt_unix_solaris.go')
-rw-r--r-- | bolt_unix_solaris.go | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/bolt_unix_solaris.go b/bolt_unix_solaris.go new file mode 100644 index 0000000..f480ee7 --- /dev/null +++ b/bolt_unix_solaris.go @@ -0,0 +1,101 @@ + +package bolt + +import ( + "fmt" + "os" + "syscall" + "time" + "unsafe" + "golang.org/x/sys/unix" +) + +// flock acquires an advisory lock on a file descriptor. +func flock(f *os.File, exclusive bool, timeout time.Duration) error { + 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 + } + var lock syscall.Flock_t + lock.Start = 0 + lock.Len = 0 + lock.Pid = 0 + lock.Whence = 0 + lock.Pid = 0 + if exclusive { + lock.Type = syscall.F_WRLCK + } else { + lock.Type = syscall.F_RDLCK + } + err := syscall.FcntlFlock(f.Fd(), syscall.F_SETLK, &lock) + if err == nil { + return nil + } else if err != syscall.EAGAIN { + 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 { + var lock syscall.Flock_t + lock.Start = 0 + lock.Len = 0 + lock.Type = syscall.F_UNLCK + lock.Whence = 0 + return syscall.FcntlFlock(uintptr(f.Fd()), syscall.F_SETLK, &lock) +} + +// mmap memory maps a DB's data file. +func mmap(db *DB, sz int) error { + // Truncate and fsync to ensure file size metadata is flushed. + // https://github.com/boltdb/bolt/issues/284 + if !db.NoGrowSync && !db.readOnly { + if err := db.file.Truncate(int64(sz)); err != nil { + return fmt.Errorf("file resize error: %s", err) + } + if err := db.file.Sync(); err != nil { + return fmt.Errorf("file sync error: %s", err) + } + } + + // Map the data file to memory. + b, err := unix.Mmap(int(db.file.Fd()), 0, sz, syscall.PROT_READ, syscall.MAP_SHARED) + if err != nil { + return err + } + + // Advise the kernel that the mmap is accessed randomly. + if err := unix.Madvise(b, syscall.MADV_RANDOM); err != nil { + return fmt.Errorf("madvise: %s", err) + } + + // 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 := unix.Munmap(db.dataref) + db.dataref = nil + db.data = nil + db.datasz = 0 + return err +} |