diff --git a/src/ReadWriteLocks.jl b/src/ReadWriteLocks.jl index fbe7c28..b1d0868 100644 --- a/src/ReadWriteLocks.jl +++ b/src/ReadWriteLocks.jl @@ -17,16 +17,23 @@ struct WriteLock{T<:AbstractLock} rwlock::T end -mutable struct ReadWriteLock <: AbstractLock +# Need Julia VERSION > v"1.2.0-DEV.28` to have `ReentrantLock <: AbstractLock` +LockTypes = Union{AbstractLock, ReentrantLock} +mutable struct ReadWriteLock{L<:LockTypes} <: AbstractLock readers::Int writer::Bool - lock::ReentrantLock # reentrant mutex + lock::L # reentrant mutex condition::Condition read_lock::ReadLock write_lock::WriteLock - function ReadWriteLock() - rwlock = new(false, 0, ReentrantLock(), Condition()) + function ReadWriteLock( + readers::Int=0, + writer::Bool=false, + lock::L=ReentrantLock(), + condition::Condition=Condition(), + ) where L <: LockTypes + rwlock = new{L}(readers, writer, lock, condition) rwlock.read_lock = ReadLock(rwlock) rwlock.write_lock = WriteLock(rwlock) diff --git a/test/runtests.jl b/test/runtests.jl index 131b899..89a27f6 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -3,6 +3,11 @@ using Test @testset "ReadWriteLock" begin +@testset "Constructor" begin + @test ReadWriteLock() isa ReadWriteLock{ReentrantLock} + @test ReadWriteLock(0, false, Threads.Mutex()) isa ReadWriteLock{Threads.Mutex} +end + @testset "Single-threaded tests" begin @testset "Initialization" begin rwlock = ReadWriteLock()