Skip to content

Commit

Permalink
scheduler-aware sleep(). fixes JuliaLang#2577
Browse files Browse the repository at this point in the history
old sleep renamed usleep(), not exported. not sure if we need it.
  • Loading branch information
JeffBezanson committed Mar 16, 2013
1 parent ac44d6a commit 471828a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
4 changes: 2 additions & 2 deletions base/libc.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
## time-related functions ##

# TODO: check for usleep errors?
@unix_only sleep(s::Real) = ccall(:usleep, Int32, (Uint32,), uint32(iround(s*1e6)))
@windows_only sleep(s::Real) = (ccall(:Sleep, stdcall, Void, (Uint32,), uint32(iround(s*1e3))); return int32(0))
@unix_only usleep(s::Real) = ccall(:usleep, Int32, (Uint32,), uint32(iround(s*1e6)))
@windows_only usleep(s::Real) = (ccall(:Sleep, stdcall, Void, (Uint32,), uint32(iround(s*1e3))); return int32(0))

strftime(t) = strftime("%c", t)
function strftime(fmt::ByteString, t)
Expand Down
12 changes: 12 additions & 0 deletions base/stream.jl
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,18 @@ function stop_timer(timer::TimeoutAsyncWork)
ccall(:jl_timer_stop,Int32,(Ptr{Void},),timer.handle)
end

function sleep(sec::Real)
timer = TimeoutAsyncWork(status->tasknotify([wt], status))
wt = WaitTask(timer, false)
start_timer(timer, iround(sec*1000), 0)
args = yield(wt)
if isa(args,InterruptException)
stop_timer(timer)
error(args)
end
nothing
end

assignIdleAsyncWork(work::IdleAsyncWork,cb::Function) = ccall(:jl_idle_start,Ptr{Void},(Ptr{Void},),work.handle)

function add_idle_cb(loop::Ptr{Void},cb::Function)
Expand Down

0 comments on commit 471828a

Please sign in to comment.