Skip to content

Commit

Permalink
Make it clear the finalizers shouldn't be called directly.
Browse files Browse the repository at this point in the history
  • Loading branch information
maleadt committed Apr 7, 2017
1 parent c134775 commit 212836b
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 12 deletions.
5 changes: 3 additions & 2 deletions src/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ CuArray
devptr = Mem.alloc(T, len)

obj = new{T,N}(devptr, shape)
finalizer(obj, free!)
finalizer(obj, _free!)
return obj
end
function (::Type{CuArray{T,N}}){T,N}(shape::NTuple{N,Int}, devptr::DevicePtr{T})
Expand All @@ -52,7 +52,8 @@ end
(::Type{CuArray{T,N}}){T,N,I<:Integer}(dims::NTuple{N,I}) = CuArray{T,N}(Int.(dims))
(::Type{CuArray{T,N}}){T,N,I<:Integer}(dims::Vararg{I,N}) = CuArray{T,N}(Int.(dims))

function free!(a::CuArray)
"""Don't call this method directly, use `finalize(obj)` instead."""
function _free!(a::CuArray)
if isvalid(a.devptr.ctx)
@trace("Finalizing CuArray at $(Base.pointer_from_objref(a))")
Mem.free(a.devptr)
Expand Down
5 changes: 3 additions & 2 deletions src/context.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type CuContext
# this prevents contexts from getting collected, requiring the user to destroy it.
ctx = get!(context_instances, handle) do
obj = new(handle, owned, true)
finalizer(obj, destroy!)
finalizer(obj, _destroy!)
return obj
end

Expand All @@ -71,7 +71,8 @@ function invalidate!(ctx::CuContext)
nothing
end

function destroy!(ctx::CuContext)
"""Don't call this method directly, use `finalize(obj)` instead."""
function _destroy!(ctx::CuContext)
@trace("Finalizing CuContext at $(Base.pointer_from_objref(ctx))")
if !ctx.owned
@trace("Not destroying context $ctx because we don't own it")
Expand Down
5 changes: 3 additions & 2 deletions src/events.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ type CuEvent

ctx = CuCurrentContext()
obj = new(handle_ref[], ctx)
finalizer(obj, destroy!)
finalizer(obj, _destroy!)
return obj
end
end

function destroy!(e::CuEvent)
"""Don't call this method directly, use `finalize(obj)` instead."""
function _destroy!(e::CuEvent)
if isvalid(e.ctx)
@trace("Finalizing CuEvent at $(Base.pointer_from_objref(e))")
@apicall(:cuEventDestroy, (CuEvent_t,), e)
Expand Down
5 changes: 3 additions & 2 deletions src/module.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,13 @@ type CuModule

ctx = CuCurrentContext()
obj = new(handle_ref[], ctx)
finalizer(obj, unload!)
finalizer(obj, _unload!)
return obj
end
end

function unload!(mod::CuModule)
"""Don't call this method directly, use `finalize(obj)` instead."""
function _unload!(mod::CuModule)
if isvalid(mod.ctx)
@trace("Finalizing CuModule at $(Base.pointer_from_objref(mod)))")
@apicall(:cuModuleUnload, (CuModule_t,), mod)
Expand Down
5 changes: 3 additions & 2 deletions src/module/linker.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@ type CuLink

ctx = CuCurrentContext()
obj = new(handle_ref[], ctx, options, optionKeys, optionVals)
finalizer(obj, destroy!)
finalizer(obj, _destroy!)
return obj
end
end

function destroy!(link::CuLink)
"""Don't call this method directly, use `finalize(obj)` instead."""
function _destroy!(link::CuLink)
if isvalid(link.ctx)
@trace("Finalizing CuLink at $(Base.pointer_from_objref(link))")
@apicall(:cuLinkDestroy, (CuLinkState_t,), link)
Expand Down
5 changes: 3 additions & 2 deletions src/stream.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ function CuStream(flags::Integer=0)

ctx = CuCurrentContext()
obj = CuStream(handle_ref[], ctx)
finalizer(obj, destroy!)
finalizer(obj, _destroy!)
return obj
end

function destroy!(s::CuStream)
"""Don't call this method directly, use `finalize(obj)` instead."""
function _destroy!(s::CuStream)
if isvalid(s.ctx)
@trace("Finalizing CuStream at $(Base.pointer_from_objref(s))")
@apicall(:cuStreamDestroy, (CuModule_t,), s)
Expand Down

0 comments on commit 212836b

Please sign in to comment.