Skip to content

Commit

Permalink
fix threadcall for new ccall lookup strategy (#41345)
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffBezanson authored Jun 28, 2021
1 parent 96b3c5d commit 5f18285
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
15 changes: 8 additions & 7 deletions base/threadcall.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ macro threadcall(f, rettype, argtypes, argvals...)
argvals = map(esc, argvals)

# construct non-allocating wrapper to call C function
wrapper = :(function (args_ptr::Ptr{Cvoid}, retval_ptr::Ptr{Cvoid})
wrapper = :(function (fptr::Ptr{Cvoid}, args_ptr::Ptr{Cvoid}, retval_ptr::Ptr{Cvoid})
p = args_ptr
# the rest of the body is created below
end)
Expand All @@ -42,18 +42,19 @@ macro threadcall(f, rettype, argtypes, argvals...)
push!(body, :(p += Core.sizeof($T)))
push!(args, arg)
end
push!(body, :(ret = ccall($f, $rettype, ($(argtypes...),), $(args...))))
push!(body, :(ret = ccall(fptr, $rettype, ($(argtypes...),), $(args...))))
push!(body, :(unsafe_store!(convert(Ptr{$rettype}, retval_ptr), ret)))
push!(body, :(return Int(Core.sizeof($rettype))))

# return code to generate wrapper function and send work request thread queue
wrapper = Expr(Symbol("hygienic-scope"), wrapper, @__MODULE__)
return :(let fun_ptr = @cfunction($wrapper, Int, (Ptr{Cvoid}, Ptr{Cvoid}))
do_threadcall(fun_ptr, $rettype, Any[$(argtypes...)], Any[$(argvals...)])
return :(let fun_ptr = @cfunction($wrapper, Int, (Ptr{Cvoid}, Ptr{Cvoid}, Ptr{Cvoid}))
# use cglobal to look up the function on the calling thread
do_threadcall(fun_ptr, cglobal($f), $rettype, Any[$(argtypes...)], Any[$(argvals...)])
end)
end

function do_threadcall(fun_ptr::Ptr{Cvoid}, rettype::Type, argtypes::Vector, argvals::Vector)
function do_threadcall(fun_ptr::Ptr{Cvoid}, cfptr::Ptr{Cvoid}, rettype::Type, argtypes::Vector, argvals::Vector)
# generate function pointer
c_notify_fun = @cfunction(
function notify_fun(idx)
Expand Down Expand Up @@ -86,8 +87,8 @@ function do_threadcall(fun_ptr::Ptr{Cvoid}, rettype::Type, argtypes::Vector, arg
GC.@preserve args_arr ret_arr roots begin
# queue up the work to be done
ccall(:jl_queue_work, Cvoid,
(Ptr{Cvoid}, Ptr{UInt8}, Ptr{UInt8}, Ptr{Cvoid}, Cint),
fun_ptr, args_arr, ret_arr, c_notify_fun, idx)
(Ptr{Cvoid}, Ptr{Cvoid}, Ptr{UInt8}, Ptr{UInt8}, Ptr{Cvoid}, Cint),
fun_ptr, cfptr, args_arr, ret_arr, c_notify_fun, idx)

# wait for a result & return it
wait(thread_notifiers[idx])
Expand Down
8 changes: 5 additions & 3 deletions src/jl_uv.c
Original file line number Diff line number Diff line change
Expand Up @@ -966,12 +966,13 @@ JL_DLLEXPORT int jl_tty_set_mode(uv_tty_t *handle, int mode)
return uv_tty_set_mode(handle, mode_enum);
}

typedef int (*work_cb_t)(void *, void *);
typedef int (*work_cb_t)(void *, void *, void *);
typedef void (*notify_cb_t)(int);

struct work_baton {
uv_work_t req;
work_cb_t work_func;
void *ccall_fptr;
void *work_args;
void *work_retval;
notify_cb_t notify_func;
Expand All @@ -985,7 +986,7 @@ struct work_baton {
void jl_work_wrapper(uv_work_t *req)
{
struct work_baton *baton = (struct work_baton*) req->data;
baton->work_func(baton->work_args, baton->work_retval);
baton->work_func(baton->ccall_fptr, baton->work_args, baton->work_retval);
}

void jl_work_notifier(uv_work_t *req, int status)
Expand All @@ -995,12 +996,13 @@ void jl_work_notifier(uv_work_t *req, int status)
free(baton);
}

JL_DLLEXPORT int jl_queue_work(work_cb_t work_func, void *work_args, void *work_retval,
JL_DLLEXPORT int jl_queue_work(work_cb_t work_func, void *ccall_fptr, void *work_args, void *work_retval,
notify_cb_t notify_func, int notify_idx)
{
struct work_baton *baton = (struct work_baton*)malloc_s(sizeof(struct work_baton));
baton->req.data = (void*) baton;
baton->work_func = work_func;
baton->ccall_fptr = ccall_fptr;
baton->work_args = work_args;
baton->work_retval = work_retval;
baton->notify_func = notify_func;
Expand Down

0 comments on commit 5f18285

Please sign in to comment.