Project

General

Profile

Feature #8658 » clock_gettime.patch

akr (Akira Tanaka), 07/19/2013 09:32 PM

View differences:

test/ruby/test_process.rb (working copy)
end
end if windows?
def test_clock_gettime
begin
t1 = Process.clock_gettime(Process::CLOCK_REALTIME)
rescue NotImplementedError
return
end
t2 = Time.now
t2 = t2.tv_sec * 1000000000 + t2.tv_nsec
assert_operator(t1, :<=, t2)
t1 = Time.now
t1 = t1.tv_sec * 1000000000 + t1.tv_nsec
t2 = Process.clock_gettime(Process::CLOCK_REALTIME)
assert_operator(t1, :<=, t2)
end
end
process.c (working copy)
#define rb_proc_times rb_f_notimplement
#endif
#if defined(HAVE_CLOCK_GETTIME)
/*
* call-seq:
* Process.clock_gettime(clk_id) -> integer
*
* Returns a number of nanoseconds returned by POSIX clock_gettime() function.
*
* _clk_id_ specifies a kind of clock.
* It is specifed as a constant which begins with <code>Process::CLOCK_</code>
* such like <code>Process::CLOCK_REALTIME</code> and
* <code>Process::CLOCK_MONOTONIC</code>.
* The supported constants depends on OS and version.
* Ruby provides following type of _clk_id_ if available.
*
* [CLOCK_REALTIME] POSIX, Linux, FreeBSD, NetBSD
* [CLOCK_MONOTONIC] POSIX, Linux, FreeBSD, NetBSD
* [CLOCK_PROCESS_CPUTIME_ID] POSIX, Linux
* [CLOCK_THREAD_CPUTIME_ID] POSIX, Linux
*
* The origin (zero) of the returned value, number of nanoseconds,
* varies.
* For example, system start up time, process start up time, the Epoch, etc.
*
* The origin in CLOCK_REALTIME is defined as the Epoch
* (1970-01-01 00:00:00 UTC).
* But some systems count leap seconds and others doesn't.
* So the number of nanoseconds can be interpreted differently across systems.
*
* p Process.clock_gettime(Process::CLOCK_MONOTONIC)
* #=> 2684827897652283
*
*/
VALUE
rb_clock_gettime(VALUE obj, VALUE clk_id)
{
clockid_t c;
struct timespec ts;
int ret;
c = NUM2CLOCKID(clk_id);
ret = clock_gettime(c, &ts);
if (ret == -1)
rb_sys_fail(0);
#if defined(HAVE_LONG_LONG)
if (!MUL_OVERFLOW_SIGNED_INTEGER_P(1000000000, (LONG_LONG)ts.tv_sec,
LLONG_MIN, LLONG_MAX-999999999)) {
return LL2NUM(ts.tv_nsec + 1000000000 * (LONG_LONG)ts.tv_sec);
}
#endif
return rb_funcall(LONG2FIX(ts.tv_nsec), '+', 1,
rb_funcall(LONG2FIX(1000000000), '*', 1, TIMET2NUM(ts.tv_sec)));
}
#else
#define rb_clock_gettime rb_f_notimplement
#endif
VALUE rb_mProcess;
VALUE rb_mProcUID;
VALUE rb_mProcGID;
......
rb_define_module_function(rb_mProcess, "times", rb_proc_times, 0);
#ifdef CLOCK_REALTIME
rb_define_const(rb_mProcess, "CLOCK_REALTIME", CLOCKID2NUM(CLOCK_REALTIME));
#endif
#ifdef CLOCK_MONOTONIC
rb_define_const(rb_mProcess, "CLOCK_MONOTONIC", CLOCKID2NUM(CLOCK_MONOTONIC));
#endif
#ifdef CLOCK_PROCESS_CPUTIME_ID
rb_define_const(rb_mProcess, "CLOCK_PROCESS_CPUTIME_ID", CLOCKID2NUM(CLOCK_PROCESS_CPUTIME_ID));
#endif
#ifdef CLOCK_THREAD_CPUTIME_ID
rb_define_const(rb_mProcess, "CLOCK_THREAD_CPUTIME_ID", CLOCKID2NUM(CLOCK_THREAD_CPUTIME_ID));
#endif
rb_define_module_function(rb_mProcess, "clock_gettime", rb_clock_gettime, 1);
#if defined(HAVE_TIMES) || defined(_WIN32)
rb_cProcessTms = rb_struct_define("Tms", "utime", "stime", "cutime", "cstime", NULL);
#endif
configure.in (working copy)
@%:@include <sys/resource.h>
])
RUBY_REPLACE_TYPE(off_t, [], OFFT)
RUBY_REPLACE_TYPE(clockid_t, [], CLOCKID)
AC_CACHE_CHECK(for prototypes, rb_cv_have_prototypes,
[AC_TRY_COMPILE([int foo(int x) { return 0; }], [return foo(10);],
(1-1/4)