Thanks Jameson, that worked great.
Since this took me into areas of Julia that I hadn't previously explored, I
thought I'd post a complete example below. I suspect that something along
these lines needs to be added to
http://docs.julialang.org/en/latest/manual/calling-c-and-fortran-
code/#passing-julia-callback-functions-to-c
Thoughts?
--Tim
# Writing thread-safe callback functions for interfacing with
# C libraries. As an example, this uses wait/notify on a condition.
c = Condition()
# Here's your function that you'd like to be the callback function.
# This will get executed indirectly, by scheduling it to run from the
# Julia event loop. It must have two inputs (which will have
# types SingleAsyncWork and Cint, respectively).
runnotify = (data, status) -> notify(c)
# Create an object that can be used to schedule a call of runnotify
# by Julia's event loop
notifyasync = Base.SingleAsyncWork(runnotify)
# Now create a C callback function that will issue uv_async_send, passing
# notifyasync.handle as its input.
# It should be set up to match the expected syntax of the C callback.
# Here we do it for the callback syntax of pthread_create.
async_send_pthread(func::Ptr{Void}) = (ccall(:uv_async_send,Cint,
(Ptr{Void},),func); C_NULL)
const c_async_send_pthread = cfunction(async_send_pthread, Ptr{Void},
(Ptr{Void},))
threads = Uint64[0]
@sync begin
@async begin
println("About to wait from 1")
wait(c)
println("Done waiting from 1")
end
@async begin
println("About to notify from 2")
# Perform the equivalent of notify(c) from a new thread
ccall(:pthread_create, Cint, (Ptr{Void}, Ptr{Void}, Ptr{Void},
Ptr{Void}),
threads, C_NULL, c_async_send_pthread, notifyasync.handle)
ccall(:pthread_join, Cint, (Uint64, Ptr{Void}), threads[1], C_NULL)
println("Done notifying from 2")
end
nothing
end