Skip to content

Commit 47c3fce

Browse files
committed
ReentrantLock is thread-safe on Julia >=v1.2-DEV.28
Make ThreadSafeReentrantLock and alias for ReentrantLock on Julia >=v1.2-DEV.28.
1 parent ecff5c9 commit 47c3fce

File tree

2 files changed

+32
-27
lines changed

2 files changed

+32
-27
lines changed

src/threadsafe.jl

+24-21
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,37 @@ export ThreadSafe
55
abstract type ThreadSafe{T} end
66

77

8-
98
export ThreadSafeReentrantLock
109

11-
struct ThreadSafeReentrantLock
12-
thread_lock::RecursiveSpinLock
13-
task_lock::ReentrantLock
10+
@static if VERSION >= v"1.2-DEV.28"
11+
const ThreadSafeReentrantLock = ReentrantLock
12+
else
13+
struct ThreadSafeReentrantLock
14+
thread_lock::RecursiveSpinLock
15+
task_lock::ReentrantLock
1416

15-
ThreadSafeReentrantLock() = new(RecursiveSpinLock(), ReentrantLock())
16-
end
17+
ThreadSafeReentrantLock() = new(RecursiveSpinLock(), ReentrantLock())
18+
end
1719

18-
function Base.lock(l::ThreadSafeReentrantLock)
19-
# @debug "LOCKING $l"
20-
lock(l.thread_lock)
21-
try
22-
lock(l.task_lock)
23-
catch err
24-
unlock(l.thread_lock)
25-
rethrow()
20+
function Base.lock(l::ThreadSafeReentrantLock)
21+
# @debug "LOCKING $l"
22+
lock(l.thread_lock)
23+
try
24+
lock(l.task_lock)
25+
catch err
26+
unlock(l.thread_lock)
27+
rethrow()
28+
end
2629
end
27-
end
2830

2931

30-
function Base.unlock(l::ThreadSafeReentrantLock)
31-
# @debug "UNLOCKING $l"
32-
try
33-
unlock(l.task_lock)
34-
finally
35-
unlock(l.thread_lock)
32+
function Base.unlock(l::ThreadSafeReentrantLock)
33+
# @debug "UNLOCKING $l"
34+
try
35+
unlock(l.task_lock)
36+
finally
37+
unlock(l.thread_lock)
38+
end
3639
end
3740
end
3841

test/test_threadsafe.jl

+8-6
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@ using Base.Threads
99

1010
@testset "ThreadSafeReentrantLock" begin
1111
tsReLock = @inferred ThreadSafeReentrantLock()
12-
lock(tsReLock)
13-
@test islocked(tsReLock.thread_lock)
14-
@test islocked(tsReLock.task_lock)
15-
unlock(tsReLock)
16-
@test !islocked(tsReLock.thread_lock)
17-
@test !islocked(tsReLock.task_lock)
12+
@static if VERSION < v"1.2-DEV.28"
13+
lock(tsReLock)
14+
@test islocked(tsReLock.thread_lock)
15+
@test islocked(tsReLock.task_lock)
16+
unlock(tsReLock)
17+
@test !islocked(tsReLock.thread_lock)
18+
@test !islocked(tsReLock.task_lock)
19+
end
1820
end
1921

2022
@testset "LockableValue" begin

0 commit comments

Comments
 (0)