Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework (VALUE* args) -> (VALUE arg) invalid function type. Fixes #287. #295

Merged
merged 1 commit into from
Apr 2, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 29 additions & 30 deletions ext/nio4r/selector.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ static VALUE NIO_Selector_closed(VALUE self);
static VALUE NIO_Selector_is_empty(VALUE self);

/* Internal functions */
static VALUE NIO_Selector_synchronize(VALUE self, VALUE (*func)(VALUE *args), VALUE *args);
static VALUE NIO_Selector_synchronize(VALUE self, VALUE (*func)(VALUE arg), VALUE arg);
static VALUE NIO_Selector_unlock(VALUE lock);
static VALUE NIO_Selector_register_synchronized(VALUE *args);
static VALUE NIO_Selector_deregister_synchronized(VALUE *args);
static VALUE NIO_Selector_select_synchronized(VALUE *args);
static VALUE NIO_Selector_close_synchronized(VALUE *args);
static VALUE NIO_Selector_closed_synchronized(VALUE *args);
static VALUE NIO_Selector_register_synchronized(VALUE arg);
static VALUE NIO_Selector_deregister_synchronized(VALUE arg);
static VALUE NIO_Selector_select_synchronized(VALUE arg);
static VALUE NIO_Selector_close_synchronized(VALUE arg);
static VALUE NIO_Selector_closed_synchronized(VALUE arg);

static int NIO_Selector_run(struct NIO_Selector *selector, VALUE timeout);
static void NIO_Selector_timeout_callback(struct ev_loop *ev_loop, struct ev_timer *timer, int revents);
Expand All @@ -62,7 +62,7 @@ static void NIO_Selector_wakeup_callback(struct ev_loop *ev_loop, struct ev_io *
#define BUSYWAIT_INTERVAL 0.01

/* Selectors wait for events */
void Init_NIO_Selector()
void Init_NIO_Selector(void)
{
mNIO = rb_define_module("NIO");
cNIO_Selector = rb_define_class_under(mNIO, "Selector", rb_cObject);
Expand Down Expand Up @@ -285,7 +285,7 @@ static VALUE NIO_Selector_backend(VALUE self)
}

/* Synchronize around a reentrant selector lock */
static VALUE NIO_Selector_synchronize(VALUE self, VALUE (*func)(VALUE *args), VALUE *args)
static VALUE NIO_Selector_synchronize(VALUE self, VALUE (*func)(VALUE arg), VALUE arg)
{
VALUE current_thread, lock_holder, lock;

Expand All @@ -298,10 +298,10 @@ static VALUE NIO_Selector_synchronize(VALUE self, VALUE (*func)(VALUE *args), VA
rb_ivar_set(self, rb_intern("lock_holder"), current_thread);

/* We've acquired the lock, so ensure we unlock it */
return rb_ensure(func, (VALUE)args, NIO_Selector_unlock, self);
return rb_ensure(func, (VALUE)arg, NIO_Selector_unlock, self);
} else {
/* We already hold the selector lock, so no need to unlock it */
return func(args);
return func(arg);
}
}

Expand All @@ -321,17 +321,18 @@ static VALUE NIO_Selector_unlock(VALUE self)
/* Register an IO object with the selector for the given interests */
static VALUE NIO_Selector_register(VALUE self, VALUE io, VALUE interests)
{
VALUE args[3] = { self, io, interests };
return NIO_Selector_synchronize(self, NIO_Selector_register_synchronized, args);
VALUE args[3] = {self, io, interests};
return NIO_Selector_synchronize(self, NIO_Selector_register_synchronized, (VALUE)args);
}

/* Internal implementation of register after acquiring mutex */
static VALUE NIO_Selector_register_synchronized(VALUE *args)
static VALUE NIO_Selector_register_synchronized(VALUE _args)
{
VALUE self, io, interests, selectables, monitor;
VALUE monitor_args[3];
struct NIO_Selector *selector;

VALUE *args = (VALUE *)_args;
self = args[0];
io = args[1];
interests = args[2];
Expand Down Expand Up @@ -361,15 +362,16 @@ static VALUE NIO_Selector_register_synchronized(VALUE *args)
/* Deregister an IO object from the selector */
static VALUE NIO_Selector_deregister(VALUE self, VALUE io)
{
VALUE args[2] = { self, io };
return NIO_Selector_synchronize(self, NIO_Selector_deregister_synchronized, args);
VALUE args[2] = {self, io};
return NIO_Selector_synchronize(self, NIO_Selector_deregister_synchronized, (VALUE)args);
}

/* Internal implementation of register after acquiring mutex */
static VALUE NIO_Selector_deregister_synchronized(VALUE *args)
static VALUE NIO_Selector_deregister_synchronized(VALUE _args)
{
VALUE self, io, selectables, monitor;

VALUE *args = (VALUE *)_args;
self = args[0];
io = args[1];

Expand All @@ -396,27 +398,26 @@ static VALUE NIO_Selector_is_registered(VALUE self, VALUE io)
static VALUE NIO_Selector_select(int argc, VALUE *argv, VALUE self)
{
VALUE timeout;
VALUE args[2];

rb_scan_args(argc, argv, "01", &timeout);

if (timeout != Qnil && NUM2DBL(timeout) < 0) {
rb_raise(rb_eArgError, "time interval must be positive");
}

args[0] = self;
args[1] = timeout;

return NIO_Selector_synchronize(self, NIO_Selector_select_synchronized, args);
VALUE args[2] = {self, timeout};
return NIO_Selector_synchronize(self, NIO_Selector_select_synchronized, (VALUE)args);
}

/* Internal implementation of select with the selector lock held */
static VALUE NIO_Selector_select_synchronized(VALUE *args)
static VALUE NIO_Selector_select_synchronized(VALUE _args)
{
int ready;
VALUE ready_array;
struct NIO_Selector *selector;

VALUE *args = (VALUE *)_args;

Data_Get_Struct(args[0], struct NIO_Selector, selector);

if (selector->closed) {
Expand Down Expand Up @@ -504,14 +505,13 @@ static VALUE NIO_Selector_wakeup(VALUE self)
/* Close the selector and free system resources */
static VALUE NIO_Selector_close(VALUE self)
{
VALUE args[1] = { self };
return NIO_Selector_synchronize(self, NIO_Selector_close_synchronized, args);
return NIO_Selector_synchronize(self, NIO_Selector_close_synchronized, self);
}

static VALUE NIO_Selector_close_synchronized(VALUE *args)
static VALUE NIO_Selector_close_synchronized(VALUE self)
{
struct NIO_Selector *selector;
VALUE self = args[0];

Data_Get_Struct(self, struct NIO_Selector, selector);

NIO_Selector_shutdown(selector);
Expand All @@ -522,14 +522,13 @@ static VALUE NIO_Selector_close_synchronized(VALUE *args)
/* Is the selector closed? */
static VALUE NIO_Selector_closed(VALUE self)
{
VALUE args[1] = { self };
return NIO_Selector_synchronize(self, NIO_Selector_closed_synchronized, args);
return NIO_Selector_synchronize(self, NIO_Selector_closed_synchronized, self);
}

static VALUE NIO_Selector_closed_synchronized(VALUE *args)
static VALUE NIO_Selector_closed_synchronized(VALUE self)
{
struct NIO_Selector *selector;
VALUE self = args[0];

Data_Get_Struct(self, struct NIO_Selector, selector);

return selector->closed ? Qtrue : Qfalse;
Expand Down