Skip to content

Commit

Permalink
Parameterise ReadWriteLock by inner Lock type
Browse files Browse the repository at this point in the history
  • Loading branch information
nickrobinson251 committed Aug 12, 2019
1 parent c8649f2 commit 2fad525
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/ReadWriteLocks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
5 changes: 5 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 2fad525

Please sign in to comment.