Skip to content

Commit

Permalink
Merge pull request #7 from wookay/fix_julia07
Browse files Browse the repository at this point in the history
Compat Julia 0.7
  • Loading branch information
ssfrr authored Aug 15, 2018
2 parents f106c74 + dea4cc2 commit bb5c3ce
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 70 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ deps/src/*.o
deps/usr/lib/pa_ringbuffer.so
deps/usr/lib/pa_ringbuffer.dylib
deps/usr/lib/pa_ringbuffer.dll
deps/build.log
deps/deps.jl
17 changes: 12 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,18 @@ os:
- osx
julia:
- 0.6
- 0.7
- 1.0
- nightly
matrix:
allow_failures:
- julia: nightly
fast_finish: true
notifications:
email: false
# uncomment the following lines to override the default test script
script:
- if [[ -a .git/shallow ]]; then git fetch --unshallow; fi
- julia -e 'Pkg.clone(pwd()); Pkg.build("RingBuffers"); Pkg.test("RingBuffers"; coverage=true)'
after_success:
- julia -e 'cd(Pkg.dir("RingBuffers")); Pkg.add("Coverage"); using Coverage; Codecov.submit(process_folder())'
- julia -e 'VERSION >= v\"0.7.0-\" && using Pkg;
VERSION < v\"0.7.0-\" && cd(Pkg.dir("RingBuffers"));
Pkg.add("Coverage");
using Coverage;
Codecov.submit(process_folder())'
4 changes: 3 additions & 1 deletion REQUIRE
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
julia 0.6.0-pre
julia 0.6
Compat 0.66.0
BinaryProvider 0.3.0
20 changes: 18 additions & 2 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,23 @@ environment:
matrix:
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x86/0.6/julia-0.6-latest-win32.exe"
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x64/0.6/julia-0.6-latest-win64.exe"
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x86/0.7/julia-0.7-latest-win32.exe"
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x64/0.7/julia-0.7-latest-win64.exe"
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x86/1.0/julia-1.0-latest-win32.exe"
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x64/1.0/julia-1.0-latest-win64.exe"
- JULIA_URL: "https://julialangnightlies-s3.julialang.org/bin/winnt/x86/julia-latest-win32.exe"
- JULIA_URL: "https://julialangnightlies-s3.julialang.org/bin/winnt/x64/julia-latest-win64.exe"

branches:
only:
- master
- /release-.*/

matrix:
allow_failures:
- JULIA_URL: "https://julialangnightlies-s3.julialang.org/bin/winnt/x86/julia-latest-win32.exe"
- JULIA_URL: "https://julialangnightlies-s3.julialang.org/bin/winnt/x64/julia-latest-win64.exe"

notifications:
- provider: Email
on_build_success: false
Expand All @@ -26,7 +37,12 @@ install:
build_script:
# Need to convert from shallow to complete for Pkg.clone to work
- IF EXIST .git\shallow (git fetch --unshallow)
- C:\projects\julia\bin\julia -e "versioninfo(); Pkg.clone(pwd(), \"RingBuffers\"); Pkg.build(\"RingBuffers\")"
- C:\projects\julia\bin\julia -e "VERSION >= v\"0.7.0-\" && using Pkg;
VERSION >= v\"0.7.0-\" && using InteractiveUtils;
versioninfo();
Pkg.clone(pwd(), \"RingBuffers\");
Pkg.build(\"RingBuffers\")"

test_script:
- C:\projects\julia\bin\julia --check-bounds=yes -e "Pkg.test(\"RingBuffers\")"
- C:\projects\julia\bin\julia --check-bounds=yes -e "VERSION >= v\"0.7.0-\" && using Pkg;
Pkg.test(\"RingBuffers\")"
14 changes: 14 additions & 0 deletions deps/build.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using BinaryProvider
using Compat

const verbose = "--verbose" in ARGS
const prefix = Prefix(joinpath(@__DIR__, "usr"))

# BinaryProvider v0.3.2 has libdir(prefix) bug on windows
@static if Compat.Sys.iswindows()
file_path = joinpath(prefix.path, "lib", string("pa_ringbuffer_", Sys.ARCH, "-w64-mingw32.dll"))
product = FileProduct(file_path, :libpa_ringbuffer)
else
product = LibraryProduct(prefix, "pa_ringbuffer", :libpa_ringbuffer)
end
satisfied(product; verbose=verbose) && write_deps_file(joinpath(@__DIR__, "deps.jl"), [product])
20 changes: 12 additions & 8 deletions src/RingBuffers.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__precompile__()
__precompile__(true)

module RingBuffers

Expand All @@ -12,8 +12,12 @@ import Base: unsafe_convert, pointer
import Base: isopen, close

using Base: AsyncCondition
import Compat
import Compat: Libdl, Cvoid, undef, popfirst!, @compat

__init__() = init_pa_ringbuffer()
depsjl = joinpath(@__DIR__, "..", "deps", "deps.jl")
isfile(depsjl) ? include(depsjl) : error("RingBuffers not properly installed. Please run Pkg.build(\"RingBuffers\")")
__init__() = check_deps()

include("pa_ringbuffer.jl")

Expand All @@ -38,7 +42,7 @@ struct RingBuffer{T}
datanotify::AsyncCondition

function RingBuffer{T}(nchannels, frames) where {T}
frames = nextpow2(frames)
frames = nextpow(2, frames)
buf = PaUtilRingBuffer(sizeof(T) * nchannels, frames)
new(buf, nchannels, Condition[], Condition[], AsyncCondition())
end
Expand Down Expand Up @@ -96,7 +100,7 @@ function write(rbuf::RingBuffer{T}, data::AbstractArray{T}, nframes) where {T}
end
finally
# we're done, remove our condition and notify the next writer if necessary
shift!(rbuf.writers)
popfirst!(rbuf.writers)
if length(rbuf.writers) > 0
notify(rbuf.writers[1])
end
Expand Down Expand Up @@ -167,7 +171,7 @@ function flush(rbuf::RingBuffer)

finally
# we're done, remove our condition and notify the next writer if necessary
shift!(rbuf.writers)
popfirst!(rbuf.writers)
if length(rbuf.writers) > 0
notify(rbuf.writers[1])
end
Expand Down Expand Up @@ -224,7 +228,7 @@ function read!(rbuf::RingBuffer{T}, data::AbstractArray{T}, nframes) where {T}
end
finally
# we're done, remove our condition and notify the next reader if necessary
shift!(rbuf.readers)
popfirst!(rbuf.readers)
if length(rbuf.readers) > 0
notify(rbuf.readers[1])
end
Expand Down Expand Up @@ -254,7 +258,7 @@ holding the interleaved data. If the buffer is empty the call will block until
data is available or the ring buffer is closed.
"""
function read(rbuf::RingBuffer{T}, nframes) where {T}
data = Array{T}(rbuf.nchannels, nframes)
data = Array{T}(undef, rbuf.nchannels, nframes)
nread = read!(rbuf, data, nframes)

if nread < nframes
Expand All @@ -273,7 +277,7 @@ available the call will block until it can read more or the ring buffer is
closed.
"""
function read(rbuf::RingBuffer{T}; blocksize=4096) where {T}
readbuf = Array{T}(rbuf.nchannels, blocksize)
readbuf = Array{T}(undef, rbuf.nchannels, blocksize)
# during accumulation we keep the channels separate so we can grow the
# arrays without needing to copy data around as much
cumbufs = [Vector{T}() for _ in 1:rbuf.nchannels]
Expand Down
51 changes: 9 additions & 42 deletions src/pa_ringbuffer.jl
Original file line number Diff line number Diff line change
@@ -1,36 +1,4 @@
function init_pa_ringbuffer()
libdir = joinpath(dirname(@__FILE__), "..", "deps", "usr", "lib")
libsuffix = ""
@static if is_linux() && Sys.ARCH == :x86_64
libsuffix = "x86_64-linux-gnu"
elseif is_linux() && Sys.ARCH == :i686
libsuffix = "i686-linux-gnu"
elseif is_apple() && Sys.ARCH == :x86_64
libsuffix = "x86_64-apple-darwin14"
elseif is_windows() && Sys.ARCH == :x86_64
libsuffix = "x86_64-w64-mingw32"
elseif is_windows() && Sys.ARCH == :i686
libsuffix = "i686-w64-mingw32"
elseif !any(
(sfx) -> isfile(joinpath(libdir, "pa_ringbuffer.$sfx")),
("so", "dll", "dylib"))
error("Unsupported platform $(Sys.MACHINE). You can build your own library by running `make` from $(joinpath(@__FILE__, "..", "deps", "src"))")
end
# if there's a suffix-less library, it was built natively on this machine,
# so load that one first, otherwise load the pre-built one
global const libpa_ringbuffer = Base.Libdl.find_library(
["pa_ringbuffer", "pa_ringbuffer_$libsuffix"],
[libdir])
libpa_ringbuffer == "" && error("Could not load pa_ringbuffer library, please file an issue at https://github.com/JuliaAudio/RingBuffers.jl/issues with your `versioninfo()` output")
# override dlopen flags to make sure we always use `RTLD_GLOBAL` so that the
# library functions are available to other C shim libraries that other
# packages might need to add to handle their audio callbacks.
Libdl.dlopen(libpa_ringbuffer, Libdl.RTLD_LAZY |
Libdl.RTLD_DEEPBIND |
Libdl.RTLD_GLOBAL)
end

@static if is_apple()
@static if Compat.Sys.isapple()
const RingBufferSize = Int32
else
const RingBufferSize = Clong
Expand All @@ -56,7 +24,7 @@ mutable struct PaUtilRingBuffer
rbuf = new()
PaUtil_InitializeRingBuffer(rbuf, elementSizeBytes, elementCount, data)

finalizer(rbuf, close)
@compat finalizer(close, rbuf)
rbuf
end
end
Expand All @@ -79,17 +47,16 @@ Initialize Ring Buffer to empty state ready to have elements written to it.
* `rbuf::PaUtilRingBuffer`: The ring buffer.
* `elementSizeBytes::RingBufferSize`: The size of a single data element in bytes.
* `elementCount::RingBufferSize`: The number of elements in the buffer (must be a power of 2).
* `dataPtr::Ptr{Void}`: A pointer to a previously allocated area where the data
* `dataPtr::Ptr{Cvoid}`: A pointer to a previously allocated area where the data
will be maintained. It must be elementCount*elementSizeBytes long.
"""
function PaUtil_InitializeRingBuffer(rbuf, elementSizeBytes, elementCount, dataPtr)
if !ispow2(elementCount)
throw(ErrorException("elementCount($elementCount) must be a power of 2"))
end

status = ccall((:PaUtil_InitializeRingBuffer, libpa_ringbuffer),
RingBufferSize,
(Ref{PaUtilRingBuffer}, RingBufferSize, RingBufferSize, Ptr{Void}),
(Ref{PaUtilRingBuffer}, RingBufferSize, RingBufferSize, Ptr{Cvoid}),
rbuf, elementSizeBytes, elementCount, dataPtr)
if status != 0
throw(ErrorException("PaUtil_InitializeRingBuffer returned status $status"))
Expand All @@ -106,7 +73,7 @@ Reset buffer to empty. Should only be called when buffer is NOT being read or wr
"""
function PaUtil_FlushRingBuffer(rbuf)
ccall((:PaUtil_FlushRingBuffer, libpa_ringbuffer),
Void,
Cvoid,
(Ref{PaUtilRingBuffer}, ),
rbuf)
end
Expand Down Expand Up @@ -137,28 +104,28 @@ end

"""
PaUtil_WriteRingBuffer(rbuf::PaUtilRingBuffer,
data::Ptr{Void},
data::Ptr{Cvoid},
elementCount::RingBufferSize)
Write data to the ring buffer and return the number of elements written.
"""
function PaUtil_WriteRingBuffer(rbuf, data, elementCount)
ccall((:PaUtil_WriteRingBuffer, libpa_ringbuffer),
RingBufferSize,
(Ref{PaUtilRingBuffer}, Ptr{Void}, RingBufferSize),
(Ref{PaUtilRingBuffer}, Ptr{Cvoid}, RingBufferSize),
rbuf, data, elementCount)
end

"""
PaUtil_ReadRingBuffer(rbuf::PaUtilRingBuffer,
data::Ptr{Void},
data::Ptr{Cvoid},
elementCount::RingBufferSize)
Read data from the ring buffer and return the number of elements read.
"""
function PaUtil_ReadRingBuffer(rbuf, data, elementCount)
ccall((:PaUtil_ReadRingBuffer, libpa_ringbuffer),
RingBufferSize,
(Ref{PaUtilRingBuffer}, Ptr{Void}, RingBufferSize),
(Ref{PaUtilRingBuffer}, Ptr{Cvoid}, RingBufferSize),
rbuf, data, elementCount)
end
1 change: 0 additions & 1 deletion test/REQUIRE
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
TestSetExtensions v1.0.0
26 changes: 15 additions & 11 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using RingBuffers
using TestSetExtensions
using Base.Test
import Compat: undef, fetch
using Compat.Test

@testset ExtendedTestSet "RingBuffer Tests" begin
@testset "RingBuffer Tests" begin
include("pa_ringbuffer.jl")
@testset "Can check frames readable and writable" begin
rb = RingBuffer{Float64}(2, 8)
Expand Down Expand Up @@ -105,7 +105,7 @@ using Base.Test
sleep(0.1)
@test t.state == :runnable
readdata = read(rb, 8)
@test wait(t) == 5
@test fetch(t) == 5
@test t.state == :done
@test readdata == hcat(writedata, writedata[:, 1:3])
end
Expand All @@ -118,7 +118,7 @@ using Base.Test
sleep(0.1)
@test t.state == :runnable
write(rb, writedata)
@test wait(t) == hcat(writedata, writedata)
@test fetch(t) == hcat(writedata, writedata)
@test t.state == :done
end

Expand All @@ -129,8 +129,8 @@ using Base.Test
t2 = @async write(rb, writedata)
sleep(0.1)
close(rb)
@test wait(t1) == 8
@test wait(t2) == 0
@test fetch(t1) == 8
@test fetch(t2) == 0
end

@testset "closing ringbuf cancels in-progress reads" begin
Expand All @@ -141,8 +141,8 @@ using Base.Test
t2 = @async read(rb, 5)
sleep(0.1)
close(rb)
@test wait(t1) == writedata[:, 1:3]
@test wait(t2) == Array{Int}(2, 0)
@test fetch(t1) == writedata[:, 1:3]
@test fetch(t2) == Array{Int}(undef, 2, 0)
end

@testset "writeavailable works with Matrices" begin
Expand All @@ -169,7 +169,11 @@ using Base.Test
end
flush(rb)
close(rb)
@test wait(reader) == repmat(writedata, 1, 4)
if VERSION >= v"0.7.0-DEV.3977" # Julia PR 26039
@test fetch(reader) == repeat(writedata, 1, 4)
else
@test fetch(reader) == Compat.repmat(writedata, 1, 4)
end
end

@testset "flush works if we're queued behind a writer" begin
Expand All @@ -179,7 +183,7 @@ using Base.Test
flusher = @async flush(rb)
writer2 = @async write(rb, writedata)
read(rb, 16)
wait(flusher)
fetch(flusher)
# as long as this gets through then we should be OK that the tasks
# woke each other up
@test true
Expand Down

0 comments on commit bb5c3ce

Please sign in to comment.