#include <ruby.h>
#include <ruby/thread.h>
#include <stdint.h>
#include <time.h>

static void *
slow(void *arg)
{
    long ms = (long)(intptr_t)arg;
    struct timespec ts = { ms / 1000, (ms % 1000) * 1000000L };

    while (nanosleep(&ts, &ts) != 0)
    {
    }
    return (void *)1;
}

static VALUE
slow_op_run(VALUE self, VALUE ms)
{
    void *ran = rb_nogvl(slow, (void *)(intptr_t)NUM2LONG(ms), RUBY_UBF_IO, NULL, RB_NOGVL_OFFLOAD_SAFE);

    return ran ? Qtrue : Qfalse;
}

void
Init_slow_op(void)
{
    VALUE mod = rb_define_module("SlowOp");

    rb_define_module_function(mod, "run", slow_op_run, 1);
}
