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