/*
 * call-seq:
 *
 *      Kgio.poll({ $stdin => :wait_readable }, 100)  -> hash or nil
 *      Kgio.poll({ $stdin => Kgio::POLLIN }, 100)  -> hash or nil
 *
 * Accepts an input hash with IO objects to wait for as the key and
 * the events to wait for as its value.  The events may either be
 * +:wait_readable+ or +:wait_writable+ symbols or a Fixnum mask of
 * Kgio::POLL* constants:
 *
 *      Kgio::POLLIN      - there is data to read
 *      Kgio::POLLPRI     - there is urgent data to read
 *      Kgio::POLLOUT     - writing will not block
 *      Kgio::POLLRDHUP   - peer has shutdown writes (Linux 2.6.17+ only)
 *
 * Timeout is specified in Integer milliseconds just like the underlying
 * poll(2), not in seconds like IO.select.  A nil timeout means to wait
 * forever.  It must be an Integer or nil.
 *
 * Kgio.poll modifies and returns its input hash on success with the
 * IO-like object as the key and an Integer mask of events as the hash
 * value.  It can return any of the events specified in the input
 * above, along with the following events:
 *
 *      Kgio::POLLERR     - error condition occurred on the descriptor
 *      Kgio::POLLHUP     - hang up
 *      Kgio::POLLNVAL    - invalid request (bad file descriptor)
 *
 * This method is only available under Ruby 1.9 or any other
 * implementations that uses native threads and rb_thread_blocking_region()
 */
static VALUE s_poll(int argc, VALUE *argv, VALUE self)
{
        VALUE timeout;
        struct poll_args a;

        rb_scan_args(argc, argv, "11", &a.ios, &timeout);
        a.timeout = num2timeout(timeout);
        a.fds = NULL;
        a.fd_to_io = NULL;

        return rb_ensure(do_poll, (VALUE)&a, poll_free, (VALUE)&a);
}