blob: 596e5406023c04d41bcebf3409d134400dcd330e [file] [log] [blame]
Abhay Kumar40252eb2025-10-13 13:25:53 +00001//go:build aix
2
3package bbolt
4
5import (
6 "fmt"
7 "syscall"
8 "time"
9 "unsafe"
10
11 "golang.org/x/sys/unix"
12
13 "go.etcd.io/bbolt/internal/common"
14)
15
16// flock acquires an advisory lock on a file descriptor.
17func flock(db *DB, exclusive bool, timeout time.Duration) error {
18 var t time.Time
19 if timeout != 0 {
20 t = time.Now()
21 }
22 fd := db.file.Fd()
23 var lockType int16
24 if exclusive {
25 lockType = syscall.F_WRLCK
26 } else {
27 lockType = syscall.F_RDLCK
28 }
29 for {
30 // Attempt to obtain an exclusive lock.
31 lock := syscall.Flock_t{Type: lockType}
32 err := syscall.FcntlFlock(fd, syscall.F_SETLK, &lock)
33 if err == nil {
34 return nil
35 } else if err != syscall.EAGAIN {
36 return err
37 }
38
39 // If we timed out then return an error.
40 if timeout != 0 && time.Since(t) > timeout-flockRetryTimeout {
41 return ErrTimeout
42 }
43
44 // Wait for a bit and try again.
45 time.Sleep(flockRetryTimeout)
46 }
47}
48
49// funlock releases an advisory lock on a file descriptor.
50func funlock(db *DB) error {
51 var lock syscall.Flock_t
52 lock.Start = 0
53 lock.Len = 0
54 lock.Type = syscall.F_UNLCK
55 lock.Whence = 0
56 return syscall.FcntlFlock(uintptr(db.file.Fd()), syscall.F_SETLK, &lock)
57}
58
59// mmap memory maps a DB's data file.
60func mmap(db *DB, sz int) error {
61 // Map the data file to memory.
62 b, err := unix.Mmap(int(db.file.Fd()), 0, sz, syscall.PROT_READ, syscall.MAP_SHARED|db.MmapFlags)
63 if err != nil {
64 return err
65 }
66
67 // Advise the kernel that the mmap is accessed randomly.
68 if err := unix.Madvise(b, syscall.MADV_RANDOM); err != nil {
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}