Skip to content

Commit 4e38ed8

Browse files
committed
Add SimpleLockFileError error
1 parent b94ecd3 commit 4e38ed8

File tree

4 files changed

+54
-19
lines changed

4 files changed

+54
-19
lines changed

src/SimpleLockFiles.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ module SimpleLockFiles
1111
include("interface.jl")
1212

1313
export SimpleLockFile
14+
export SimpleLockFileError
1415
export lockpath
1516

1617
end

src/core.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# ----------------------------------------------------------------------
2-
# type
2+
# types
33
mutable struct SimpleLockFile <: Base.AbstractLock
44
pidfile_path::AbstractString
55
reelk::ReentrantLock
@@ -11,3 +11,7 @@ mutable struct SimpleLockFile <: Base.AbstractLock
1111
return lk
1212
end
1313
end
14+
15+
struct SimpleLockFileError <: Exception
16+
msg::AbstractString
17+
end

src/interface.jl

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,15 @@ function Base.lock(slf::SimpleLockFile;
2727
lock(slf.reelk)
2828

2929
# pidfile
30-
isnothing(slf.mon) || error("'lock' call without matching 'unlock'")
30+
if !isnothing(slf.mon)
31+
unlock(slf.reelk) # avoid unsync between pidfile and reelk state
32+
throw(SimpleLockFileError(string(
33+
"'lock' call without matching 'unlock', file: ",
34+
lockpath(slf)
35+
)))
36+
end
37+
38+
mkpath(slf)
3139
slf.mon = mkpidlock(slf.pidfile_path; stale_age, refresh, poll_interval)
3240
return slf
3341
end
@@ -37,8 +45,11 @@ function Base.unlock(slf::SimpleLockFile; force = false)
3745

3846
# pidfile
3947
if isnothing(slf.mon)
40-
force && rm(slf.pidfile_path; force = true)
41-
error("'unlock' call without matching 'lock'")
48+
force && rm(slf.pidfile_path; force = true) # Think about it
49+
throw(SimpleLockFileError(string(
50+
"'unlock' call without matching 'lock'. file: ",
51+
lockpath(slf)
52+
)))
4253
end
4354
close(slf.mon)
4455
force && rm(slf.pidfile_path; force = true)
@@ -51,34 +62,47 @@ function Base.unlock(slf::SimpleLockFile; force = false)
5162
end
5263

5364
function Base.lock(f::Function, slf::SimpleLockFile; force = false, kwargs...)
54-
try; lock(slf,; kwargs...)
65+
try; lock(slf; kwargs...)
5566
return f()
5667
finally
5768
unlock(slf; force)
5869
end
5970
end
6071

72+
function islocked_pidfile(slf::SimpleLockFile)
73+
# Ignoring the monitor, all checks depending on the file
74+
isfile(slf.pidfile_path) || return false
75+
# trymkpidlock return false if try failed
76+
stale_age = get(slf.extras, "stale_age.islock", 5 * SLF_DEAFAULT_STALE_AGE)::Float64
77+
_success = trymkpidlock(slf.pidfile_path; stale_age) do
78+
return true
79+
end
80+
return !_success
81+
end
82+
6183
import Base.islocked
6284
function Base.islocked(slf::SimpleLockFile)
6385
# reelk
6486
_islocked_reelk = islocked(slf.reelk)
6587

6688
# pidfile
67-
# Ignoring the monitor, all checks depending on the file
68-
isfile(slf.pidfile_path) || return false
69-
# trymkpidlock return false if try failed
70-
stale_age = get(slf.extras, "stale_age", SLF_DEAFAULT_STALE_AGE)::Float64
71-
_islocked_pidfile = !trymkpidlock(slf.pidfile_path; stale_age) do
72-
return true
73-
end
74-
75-
(_islocked_pidfile === _islocked_reelk) ||
76-
error(
77-
"unvalid lock state!!!",
78-
" reelk, ", _islocked_reelk,
79-
", pidfile: ", _islocked_pidfile
89+
_islocked_pidfile = islocked_pidfile(slf)
90+
91+
# This is legal
92+
# reelk = :unlocked
93+
# pidfile = :locked
94+
# This illegal
95+
# reelk = :locked
96+
# pidfile = :unlocked
97+
# TODO: TAI: make it an error
98+
(_islocked_reelk && !_islocked_pidfile) &&
99+
@warn(
100+
"unsync lock state!!!",
101+
reelk = _islocked_reelk ? :locked : :unlocked,
102+
pidfile = _islocked_pidfile ? :locked : :unlocked
80103
)
81-
return _islocked_pidfile
104+
105+
return _islocked_pidfile | _islocked_reelk
82106
end
83107

84108
# ----------------------------------------------------------------------

test/core_tests.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ let
66
try
77
slf = SimpleLockFile(lkfn)
88
@test lockpath(slf) == lkfn
9+
10+
# Nested locking is not allowed
11+
@test_throws SimpleLockFileError lock(slf) do
12+
lock(slf) do
13+
end
14+
end
915

1016
# Single threaded test
1117
for it in 1:100

0 commit comments

Comments
 (0)