Skip to content

Commit

Permalink
Make sleep(n) and Timer creation more MT-safe (#32174)
Browse files Browse the repository at this point in the history
fixes #32152
  • Loading branch information
jpsamaroo authored and JeffBezanson committed Jun 4, 2019
1 parent 7133250 commit b6f1be5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
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);
if (err)
abort();
JL_UV_UNLOCK();
}

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

1 comment on commit b6f1be5

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

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

Executing the daily benchmark build, I will reply here when finished:

@nanosoldier runbenchmarks(ALL, isdaily = true)

Please sign in to comment.