Project

General

Profile

Feature #15609 ยป ruby-changes.patch

gabewild37 (Gabe Wild), 08/13/2019 04:26 PM

View differences:

process.c
4823 4823

  
4824 4824
/*
4825 4825
 *  call-seq:
4826
 *     sleep([duration])    -> integer
4826
 *     sleep([duration])    -> integer/float
4827 4827
 *
4828 4828
 *  Suspends the current thread for _duration_ seconds (which may be any number,
4829
 *  including a +Float+ with fractional seconds). Returns the actual number of
4830
 *  seconds slept (rounded), which may be less than that asked for if another
4831
 *  thread calls Thread#run. Called without an argument, sleep()
4832
 *  will sleep forever.
4829
 *  including a +Float+ with fractional seconds). Returns the actual time slept, 
4830
 *  which may be less than that asked for if another thread calls Thread#run. 
4831
 *  Given an float input, the function returns the precise time slept, up to 
4832
 *  the microsecond; otherwise it returns the seconds slept. Called without an 
4833
 *  argument, sleep() will sleep forever.
4833 4834
 *
4834 4835
 *     Time.new    #=> 2008-03-08 19:56:19 +0900
4835
 *     sleep 1.2   #=> 1
4836
 *     sleep 1     #=> 1
4836 4837
 *     Time.new    #=> 2008-03-08 19:56:20 +0900
4837
 *     sleep 1.9   #=> 2
4838
 *     Time.new    #=> 2008-03-08 19:56:22 +0900
4838
 *     sleep 1.0   #=> 1.xxxxxx
4839
 *     Time.new    #=> 2008-03-08 19:56:21 +0900
4839 4840
 */
4840 4841

  
4841 4842
static VALUE
4842 4843
rb_f_sleep(int argc, VALUE *argv)
4843 4844
{
4844
    time_t beg, end;
4845
    struct timeval beg, end;
4846
    gettimeofday(&beg, NULL);
4845 4847

  
4846
    beg = time(0);
4847 4848
    if (argc == 0) {
4848
	rb_thread_sleep_forever();
4849
        rb_thread_sleep_forever();
4849 4850
    }
4850 4851
    else {
4851
	rb_check_arity(argc, 0, 1);
4852
	rb_thread_wait_for(rb_time_interval(argv[0]));
4852
        rb_check_arity(argc, 0, 1);
4853
        rb_thread_wait_for(rb_time_interval(argv[0]));
4853 4854
    }
4854 4855

  
4855
    end = time(0) - beg;
4856
    gettimeofday(&end, NULL);
4856 4857

  
4857
    return INT2FIX(end);
4858
    double total_time = end.tv_sec - beg.tv_sec;
4859
    switch(TYPE(argv[0])){
4860
        default:
4861
            return INT2NUM((int)total_time);
4862
        case T_FLOAT: 
4863
            total_time += (end.tv_usec - beg.tv_usec) * 1e-6; 
4864
            return DBL2NUM(total_time);
4865

  
4866
    }
4858 4867
}
4859 4868

  
4860 4869

  
test/ruby/test_sleep.rb
14 14
  ensure
15 15
    GC.enable
16 16
  end
17

  
18
  def test_sleep_return
19
    GC.disable
20
    ret = sleep 0
21
    assert_equal(ret, 0)
22
    ret = sleep 0.0 
23
    assert_not_equal(ret, 0)
24
    assert_operator(slept, :>=, 0.0)
25
    assert_operator(slept, :<=, 1.0)
26

  
27
  ensure
28
    GC.enable
29
  end
17 30
end