Skip to content

Commit

Permalink
Update syntax for Julia v1
Browse files Browse the repository at this point in the history
  • Loading branch information
nickrobinson251 committed Aug 12, 2019
1 parent 4dc900b commit 8e38292
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 112 deletions.
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
18 changes: 13 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,20 @@ os:
- linux
- osx
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.

48 changes: 19 additions & 29 deletions src/ReadWriteLocks.jl
Original file line number Diff line number Diff line change
@@ -1,42 +1,32 @@
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-"
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
mutable struct ReadWriteLock <: AbstractLock
readers::Int
writer::Bool
lock::Mutex # reentrant mutex
lock::ReentrantLock # reentrant mutex
condition::Condition
read_lock::ReadLock
write_lock::WriteLock

function ReadWriteLock()
rwlock = new(false, 0, Mutex(), Condition())
rwlock = new(false, 0, ReentrantLock(), Condition())
rwlock.read_lock = ReadLock(rwlock)
rwlock.write_lock = WriteLock(rwlock)

Expand All @@ -49,7 +39,7 @@ write_lock(rwlock::ReadWriteLock) = rwlock.write_lock

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

try
while rwlock.writer
Expand All @@ -58,31 +48,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 +81,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.

8 changes: 6 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
# write your own tests here
include("singlethreaded.jl")
using ReadWriteLocks
using Test

@testset "ReadWriteLocks" begin
include("singlethreaded.jl")
end
Loading

0 comments on commit 8e38292

Please sign in to comment.