Module: BlockingSleep
- Defined in:
- lib/blocking_sleep.rb,
ext/blocking_sleep/blocking_sleep.c
Overview
BlockingSleep module provides a native blocking sleep that doesn’t release the GVL (Global VM Lock). This is useful for testing thread behavior and GVL interactions.
Constant Summary collapse
- VERSION =
"0.1.0"
Class Method Summary collapse
-
.sleep(seconds) ⇒ Object
Call native sleep() function without releasing GVL.
Class Method Details
.sleep(seconds) ⇒ Object
Call native sleep() function without releasing GVL. This blocks the entire Ruby VM, useful for testing thread behavior.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'ext/blocking_sleep/blocking_sleep.c', line 11
static VALUE
blocking_sleep_sleep(VALUE self, VALUE seconds)
{
double sleep_time;
double integral;
double fractional;
struct timespec req;
struct timespec rem;
// Convert Ruby number to C double
sleep_time = NUM2DBL(seconds);
if (sleep_time < 0.0) {
rb_raise(rb_eArgError, "sleep duration must be non-negative");
}
fractional = modf(sleep_time, &integral);
req.tv_sec = (time_t)integral;
req.tv_nsec = (long)(fractional * 1e9 + 0.5);
if (req.tv_nsec >= 1000000000L) {
req.tv_sec += 1;
req.tv_nsec -= 1000000000L;
}
// Call native nanosleep() - this blocks without releasing GVL
while (nanosleep(&req, &rem) == -1) {
if (errno == EINTR) {
req = rem;
continue;
}
rb_sys_fail("nanosleep");
}
return Qnil;
}
|