From 8e38292891518d22860c26a95b15992f1958f50b Mon Sep 17 00:00:00 2001 From: Nick Robinson Date: Mon, 12 Aug 2019 16:36:10 +0100 Subject: [PATCH] Update syntax for Julia v1 --- .gitignore | 1 + .travis.yml | 18 +++-- Project.toml | 13 ++++ REQUIRE | 1 - src/ReadWriteLocks.jl | 48 ++++++------- test/REQUIRE | 1 - test/runtests.jl | 8 ++- test/singlethreaded.jl | 149 +++++++++++++++++++++-------------------- 8 files changed, 127 insertions(+), 112 deletions(-) create mode 100644 Project.toml delete mode 100644 REQUIRE delete mode 100644 test/REQUIRE diff --git a/.gitignore b/.gitignore index 8c960ec..97e6a6f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ *.jl.cov *.jl.*.cov *.jl.mem +/Manifest.toml diff --git a/.travis.yml b/.travis.yml index 1121e9a..890561b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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())' diff --git a/Project.toml b/Project.toml new file mode 100644 index 0000000..cbafd38 --- /dev/null +++ b/Project.toml @@ -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"] diff --git a/REQUIRE b/REQUIRE deleted file mode 100644 index d5d6467..0000000 --- a/REQUIRE +++ /dev/null @@ -1 +0,0 @@ -julia 0.4 diff --git a/src/ReadWriteLocks.jl b/src/ReadWriteLocks.jl index 1a18ac3..08e1e04 100644 --- a/src/ReadWriteLocks.jl +++ b/src/ReadWriteLocks.jl @@ -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) @@ -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 @@ -58,7 +48,7 @@ function lock!(read_lock::ReadLock) rwlock.readers += 1 finally - unlock!(rwlock.lock) + unlock(rwlock.lock) end return nothing @@ -66,7 +56,7 @@ end function unlock!(read_lock::ReadLock) rwlock = read_lock.rwlock - lock!(rwlock.lock) + lock(rwlock.lock) try rwlock.readers -= 1 @@ -74,7 +64,7 @@ function unlock!(read_lock::ReadLock) notify(rwlock.condition; all=true) end finally - unlock!(rwlock.lock) + unlock(rwlock.lock) end return nothing @@ -82,7 +72,7 @@ end function lock!(write_lock::WriteLock) rwlock = write_lock.rwlock - lock!(rwlock.lock) + lock(rwlock.lock) try while rwlock.readers > 0 || rwlock.writer @@ -91,7 +81,7 @@ function lock!(write_lock::WriteLock) rwlock.writer = true finally - unlock!(rwlock.lock) + unlock(rwlock.lock) end return nothing @@ -99,13 +89,13 @@ 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 diff --git a/test/REQUIRE b/test/REQUIRE deleted file mode 100644 index bc3e234..0000000 --- a/test/REQUIRE +++ /dev/null @@ -1 +0,0 @@ -FactCheck diff --git a/test/runtests.jl b/test/runtests.jl index 962b460..cc28767 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,2 +1,6 @@ -# write your own tests here -include("singlethreaded.jl") +using ReadWriteLocks +using Test + +@testset "ReadWriteLocks" begin + include("singlethreaded.jl") +end diff --git a/test/singlethreaded.jl b/test/singlethreaded.jl index 5564f03..281b16b 100644 --- a/test/singlethreaded.jl +++ b/test/singlethreaded.jl @@ -1,58 +1,59 @@ -using FactCheck using ReadWriteLocks +using Test -facts("Single-threaded tests") do - context("Initialization") do +@testset "Single-threaded tests" begin + @testset "Initialization" begin rwlock = ReadWriteLock() - @fact rwlock.read_lock.rwlock --> rwlock - @fact rwlock.write_lock.rwlock --> rwlock - @fact rwlock.readers --> 0 - @fact rwlock.writer --> false - @fact read_lock(rwlock) --> rwlock.read_lock - @fact write_lock(rwlock) --> rwlock.write_lock + @test rwlock.read_lock.rwlock == rwlock + @test rwlock.write_lock.rwlock == rwlock + @test rwlock.readers == 0 + @test rwlock.writer == false + @test read_lock(rwlock) == rwlock.read_lock + @test write_lock(rwlock) == rwlock.write_lock end end -facts("Two-threaded tests") do - context("Read locks") do +@testset "Two-threaded tests" begin + @testset "Read locks" begin NUM_LOCKS = 10 - context("$NUM_LOCKS locks") do + @testset "$NUM_LOCKS locks" begin rwlock = ReadWriteLock() rlock = read_lock(rwlock) @async begin - @fact rwlock.writer --> false - @fact rwlock.readers --> 0 + @test rwlock.writer == false + @test rwlock.readers == 0 for i = 1:NUM_LOCKS - @fact lock!(rlock) --> nothing - @fact rwlock.readers --> i + @test lock!(rlock) == nothing + # lock(rlock) + @test rwlock.readers == i end end sleep(1) - @fact rwlock.readers --> NUM_LOCKS + @test rwlock.readers == NUM_LOCKS @async begin - @fact rwlock.writer --> false - @fact rwlock.readers --> NUM_LOCKS - for i = NUM_LOCKS:-1:1 - @fact unlock!(rlock) --> nothing - @fact rwlock.readers --> i - 1 + @test rwlock.writer == false + @test rwlock.readers == NUM_LOCKS + for i in NUM_LOCKS:-1:1 + @test unlock!(rlock) == nothing + @test rwlock.readers == i - 1 end - @fact rwlock.readers --> 0 + @test rwlock.readers == 0 end sleep(1) - @fact rwlock.readers --> 0 + @test rwlock.readers == 0 end end - context("Write locks") do - context("two locks") do + @testset "Write locks" begin + @testset "two locks" begin rwlock = ReadWriteLock() wlock = write_lock(rwlock) @@ -60,30 +61,30 @@ facts("Two-threaded tests") do put!(c, :pretest) @async begin - @fact rwlock.writer --> false - @fact rwlock.readers --> 0 - @fact lock!(wlock) --> nothing - @fact rwlock.writer --> true - @fact rwlock.readers --> 0 - @fact take!(c) --> :pretest + @test rwlock.writer == false + @test rwlock.readers == 0 + @test lock!(wlock) == nothing + @test rwlock.writer == true + @test rwlock.readers == 0 + @test take!(c) == :pretest put!(c, :prelock) - @fact lock!(wlock) --> nothing + @test lock!(wlock) == nothing # this code should never be reached - @fact take!(c) --> :prelock + @test take!(c) == :prelock put!(c, :postlock) end sleep(1) - @fact take!(c) --> :prelock + @test take!(c) == :prelock put!(c, :posttest) - @fact rwlock.writer --> true - @fact rwlock.readers --> 0 + @test rwlock.writer == true + @test rwlock.readers == 0 end - context("unlock") do + @testset "unlock" begin rwlock = ReadWriteLock() wlock = write_lock(rwlock) @@ -91,30 +92,30 @@ facts("Two-threaded tests") do put!(c, :pretest) @async begin - @fact rwlock.writer --> false - @fact rwlock.readers --> 0 - @fact lock!(wlock) --> nothing - @fact rwlock.writer --> true - @fact rwlock.readers --> 0 - @fact take!(c) --> :pretest + @test rwlock.writer == false + @test rwlock.readers == 0 + @test lock!(wlock) == nothing + @test rwlock.writer == true + @test rwlock.readers == 0 + @test take!(c) == :pretest put!(c, :preunlock) - @fact unlock!(wlock) --> nothing - @fact take!(c) --> :preunlock + @test unlock!(wlock) == nothing + @test take!(c) == :preunlock put!(c, :postunlock) end sleep(1) - @fact take!(c) --> :postunlock + @test take!(c) == :postunlock put!(c, :posttest) - @fact rwlock.writer --> false - @fact rwlock.readers --> 0 + @test rwlock.writer == false + @test rwlock.readers == 0 end end - context("read and write locks") do - context("write then read") do + @testset "read and write locks" begin + @testset "write then read" begin rwlock = ReadWriteLock() wlock = write_lock(rwlock) rlock = read_lock(rwlock) @@ -123,30 +124,30 @@ facts("Two-threaded tests") do put!(c, :pretest) @async begin - @fact rwlock.writer --> false - @fact rwlock.readers --> 0 - @fact lock!(wlock) --> nothing - @fact rwlock.writer --> true - @fact rwlock.readers --> 0 - @fact take!(c) --> :pretest + @test rwlock.writer == false + @test rwlock.readers == 0 + @test lock!(wlock) == nothing + @test rwlock.writer == true + @test rwlock.readers == 0 + @test take!(c) == :pretest put!(c, :prelock) - @fact lock!(rlock) --> nothing + @test lock!(rlock) == nothing # this code should never be reached - @fact take!(c) --> :prelock + @test take!(c) == :prelock put!(c, :postlock) end sleep(1) - @fact take!(c) --> :prelock + @test take!(c) == :prelock put!(c, :posttest) - @fact rwlock.writer --> true - @fact rwlock.readers --> 0 + @test rwlock.writer == true + @test rwlock.readers == 0 end - context("read then write") do + @testset "read then write" begin rwlock = ReadWriteLock() wlock = write_lock(rwlock) rlock = read_lock(rwlock) @@ -155,27 +156,27 @@ facts("Two-threaded tests") do put!(c, :pretest) @async begin - @fact rwlock.writer --> false - @fact rwlock.readers --> 0 - @fact lock!(rlock) --> nothing - @fact rwlock.writer --> false - @fact rwlock.readers --> 1 - @fact take!(c) --> :pretest + @test rwlock.writer == false + @test rwlock.readers == 0 + @test lock!(rlock) == nothing + @test rwlock.writer == false + @test rwlock.readers == 1 + @test take!(c) == :pretest put!(c, :prelock) - @fact lock!(wlock) --> nothing + @test lock!(wlock) == nothing # this code should never be reached - @fact take!(c) --> :prelock + @test take!(c) == :prelock put!(c, :postlock) end sleep(1) - @fact take!(c) --> :prelock + @test take!(c) == :prelock put!(c, :posttest) - @fact rwlock.writer --> false - @fact rwlock.readers --> 1 + @test rwlock.writer == false + @test rwlock.readers == 1 end end