Segmentation fault on threadcall of cfunction from Julia function

multithreading

#1

Hi,

I have a blocking function in a C++ library that I have wrapped using CxxWrap. I would like to start a thread calling this blocking function, so no Julia thread is blocked, using uv_async_send to signal a Julia async task whenever the blocking C++ function completes.

I have tried keeping as much code as possible in Julia, by writing a Julia function blocking_fn calling uv_async_send, obtaining a cfunction, and passing this to @threadcall - unfortunately this segfaults:

function blocking_fn(handle::Ptr{Nothing})
    uv_async_send_result = ccall(:uv_async_send, Cint, (Ptr{Nothing}, ), handle)
    return uv_async_send_result
end

cond = Base.AsyncCondition()

@sync begin
    @async begin
        for i = 1:3
            println("T1 Waiting")
            wait(cond)
            println("T1 Processing")
            sleep(0.1)
        end
    end
    @async begin
        for i = 1:3
            println("T2 Waiting")
            sleep(0.1)
            println("T2 Ready")
            # @threadcall(:uv_async_send, Cint, (Ptr{Nothing}, ), cond.handle)
            let blocking_fn_c = @cfunction(blocking_fn, Cint, (Ptr{Nothing},))
                GC.@preserve blocking_fn_c @threadcall(Base.unsafe_convert(Ptr{Cvoid}, blocking_fn_c), Cint, (Ptr{Nothing},), cond.handle)
            end
        end
    end
end

@threadcall'ing uv_async_send directly seems to work.

Is this just something which have to wait for the parallel task runtime (PTR) to be done?

See also Equiavalent of @threadcall but for Julia code?