Skip to content

Commit

Permalink
Make read lock API more Base-like
Browse files Browse the repository at this point in the history
  • Loading branch information
tkf committed Apr 25, 2022
1 parent ea0a68e commit b131065
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 25 deletions.
15 changes: 7 additions & 8 deletions src/ConcurrentUtils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,10 @@ InternalPrelude.@exported_function isacquirable
InternalPrelude.@exported_function isacquirable_read
=#

InternalPrelude.@exported_function acquire_read
InternalPrelude.@exported_function acquire_read_then
InternalPrelude.@exported_function read_write_lock
InternalPrelude.@exported_function release_read
InternalPrelude.@exported_function try_race_acquire_read
InternalPrelude.@exported_function trylock_read
InternalPrelude.@exported_function lock_read
InternalPrelude.@exported_function unlock_read

abstract type AbstractGuard end
abstract type AbstractReadWriteGuard end
Expand Down Expand Up @@ -109,13 +108,13 @@ using ..ConcurrentUtils:
NotSetError,
OccupiedError,
TooManyTries,
acquire_read,
acquire_read_then,
lock_read,
lock_read,
race_fetch_or!,
release_read,
unlock_read,
spinfor,
spinloop,
try_race_acquire_read,
trylock_read,
try_race_fetch,
try_race_fetch_or!,
try_race_put!
Expand Down
2 changes: 1 addition & 1 deletion src/guards.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ end
function ConcurrentUtils.guarding_read(f::F, g::GenericReadWriteGuard) where {F}
data = g.data
criticalsection() = f(data)
acquire_read_then(criticalsection, g.lock)
lock_read(criticalsection, g.lock)
end

# Maybe this is a bad idea since it's hard to remember that `guarding(_, ::ReadGuard)`
Expand Down
12 changes: 6 additions & 6 deletions src/lock_interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@

abstract type AbstractReadWriteLock <: Base.AbstractLock end

function ConcurrentUtils.acquire_read_then(f, lock::AbstractReadWriteLock)
acquire_read(lock)
function ConcurrentUtils.lock_read(f, lock::AbstractReadWriteLock)
lock_read(lock)
try
return f()
finally
release_read(lock)
unlock_read(lock)
end
end

struct ReadLockHandle{RWLock} <: Base.AbstractLock
rwlock::RWLock
end

Base.trylock(lck::ReadLockHandle) = Try.iok(try_race_acquire_read(lck.rwlock))
Base.lock(lck::ReadLockHandle) = acquire_read(lck.rwlock)
Base.unlock(lck::ReadLockHandle) = release_read(lck.rwlock)
Base.trylock(lck::ReadLockHandle) = trylock_read(lck.rwlock)
Base.lock(lck::ReadLockHandle) = lock_read(lck.rwlock)
Base.unlock(lck::ReadLockHandle) = unlock_read(lck.rwlock)

ConcurrentUtils.read_write_lock(lock::AbstractReadWriteLock = ReadWriteLock()) =
(ReadLockHandle(lock), lock)
16 changes: 6 additions & 10 deletions src/read_write_lock.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,7 @@ function ReadWriteLock()
end

# Not very efficient but lock-free
function ConcurrentUtils.try_race_acquire_read(
rwlock::ReadWriteLock;
nspins = -∞,
ntries = -∞,
)
function ConcurrentUtils.trylock_read(rwlock::ReadWriteLock; nspins = -∞, ntries = -∞)
local ns::Int = 0
local nt::Int = 0
while true
Expand All @@ -41,16 +37,16 @@ function ConcurrentUtils.try_race_acquire_read(
rwlock.nreaders_and_writelock,
old => old + NREADERS_INC,
)
success && return Ok(nothing)
success && return true
nt += 1
nt < ntries || return Err(TooManyTries(ns, nt))
nt < ntries || return false
end
ns += 1
ns < nspins || return Err(TooManyTries(ns, nt))
ns < nspins || return false
end
end

function ConcurrentUtils.acquire_read(rwlock::ReadWriteLock)
function ConcurrentUtils.lock_read(rwlock::ReadWriteLock)

# Using hardware FAA
ptr = Ptr{NReadersAndWritelock}(
Expand All @@ -76,7 +72,7 @@ function ConcurrentUtils.acquire_read(rwlock::ReadWriteLock)
end
end

function ConcurrentUtils.release_read(rwlock::ReadWriteLock)
function ConcurrentUtils.unlock_read(rwlock::ReadWriteLock)

# Using hardware FAA
ptr = Ptr{NReadersAndWritelock}(
Expand Down

0 comments on commit b131065

Please sign in to comment.