Feature #15609 ยป ruby-changes.patch
| process.c | ||
|---|---|---|
|
/*
|
||
|
* call-seq:
|
||
|
* sleep([duration]) -> integer
|
||
|
* sleep([duration]) -> integer/float
|
||
|
*
|
||
|
* Suspends the current thread for _duration_ seconds (which may be any number,
|
||
|
* including a +Float+ with fractional seconds). Returns the actual number of
|
||
|
* seconds slept (rounded), which may be less than that asked for if another
|
||
|
* thread calls Thread#run. Called without an argument, sleep()
|
||
|
* will sleep forever.
|
||
|
* including a +Float+ with fractional seconds). Returns the actual time slept,
|
||
|
* which may be less than that asked for if another thread calls Thread#run.
|
||
|
* Given an float input, the function returns the precise time slept, up to
|
||
|
* the microsecond; otherwise it returns the seconds slept. Called without an
|
||
|
* argument, sleep() will sleep forever.
|
||
|
*
|
||
|
* Time.new #=> 2008-03-08 19:56:19 +0900
|
||
|
* sleep 1.2 #=> 1
|
||
|
* sleep 1 #=> 1
|
||
|
* Time.new #=> 2008-03-08 19:56:20 +0900
|
||
|
* sleep 1.9 #=> 2
|
||
|
* Time.new #=> 2008-03-08 19:56:22 +0900
|
||
|
* sleep 1.0 #=> 1.xxxxxx
|
||
|
* Time.new #=> 2008-03-08 19:56:21 +0900
|
||
|
*/
|
||
|
static VALUE
|
||
|
rb_f_sleep(int argc, VALUE *argv)
|
||
|
{
|
||
|
time_t beg, end;
|
||
|
struct timeval beg, end;
|
||
|
gettimeofday(&beg, NULL);
|
||
|
beg = time(0);
|
||
|
if (argc == 0) {
|
||
|
rb_thread_sleep_forever();
|
||
|
rb_thread_sleep_forever();
|
||
|
}
|
||
|
else {
|
||
|
rb_check_arity(argc, 0, 1);
|
||
|
rb_thread_wait_for(rb_time_interval(argv[0]));
|
||
|
rb_check_arity(argc, 0, 1);
|
||
|
rb_thread_wait_for(rb_time_interval(argv[0]));
|
||
|
}
|
||
|
end = time(0) - beg;
|
||
|
gettimeofday(&end, NULL);
|
||
|
return INT2FIX(end);
|
||
|
double total_time = end.tv_sec - beg.tv_sec;
|
||
|
switch(TYPE(argv[0])){
|
||
|
default:
|
||
|
return INT2NUM((int)total_time);
|
||
|
case T_FLOAT:
|
||
|
total_time += (end.tv_usec - beg.tv_usec) * 1e-6;
|
||
|
return DBL2NUM(total_time);
|
||
|
}
|
||
|
}
|
||
| test/ruby/test_sleep.rb | ||
|---|---|---|
|
ensure
|
||
|
GC.enable
|
||
|
end
|
||
|
def test_sleep_return
|
||
|
GC.disable
|
||
|
ret = sleep 0
|
||
|
assert_equal(ret, 0)
|
||
|
ret = sleep 0.0
|
||
|
assert_not_equal(ret, 0)
|
||
|
assert_operator(slept, :>=, 0.0)
|
||
|
assert_operator(slept, :<=, 1.0)
|
||
|
ensure
|
||
|
GC.enable
|
||
|
end
|
||
|
end
|
||