This repository has been archived by the owner on Mar 9, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #208 from benbjohnson/open-timeout
Add Open() options, flock timeout.
- Loading branch information
Showing
20 changed files
with
172 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// +build linux darwin | ||
|
||
package bolt | ||
|
||
import ( | ||
"os" | ||
"syscall" | ||
"time" | ||
"unsafe" | ||
) | ||
|
||
// flock acquires an advisory lock on a file descriptor. | ||
func flock(f *os.File, 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 | ||
} | ||
|
||
// Otherwise attempt to obtain an exclusive lock. | ||
err := syscall.Flock(int(f.Fd()), syscall.LOCK_EX|syscall.LOCK_NB) | ||
if err == nil { | ||
return nil | ||
} else if err != syscall.EWOULDBLOCK { | ||
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 { | ||
return syscall.Flock(int(f.Fd()), syscall.LOCK_UN) | ||
} | ||
|
||
// mmap memory maps a DB's data file. | ||
func mmap(db *DB, sz int) error { | ||
b, err := syscall.Mmap(int(db.file.Fd()), 0, sz, syscall.PROT_READ, syscall.MAP_SHARED) | ||
if err != nil { | ||
return 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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.