Debian Codegraph @

vendor/github.com/coreos/bbolt/bolt_unix_solaris.go

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