clock_gettime.patch

Akira Tanaka, 07/19/2013 09:32 PM

Download (4.11 KB)

View differences:

test/ruby/test_process.rb (working copy)
1658 1658
    end
1659 1659
  end if windows?
1660 1660

  
1661
  def test_clock_gettime
1662
    begin
1663
      t1 = Process.clock_gettime(Process::CLOCK_REALTIME)
1664
    rescue NotImplementedError
1665
      return
1666
    end
1667
    t2 = Time.now
1668
    t2 = t2.tv_sec * 1000000000 + t2.tv_nsec
1669
    assert_operator(t1, :<=, t2)
1670

  
1671
    t1 = Time.now
1672
    t1 = t1.tv_sec * 1000000000 + t1.tv_nsec
1673
    t2 = Process.clock_gettime(Process::CLOCK_REALTIME)
1674
    assert_operator(t1, :<=, t2)
1675
  end
1676

  
1661 1677
end
process.c (working copy)
6627 6627
#define rb_proc_times rb_f_notimplement
6628 6628
#endif
6629 6629

  
6630
#if defined(HAVE_CLOCK_GETTIME)
6631
/*
6632
 *  call-seq:
6633
 *     Process.clock_gettime(clk_id)   -> integer
6634
 *
6635
 *  Returns a number of nanoseconds returned by POSIX clock_gettime() function.
6636
 *
6637
 *  _clk_id_ specifies a kind of clock.
6638
 *  It is specifed as a constant which begins with <code>Process::CLOCK_</code>
6639
 *  such like <code>Process::CLOCK_REALTIME</code> and
6640
 *  <code>Process::CLOCK_MONOTONIC</code>.
6641
 *  The supported constants depends on OS and version.
6642
 *  Ruby provides following type of _clk_id_ if available.
6643
 *
6644
 *  [CLOCK_REALTIME] POSIX, Linux, FreeBSD, NetBSD
6645
 *  [CLOCK_MONOTONIC] POSIX, Linux, FreeBSD, NetBSD
6646
 *  [CLOCK_PROCESS_CPUTIME_ID] POSIX, Linux
6647
 *  [CLOCK_THREAD_CPUTIME_ID] POSIX, Linux
6648
 *
6649
 *  The origin (zero) of the returned value, number of nanoseconds,
6650
 *  varies.
6651
 *  For example, system start up time, process start up time, the Epoch, etc.
6652
 *
6653
 *  The origin in CLOCK_REALTIME is defined as the Epoch
6654
 *  (1970-01-01 00:00:00 UTC).
6655
 *  But some systems count leap seconds and others doesn't.
6656
 *  So the number of nanoseconds can be interpreted differently across systems.
6657
 *
6658
 *    p Process.clock_gettime(Process::CLOCK_MONOTONIC)
6659
 *    #=> 2684827897652283
6660
 *
6661
 */
6662
VALUE
6663
rb_clock_gettime(VALUE obj, VALUE clk_id)
6664
{
6665
    clockid_t c;
6666
    struct timespec ts;
6667
    int ret;
6668

  
6669
    c = NUM2CLOCKID(clk_id);
6670
    ret = clock_gettime(c, &ts);
6671
    if (ret == -1)
6672
	rb_sys_fail(0);
6673
#if defined(HAVE_LONG_LONG)
6674
    if (!MUL_OVERFLOW_SIGNED_INTEGER_P(1000000000, (LONG_LONG)ts.tv_sec,
6675
                LLONG_MIN, LLONG_MAX-999999999)) {
6676
        return LL2NUM(ts.tv_nsec + 1000000000 * (LONG_LONG)ts.tv_sec);
6677
    }
6678
#endif
6679
    return rb_funcall(LONG2FIX(ts.tv_nsec), '+', 1,
6680
            rb_funcall(LONG2FIX(1000000000), '*', 1, TIMET2NUM(ts.tv_sec)));
6681
}
6682
#else
6683
#define rb_clock_gettime rb_f_notimplement
6684
#endif
6685

  
6630 6686
VALUE rb_mProcess;
6631 6687
VALUE rb_mProcUID;
6632 6688
VALUE rb_mProcGID;
......
6885 6941

  
6886 6942
    rb_define_module_function(rb_mProcess, "times", rb_proc_times, 0);
6887 6943

  
6944
#ifdef CLOCK_REALTIME
6945
    rb_define_const(rb_mProcess, "CLOCK_REALTIME", CLOCKID2NUM(CLOCK_REALTIME));
6946
#endif
6947
#ifdef CLOCK_MONOTONIC
6948
    rb_define_const(rb_mProcess, "CLOCK_MONOTONIC", CLOCKID2NUM(CLOCK_MONOTONIC));
6949
#endif
6950
#ifdef CLOCK_PROCESS_CPUTIME_ID
6951
    rb_define_const(rb_mProcess, "CLOCK_PROCESS_CPUTIME_ID", CLOCKID2NUM(CLOCK_PROCESS_CPUTIME_ID));
6952
#endif
6953
#ifdef CLOCK_THREAD_CPUTIME_ID
6954
    rb_define_const(rb_mProcess, "CLOCK_THREAD_CPUTIME_ID", CLOCKID2NUM(CLOCK_THREAD_CPUTIME_ID));
6955
#endif
6956
    rb_define_module_function(rb_mProcess, "clock_gettime", rb_clock_gettime, 1);
6957

  
6888 6958
#if defined(HAVE_TIMES) || defined(_WIN32)
6889 6959
    rb_cProcessTms = rb_struct_define("Tms", "utime", "stime", "cutime", "cstime", NULL);
6890 6960
#endif
configure.in (working copy)
1262 1262
@%:@include <sys/resource.h>
1263 1263
])
1264 1264
RUBY_REPLACE_TYPE(off_t, [], OFFT)
1265
RUBY_REPLACE_TYPE(clockid_t, [], CLOCKID)
1265 1266

  
1266 1267
AC_CACHE_CHECK(for prototypes, rb_cv_have_prototypes,
1267 1268
  [AC_TRY_COMPILE([int foo(int x) { return 0; }], [return foo(10);],