@@ -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
3341end
@@ -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)
5162end
5263
5364function 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
5970end
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+
6183import Base. islocked
6284function 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
82106end
83107
84108# ----------------------------------------------------------------------
0 commit comments