Skip to content

Commit

Permalink
preserve return values
Browse files Browse the repository at this point in the history
  • Loading branch information
vchuravy committed Nov 7, 2015
1 parent f39cc4c commit a6ce169
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/event.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ abstract CLEvent <: CLObject

type Event <: CLEvent
id :: CL_event
_callbacks :: ObjectIdDict

function Event(evt_id::CL_event; retain=false)
if retain
@check api.clRetainEvent(evt_id)
end
evt = new(evt_id)
evt = new(evt_id, ObjectIdDict())
finalizer(evt, _finalize)
return evt
end
Expand All @@ -19,12 +20,13 @@ end
type NannyEvent <: CLEvent
id::CL_event
obj::Any
_callbacks :: ObjectIdDict

function NannyEvent(evt_id::CL_event, obj::Any; retain=false)
if retain
@check api.clRetainEvent(evt_id)
end
nanny_evt = new(evt_id, obj)
nanny_evt = new(evt_id, obj, ObjectIdDict())
finalizer(nanny_evt, x -> begin
wait(x)
x.obj = nothing
Expand Down Expand Up @@ -107,6 +109,10 @@ function event_notify(evt_id::CL_event, status::CL_int, payload::Ptr{Ptr{Void}})
nothing
end

function preserve_callback(evt :: CLEvent, cb_arr)
evt._callbacks[cb_arr] = 0
end


function add_callback(evt::CLEvent, callback::Function)
event_notify_ptr = cfunction(event_notify, Void,
Expand All @@ -116,6 +122,7 @@ function add_callback(evt::CLEvent, callback::Function)
status = Ref{CL_int}(0)
cb = Base.SingleAsyncWork(data -> callback(evt_id[], status[]))
ptrs = [cb.handle, Base.unsafe_convert(Ptr, evt_id), Base.unsafe_convert(Ptr, status)]
preserve_callback(evt, ptrs)

@check api.clSetEventCallback(evt.id, CL_COMPLETE, event_notify_ptr, ptrs)
end
Expand Down

0 comments on commit a6ce169

Please sign in to comment.