Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update syntax for Julia v1 #4

Merged
merged 7 commits into from
Aug 26, 2019
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*.jl.cov
*.jl.*.cov
*.jl.mem
/Manifest.toml
19 changes: 14 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,22 @@ language: julia
os:
- linux
- osx
- windows
julia:
- 0.4
- 1.0
- 1.1
- 1.2
- 1.3
- nightly
matrix:
allow_failures:
- julia: nightly
fast_finish: true
notifications:
email: false
script:
- if [[ -a .git/shallow ]]; then git fetch --unshallow; fi
- julia -e 'Pkg.clone(pwd()); Pkg.build("ReadWriteLocks"); Pkg.test("ReadWriteLocks"; coverage=true)'
after_success:
- julia -e 'cd(Pkg.dir("ReadWriteLocks")); Pkg.add("Coverage"); using Coverage; Codecov.submit(process_folder())'
- julia -e '
using Pkg;
Pkg.add("Coverage");
using Coverage;
Codecov.submit(process_folder())'
13 changes: 13 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name = "ReadWriteLocks"
uuid = "c9a1755d-84d0-58e0-8f48-2f3129b1cb7a"
authors = "Invenia Technical Computing"
version = "0.1.0"

[compat]
julia = "1"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Test"]
1 change: 0 additions & 1 deletion REQUIRE

This file was deleted.

34 changes: 0 additions & 34 deletions appveyor.yml

This file was deleted.

57 changes: 27 additions & 30 deletions src/ReadWriteLocks.jl
Original file line number Diff line number Diff line change
@@ -1,42 +1,39 @@
module ReadWriteLocks

export ReadWriteLock, read_lock, write_lock, lock!, unlock!

if isdefined(Base, :lock!)
import Base: lock!
end

if isdefined(Base, :unlock!)
import Base: unlock!
using Base: lock, unlock
if VERSION < v"1.2.0-DEV.28"
using Base.Threads: AbstractLock
else
using Base: AbstractLock
end

if !isdefined(Base, :Mutex)
typealias Mutex ReentrantLock

lock!(x::Mutex) = lock(x)
unlock!(x::Mutex) = unlock(x)
end

abstract _Lock
export ReadWriteLock, read_lock, write_lock, lock!, unlock!

immutable ReadLock{T<:_Lock}
struct ReadLock{T<:AbstractLock}
rwlock::T
end

immutable WriteLock{T<:_Lock}
struct WriteLock{T<:AbstractLock}
rwlock::T
end

type ReadWriteLock <: _Lock
# Need Julia VERSION > v"1.2.0-DEV.28` to have `ReentrantLock <: AbstractLock`
LockTypes = Union{AbstractLock, ReentrantLock}
nickrobinson251 marked this conversation as resolved.
Show resolved Hide resolved
mutable struct ReadWriteLock{L<:LockTypes} <: AbstractLock
readers::Int
writer::Bool
lock::Mutex # reentrant mutex
lock::L # reentrant mutex
condition::Condition
read_lock::ReadLock
write_lock::WriteLock

function ReadWriteLock()
rwlock = new(false, 0, Mutex(), 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 All @@ -49,7 +46,7 @@ write_lock(rwlock::ReadWriteLock) = rwlock.write_lock

function lock!(read_lock::ReadLock)
Copy link
Member

Choose a reason for hiding this comment

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

This API should be changed to match the AbstractLock API, including the other API functions like trylock.

Copy link
Contributor Author

@nickrobinson251 nickrobinson251 Aug 17, 2019

Choose a reason for hiding this comment

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

You mean also rename lock -> lock! (even though we mutate the rwlock.readers)?

#5

rwlock = read_lock.rwlock
lock!(rwlock.lock)
lock(rwlock.lock)

try
while rwlock.writer
Expand All @@ -58,31 +55,31 @@ function lock!(read_lock::ReadLock)

rwlock.readers += 1
finally
unlock!(rwlock.lock)
unlock(rwlock.lock)
end

return nothing
end

function unlock!(read_lock::ReadLock)
rwlock = read_lock.rwlock
lock!(rwlock.lock)
lock(rwlock.lock)

try
rwlock.readers -= 1
if rwlock.readers == 0
notify(rwlock.condition; all=true)
end
finally
unlock!(rwlock.lock)
unlock(rwlock.lock)
end

return nothing
end

function lock!(write_lock::WriteLock)
rwlock = write_lock.rwlock
lock!(rwlock.lock)
lock(rwlock.lock)

try
while rwlock.readers > 0 || rwlock.writer
Expand All @@ -91,21 +88,21 @@ function lock!(write_lock::WriteLock)

rwlock.writer = true
finally
unlock!(rwlock.lock)
unlock(rwlock.lock)
end

return nothing
end

function unlock!(write_lock::WriteLock)
rwlock = write_lock.rwlock
lock!(rwlock.lock)
lock(rwlock.lock)

try
rwlock.writer = false
notify(rwlock.condition; all=true)
finally
unlock!(rwlock.lock)
unlock(rwlock.lock)
end

return nothing
Expand Down
1 change: 0 additions & 1 deletion test/REQUIRE

This file was deleted.

Loading