-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Store): a generic file-based lock type
I have a feeling I'll want to end up using this for more than just inventory writing in time.
- Loading branch information
Showing
4 changed files
with
81 additions
and
1 deletion.
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,76 @@ | ||
""" | ||
LockFile(path::String) -> LockFile | ||
A file-based lock that can be used to synchronise access to a resource across | ||
multiple processes. The lock is implemented using a file at `path` that is | ||
created when the lock is acquired and deleted when the lock is released. | ||
""" | ||
struct LockFile <: Base.AbstractLock | ||
path::String | ||
owned::ReentrantLock | ||
end | ||
|
||
function LockFile(prefix::String, target) | ||
path = BaseDirs.User.runtime( | ||
PROJECT_SUBPATH, | ||
prefix * "-" * string(hash(inv.file.path), base=32) * ".lock") | ||
LockFile(path, ReentrantLock()) | ||
end | ||
|
||
function Base.islocked(lf::LockFile) | ||
islocked(lf.owned) && return true | ||
isfile(lf.path) || return false | ||
pid = open(io -> if eof(io) 0 else read(io, Int) end, lf.path) | ||
iszero(@ccall uv_kill(pid::Cint, 0::Cint)::Cint) | ||
end | ||
|
||
function Base.trylock(lf::LockFile) | ||
islocked(lf.owned) && return trylock(lf.owned) | ||
islocked(lf) && return false | ||
ispath(dirname(lf.path)) || mkpath(dirname(lf.path)) | ||
try | ||
write(lf.path, UInt(getpid())) | ||
chmod(lf.path, 0o444) | ||
catch _ | ||
return false | ||
end | ||
lock(lf.owned) | ||
true | ||
end | ||
|
||
function Base.lock(lf::LockFile) | ||
backoff = 0.00001 # 10μs, given that it takes 5μs lock + unlock on my machine | ||
while !trylock(lf) | ||
quicksleep(backoff) | ||
backoff = min(0.05, backoff * 2) | ||
end | ||
end | ||
|
||
function Base.unlock(lf::LockFile) | ||
backoff = 0.00001 # 10μs, given that it takes 5μs to lock + unlock on my machine | ||
if !islocked(lf.owned) | ||
while islocked(lf) | ||
quicksleep(backoff) | ||
backoff = min(0.05, backoff * 2) | ||
end | ||
end | ||
rm(lf.path, force=true) | ||
unlock(lf.owned) | ||
end | ||
|
||
# REVIEW: Only needed until something like <https://github.com/JuliaLang/julia/pull/55163> lands. | ||
""" | ||
quicksleep(period::Real) | ||
Sleep for `period` seconds, but use a busy loop for short periods (< 2ms). | ||
""" | ||
function quicksleep(period::Real) | ||
if period < 0.02 | ||
start = time() | ||
while time() - start <= period | ||
yield() | ||
end | ||
else | ||
sleep(period) | ||
end | ||
end |
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