Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions src/rwlock.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ while the write side is acquired/released via `lock(rw)` and `unlock(rw)`.
While a writer is active, all readers will block. Once the writer is finished,
all pending readers will be allowed to acquire/release before the next writer.
"""
mutable struct ReadWriteLock
mutable struct ReadWriteLock <: Base.AbstractLock
writelock::ReentrantLock
readwait::Threads.Condition
writeready::Threads.Event
Expand Down Expand Up @@ -95,6 +95,23 @@ function Base.lock(rw::ReadWriteLock)
return
end

function Base.trylock(rw::ReadWriteLock)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this trying to readlock or write lock?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Writelock, since lock and unlock are for writelock. The readlock equivalent would likely be a custom tryreadlock function.

success = trylock(rw.writelock)
success || return false

r = (@atomic :acquire_release rw.readercount -= MaxReaders) + MaxReaders
if r != 0
r = (@atomic :acquire_release rw.readercount += MaxReaders)
if r > 0
# wake up waiting readers
Base.@lock rw.readwait notify(rw.readwait)
end
unlock(rw.writelock)
return false
end
return true
end

Base.islocked(rw::ReadWriteLock) = islocked(rw.writelock)

function Base.unlock(rw::ReadWriteLock)
Expand All @@ -107,4 +124,4 @@ function Base.unlock(rw::ReadWriteLock)
return
end

end
end