class SleepyPenguin::TimerFD

TimerFD exposes kernel timers as IO objects that may be monitored by IO.select or Epoll. IO#close disarms the timers and returns resources back to the kernel.

Public Class Methods

new([clockid[, flags]]) → TimerFD IO object click to toggle source

Creates a new timer as an IO object.

If set clockid must be be one of the following:

  • :REALTIME - use the settable clock

  • :MONOTONIC - use the non-settable clock unaffected by manual changes

clockid defaults to :MONOTONIC if unspecified flags may be any or none of the following:

  • :CLOEXEC - set the close-on-exec flag on the new object

  • :NONBLOCK - set the non-blocking I/O flag on the new object

static VALUE s_new(int argc, VALUE *argv, VALUE klass)
{
        VALUE cid, fl, rv;
        int clockid, flags;
        int fd;

        rb_scan_args(argc, argv, "02", &cid, &fl);
        clockid = NIL_P(cid) ? CLOCK_MONOTONIC : rb_sp_get_flags(klass, cid, 0);
        flags = rb_sp_get_flags(klass, fl, RB_SP_CLOEXEC(TFD_CLOEXEC));

        fd = timerfd_create(clockid, flags);
        if (fd < 0) {
                if (rb_sp_gc_for_fd(errno))
                        fd = timerfd_create(clockid, flags);
                if (fd < 0)
                        rb_sys_fail("timerfd_create");
        }

        rv = INT2FIX(fd);
        return rb_call_super(1, &rv);
}

Public Instance Methods

expirations([nonblock]) → Integer click to toggle source

Returns the number of expirations that have occurred. This will block if no expirations have occurred at the time of the call. Returns nil if nonblock is passed and is true

static VALUE expirations(int argc, VALUE *argv, VALUE self)
{
        ssize_t r;
        int fd = rb_sp_fileno(self);
        uint64_t buf = (uint64_t)fd;
        VALUE nonblock;

        rb_scan_args(argc, argv, "01", &nonblock);
        if (RTEST(nonblock))
                rb_sp_set_nonblock(fd);
retry:
        r = (ssize_t)rb_sp_fd_region(tfd_read, &buf, fd);
        if (r < 0) {
                if (errno == EAGAIN && RTEST(nonblock))
                        return Qnil;
                if (rb_sp_wait(rb_io_wait_readable, self, &fd))
                        goto retry;
                rb_sys_fail("read(timerfd)");
        }

        return ULL2NUM(buf);
}
tfd#gettime → [ interval, value ] click to toggle source

Returns the current interval and value of the timer as an Array.

static VALUE gettime(VALUE self)
{
        int fd = rb_sp_fileno(self);
        struct itimerspec curr;

        if (timerfd_gettime(fd, &curr) < 0)
                rb_sys_fail("timerfd_gettime");

        return itimerspec2ary(&curr);
}
settime(flags, interval, value) → [ old_interval, old_value ] click to toggle source

Arms (starts) or disarms (stops) the timer referred by the TimerFD object and returns the old value of the timer.

flags is either zero (or nil) to start a relative timer or :ABSTIME to start an absolute timer. If the interval is zero, the timer fires only once, otherwise the timer is fired every interval seconds. value is the time of the initial expiration in seconds.

static VALUE settime(VALUE self, VALUE fl, VALUE interval, VALUE value)
{
        int fd = rb_sp_fileno(self);
        int flags = rb_sp_get_flags(self, fl, 0);
        struct itimerspec old, new;

        value2timespec(&new.it_interval, interval);
        value2timespec(&new.it_value, value);

        if (timerfd_settime(fd, flags, &new, &old) < 0)
                rb_sys_fail("timerfd_settime");

        return itimerspec2ary(&old);
}