2020-08-19 21:51:45 -04:00
|
|
|
/**********************************************************************
|
|
|
|
|
|
|
|
scheduler.c
|
|
|
|
|
|
|
|
$Author$
|
|
|
|
|
|
|
|
Copyright (C) 2020 Samuel Grant Dawson Williams
|
|
|
|
|
|
|
|
**********************************************************************/
|
|
|
|
|
|
|
|
#include "internal/scheduler.h"
|
|
|
|
#include "ruby/io.h"
|
|
|
|
|
|
|
|
static ID id_kernel_sleep;
|
2020-09-05 00:26:24 -04:00
|
|
|
static ID id_mutex_lock;
|
|
|
|
static ID id_mutex_unlock;
|
2020-08-19 21:51:45 -04:00
|
|
|
static ID id_io_read;
|
|
|
|
static ID id_io_write;
|
|
|
|
static ID id_io_wait;
|
|
|
|
|
|
|
|
void
|
|
|
|
Init_Scheduler(void)
|
|
|
|
{
|
|
|
|
id_kernel_sleep = rb_intern_const("kernel_sleep");
|
2020-09-05 00:26:24 -04:00
|
|
|
id_mutex_lock = rb_intern_const("mutex_lock");
|
|
|
|
id_mutex_unlock = rb_intern_const("mutex_unlock");
|
2020-08-19 21:51:45 -04:00
|
|
|
id_io_read = rb_intern_const("io_read");
|
|
|
|
id_io_write = rb_intern_const("io_write");
|
|
|
|
id_io_wait = rb_intern_const("io_wait");
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_scheduler_timeout(struct timeval *timeout) {
|
|
|
|
if (timeout) {
|
|
|
|
return rb_float_new((double)timeout->tv_sec + (0.000001f * timeout->tv_usec));
|
|
|
|
}
|
|
|
|
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE rb_scheduler_kernel_sleep(VALUE scheduler, VALUE timeout)
|
|
|
|
{
|
|
|
|
return rb_funcall(scheduler, id_kernel_sleep, 1, timeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE rb_scheduler_kernel_sleepv(VALUE scheduler, int argc, VALUE * argv)
|
|
|
|
{
|
|
|
|
return rb_funcallv(scheduler, id_kernel_sleep, argc, argv);
|
|
|
|
}
|
|
|
|
|
2020-09-05 00:26:24 -04:00
|
|
|
VALUE rb_scheduler_mutex_lock(VALUE scheduler, VALUE mutex)
|
|
|
|
{
|
|
|
|
return rb_funcall(scheduler, id_mutex_lock, 1, mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE rb_scheduler_mutex_unlock(VALUE scheduler, VALUE mutex, VALUE fiber)
|
|
|
|
{
|
|
|
|
return rb_funcall(scheduler, id_mutex_unlock, 2, mutex, fiber);
|
|
|
|
}
|
|
|
|
|
2020-08-19 21:51:45 -04:00
|
|
|
VALUE rb_scheduler_io_wait(VALUE scheduler, VALUE io, VALUE events, VALUE timeout)
|
|
|
|
{
|
|
|
|
return rb_funcall(scheduler, id_io_wait, 3, io, events, timeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE rb_scheduler_io_wait_readable(VALUE scheduler, VALUE io)
|
|
|
|
{
|
|
|
|
return rb_scheduler_io_wait(scheduler, io, RB_UINT2NUM(RUBY_IO_READABLE), Qnil);
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE rb_scheduler_io_wait_writable(VALUE scheduler, VALUE io)
|
|
|
|
{
|
|
|
|
return rb_scheduler_io_wait(scheduler, io, RB_UINT2NUM(RUBY_IO_WRITABLE), Qnil);
|
|
|
|
}
|
|
|
|
|
2020-08-20 08:53:08 -04:00
|
|
|
int rb_scheduler_supports_io_read(VALUE scheduler)
|
2020-08-19 21:51:45 -04:00
|
|
|
{
|
2020-08-20 08:53:08 -04:00
|
|
|
return rb_respond_to(scheduler, id_io_read);
|
2020-08-19 21:51:45 -04:00
|
|
|
}
|
|
|
|
|
2020-08-20 08:53:08 -04:00
|
|
|
VALUE rb_scheduler_io_read(VALUE scheduler, VALUE io, VALUE buffer, size_t offset, size_t length)
|
|
|
|
{
|
|
|
|
return rb_funcall(scheduler, id_io_read, 4, io, buffer, SIZET2NUM(offset), SIZET2NUM(length));
|
|
|
|
}
|
|
|
|
|
|
|
|
int rb_scheduler_supports_io_write(VALUE scheduler)
|
|
|
|
{
|
|
|
|
return rb_respond_to(scheduler, id_io_write);
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE rb_scheduler_io_write(VALUE scheduler, VALUE io, VALUE buffer, size_t offset, size_t length)
|
2020-08-19 21:51:45 -04:00
|
|
|
{
|
|
|
|
// We should ensure string has capacity to receive data, and then resize it afterwards.
|
2020-08-20 08:53:08 -04:00
|
|
|
return rb_funcall(scheduler, id_io_write, 4, io, buffer, SIZET2NUM(offset), SIZET2NUM(length));
|
2020-08-19 21:51:45 -04:00
|
|
|
}
|