Skip to content

Commit 79de7f9

Browse files
committed
Exports
1 parent c0bed0b commit 79de7f9

File tree

3 files changed

+23
-20
lines changed

3 files changed

+23
-20
lines changed

src/SimpleLockFiles.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ module SimpleLockFiles
22

33
include("core.jl")
44

5+
export SimpleLockFile
56
export lock_path, rand_lkid
7+
export acquire_lock, release_lock
8+
export lock
69

710
end

src/core.jl

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ read_lock_file(slf::SimpleLockFile) = _read_lock_file(lock_path(slf))
6666

6767
_is_valid_ttag(ttag) = ttag > time()
6868

69-
function has_lock(lf::String, lkid::String)
69+
function is_locked(lf::String, lkid::String)
7070

7171
# read
7272
curr_lid, ttag = _read_lock_file(lf)
@@ -81,22 +81,22 @@ function has_lock(lf::String, lkid::String)
8181
return lkid == curr_lid
8282
end
8383

84-
has_lock(slf::SimpleLockFile, lkid::String) = has_lock(lock_path(slf), lkid)
84+
is_locked(slf::SimpleLockFile, lkid::String) = is_locked(lock_path(slf), lkid)
8585

8686
# ----------------------------------------------------------------------
8787
# release
8888

8989
function release_lock(lf::String, lkid::String)
9090
!isfile(lf) && return false
91-
!has_lock(lf, lkid) && return false
91+
!is_locked(lf, lkid) && return false
9292
isfile(lf) && rm(lf; force = true)
9393
return true
9494
end
9595

9696
release_lock(slf::SimpleLockFile, lkid::String) = release_lock(lock_path(slf), lkid)
9797

9898
# ----------------------------------------------------------------------
99-
# acquire
99+
# acquire_lock
100100

101101
function _acquire(lf::String, lkid::String = rand_lkid();
102102
vtime = _LOCK_DFT_VALID_TIME
@@ -120,7 +120,7 @@ function _acquire(lf::String, lkid::String = rand_lkid();
120120
return _write_lock_file(lf; lkid, vtime)
121121
end
122122

123-
function acquire(lf::String, lkid::String = rand_lkid();
123+
function acquire_lock(lf::String, lkid::String = rand_lkid();
124124
vtime = _LOCK_DFT_VALID_TIME,
125125
wt = _LOCK_DFT_WAIT_TIME,
126126
tout = _LOCK_DFT_TIME_OUT,
@@ -147,7 +147,7 @@ function acquire(lf::String, lkid::String = rand_lkid();
147147
end
148148
end
149149

150-
acquire(slf::SimpleLockFile, lkid::String = rand_lkid(); kwargs...) = acquire(lock_path(slf), lkid)
150+
acquire_lock(slf::SimpleLockFile, lkid::String = rand_lkid(); kwargs...) = acquire_lock(lock_path(slf), lkid)
151151

152152
# ----------------------------------------------------------------------
153153
# Base.lock
@@ -161,10 +161,10 @@ import Base.lock
161161
force = false
162162
)
163163
164-
Acquire the lock, execute `f()` with the lock held, and release the lock when f returns.
164+
acquire_lock the lock, execute `f()` with the lock held, and release the lock when f returns.
165165
If the lock is already locked by a different `lkid`, wait (till `tout`) for it to become available.
166-
During waiting, it will sleep `wt` seconds before re-attemping to acquire.
167-
If `force = true` it will acquire the lock after `tout`.
166+
During waiting, it will sleep `wt` seconds before re-attemping to acquire_lock.
167+
If `force = true` it will acquire_lock the lock after `tout`.
168168
This method is not fully secure to race, but it must be ok for sllow applications.
169169
Returns `true` if the lock
170170
@@ -179,10 +179,10 @@ function lock(
179179

180180
lf = lock_path(slf)
181181
try
182-
acquire(lf, lkid; force, vtime, wt, tout)
182+
acquire_lock(lf, lkid; force, vtime, wt, tout)
183183
f()
184184
finally
185-
ok_flag = has_lock(lf, lkid)
185+
ok_flag = is_locked(lf, lkid)
186186
release_lock(lf, lkid)
187187
return ok_flag
188188
end

test/runtests.jl

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,39 +23,39 @@ using Test
2323
@test ttag3 > time()
2424
@test isfile(slf)
2525

26-
@test SLF.has_lock(slf, lid3)
26+
@test SLF.is_locked(slf, lid3)
2727

28-
lid4, ttag4 = SLF.acquire(slf) # This must be taken
28+
lid4, ttag4 = SLF.acquire_lock(slf) # This must be taken
2929
@test isempty(lid4)
3030
@test ttag3 == ttag4
3131

3232
sleep(2 * vtime) # expire lock
3333

34-
@test !SLF.has_lock(slf, lid3)
35-
@test !isfile(slf) # has_lock must delete an invalid lock file
34+
@test !SLF.is_locked(slf, lid3)
35+
@test !isfile(slf) # is_locked must delete an invalid lock file
3636

3737
vtime = 50.0
38-
lid4, ttag4 = SLF.acquire(slf; vtime) # This must be free
38+
lid4, ttag4 = SLF.acquire_lock(slf; vtime) # This must be free
3939
@test lid4 != lid3
4040
@test ttag4 > ttag3
4141
@test isfile(slf)
4242

4343
# test wait
44-
lid5, ttag5 = SLF.acquire(slf; tout = 2.0) # This must fail
44+
lid5, ttag5 = SLF.acquire_lock(slf; tout = 2.0) # This must fail
4545
@test isempty(lid5)
4646
@test ttag4 == ttag5
4747

4848
# Test release
49-
@test SLF.has_lock(slf, lid4)
49+
@test SLF.is_locked(slf, lid4)
5050
@test SLF.release_lock(slf, lid4)
51-
@test !SLF.has_lock(slf, lid4)
51+
@test !SLF.is_locked(slf, lid4)
5252
@test !isfile(slf)
5353

5454
# base.lock
5555
lock(slf; vtime = 3.0) do
5656
# all this time the lock is taken
5757
for it in 1:10
58-
@test !SLF.has_lock(slf, "Not a lock id")
58+
@test !SLF.is_locked(slf, "Not a lock id")
5959
sleep(0.2)
6060
end
6161
end

0 commit comments

Comments
 (0)