Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sleep(n) and Timer creation more MT-safe #32174

Merged
merged 7 commits into from
Jun 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 4 additions & 14 deletions base/asyncevent.jl
Original file line number Diff line number Diff line change
Expand Up @@ -87,21 +87,11 @@ mutable struct Timer
interval ≥ 0 || throw(ArgumentError("timer cannot have negative repeat interval of $interval seconds"))

this = new(Libc.malloc(_sizeof_uv_timer), ThreadSynchronizer(), true)
err = ccall(:uv_timer_init, Cint, (Ptr{Cvoid}, Ptr{Cvoid}), eventloop(), this)
if err != 0
#TODO: this codepath is currently not tested
Libc.free(this.handle)
this.handle = C_NULL
throw(_UVError("uv_timer_init", err))
end

associate_julia_struct(this.handle, this)
finalizer(uvfinalize, this)

ccall(:uv_update_time, Cvoid, (Ptr{Cvoid},), eventloop())
ccall(:uv_timer_start, Cint, (Ptr{Cvoid}, Ptr{Cvoid}, UInt64, UInt64),
this, uv_jl_timercb::Ptr{Cvoid},
ccall(:jl_uv_update_timer_start, Cvoid,
(Ptr{Cvoid}, Any, Ptr{Cvoid}, Ptr{Cvoid}, UInt64, UInt64),
eventloop(), this, this.handle, uv_jl_timercb::Ptr{Cvoid},
UInt64(round(timeout * 1000)) + 1, UInt64(round(interval * 1000)))
finalizer(uvfinalize, this)
return this
end
end
Expand Down
17 changes: 17 additions & 0 deletions src/jl_uv.c
Original file line number Diff line number Diff line change
Expand Up @@ -1085,6 +1085,23 @@ JL_DLLEXPORT int jl_queue_work(work_cb_t work_func, void *work_args, void *work_
return 0;
}

JL_DLLEXPORT void jl_uv_update_timer_start(uv_loop_t* loop, jl_value_t* jltimer,
uv_timer_t* uvtimer, uv_timer_cb cb,
uint64_t timeout, uint64_t repeat)
{
JL_UV_LOCK();
int err = uv_timer_init(loop, uvtimer);
if (err)
abort();

jl_uv_associate_julia_struct((uv_handle_t*)uvtimer, jltimer);
uv_update_time(loop);
err = uv_timer_start(uvtimer, cb, timeout, repeat);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

similarly, this can't actually fail either, we can just abort if it returns non-zero

if (err)
abort();
JL_UV_UNLOCK();
}

JL_DLLEXPORT void jl_uv_stop(uv_loop_t* loop)
{
JL_UV_LOCK();
Expand Down