mirror of https://github.com/go-gitea/gitea
Git with a cup of tea, painless self-hosted git service
Mirror for internal git.with.parts use
https://git.with.parts
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
94 lines
2.1 KiB
94 lines
2.1 KiB
5 years ago
|
// +build !windows,!plan9,!solaris,!aix
|
||
8 years ago
|
|
||
6 years ago
|
package bbolt
|
||
8 years ago
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"syscall"
|
||
|
"time"
|
||
|
"unsafe"
|
||
|
)
|
||
|
|
||
|
// flock acquires an advisory lock on a file descriptor.
|
||
6 years ago
|
func flock(db *DB, exclusive bool, timeout time.Duration) error {
|
||
8 years ago
|
var t time.Time
|
||
6 years ago
|
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
|
||
|
}
|
||
8 years ago
|
for {
|
||
6 years ago
|
// Attempt to obtain an exclusive lock.
|
||
|
err := syscall.Flock(int(fd), flag)
|
||
8 years ago
|
if err == nil {
|
||
|
return nil
|
||
|
} else if err != syscall.EWOULDBLOCK {
|
||
|
return err
|
||
|
}
|
||
|
|
||
6 years ago
|
// If we timed out then return an error.
|
||
|
if timeout != 0 && time.Since(t) > timeout-flockRetryTimeout {
|
||
|
return ErrTimeout
|
||
|
}
|
||
|
|
||
8 years ago
|
// Wait for a bit and try again.
|
||
6 years ago
|
time.Sleep(flockRetryTimeout)
|
||
8 years ago
|
}
|
||
|
}
|
||
|
|
||
|
// funlock releases an advisory lock on a file descriptor.
|
||
|
func funlock(db *DB) error {
|
||
|
return syscall.Flock(int(db.file.Fd()), syscall.LOCK_UN)
|
||
|
}
|
||
|
|
||
|
// mmap memory maps a DB's data file.
|
||
|
func mmap(db *DB, sz int) error {
|
||
|
// Map the data file to memory.
|
||
|
b, err := syscall.Mmap(int(db.file.Fd()), 0, sz, syscall.PROT_READ, syscall.MAP_SHARED|db.MmapFlags)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
// Advise the kernel that the mmap is accessed randomly.
|
||
6 years ago
|
err = madvise(b, syscall.MADV_RANDOM)
|
||
|
if err != nil && err != syscall.ENOSYS {
|
||
|
// Ignore not implemented error in kernel because it still works.
|
||
8 years ago
|
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 := syscall.Munmap(db.dataref)
|
||
|
db.dataref = nil
|
||
|
db.data = nil
|
||
|
db.datasz = 0
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
// NOTE: This function is copied from stdlib because it is not available on darwin.
|
||
|
func madvise(b []byte, advice int) (err error) {
|
||
|
_, _, e1 := syscall.Syscall(syscall.SYS_MADVISE, uintptr(unsafe.Pointer(&b[0])), uintptr(len(b)), uintptr(advice))
|
||
|
if e1 != 0 {
|
||
|
err = e1
|
||
|
}
|
||
|
return
|
||
|
}
|