Skip to content

Commit

Permalink
Always use rb_to_float.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Jun 12, 2024
1 parent bbcba97 commit 9d99200
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 39 deletions.
21 changes: 9 additions & 12 deletions ext/io/event/selector/epoll.c
Original file line number Diff line number Diff line change
Expand Up @@ -786,24 +786,21 @@ struct timespec * make_timeout(VALUE duration, struct timespec * storage) {
return NULL;
}

if (RB_FLOAT_TYPE_P(duration)) {
double value = RFLOAT_VALUE(duration);
time_t seconds = value;

storage->tv_sec = seconds;
storage->tv_nsec = (value - seconds) * 1000000000L;

return storage;
}

else if (RB_INTEGER_TYPE_P(duration)) {
if (RB_INTEGER_TYPE_P(duration)) {
storage->tv_sec = NUM2TIMET(duration);
storage->tv_nsec = 0;

return storage;
}

rb_raise(rb_eArgError, "unable to convert timeout: %"PRIsVALUE, rb_inspect(duration));
duration = rb_to_float(duration);
double value = RFLOAT_VALUE(duration);
time_t seconds = value;

storage->tv_sec = seconds;
storage->tv_nsec = (value - seconds) * 1000000000L;

return storage;
}

static
Expand Down
21 changes: 9 additions & 12 deletions ext/io/event/selector/kqueue.c
Original file line number Diff line number Diff line change
Expand Up @@ -799,24 +799,21 @@ struct timespec * make_timeout(VALUE duration, struct timespec * storage) {
return NULL;
}

if (RB_FLOAT_TYPE_P(duration)) {
double value = RFLOAT_VALUE(duration);
time_t seconds = value;

storage->tv_sec = seconds;
storage->tv_nsec = (value - seconds) * 1000000000L;

return storage;
}

else if (RB_INTEGER_TYPE_P(duration)) {
if (RB_INTEGER_TYPE_P(duration)) {
storage->tv_sec = NUM2TIMET(duration);
storage->tv_nsec = 0;

return storage;
}

rb_raise(rb_eArgError, "unable to convert timeout: %"PRIsVALUE, rb_inspect(duration));
duration = rb_to_float(duration);
double value = RFLOAT_VALUE(duration);
time_t seconds = value;

storage->tv_sec = seconds;
storage->tv_nsec = (value - seconds) * 1000000000L;

return storage;
}

static
Expand Down
21 changes: 9 additions & 12 deletions ext/io/event/selector/uring.c
Original file line number Diff line number Diff line change
Expand Up @@ -901,24 +901,21 @@ struct __kernel_timespec * make_timeout(VALUE duration, struct __kernel_timespec
return NULL;
}

if (RB_FLOAT_TYPE_P(duration)) {
double value = RFLOAT_VALUE(duration);
time_t seconds = value;

storage->tv_sec = seconds;
storage->tv_nsec = (value - seconds) * 1000000000L;

return storage;
}

else if (RB_INTEGER_TYPE_P(duration)) {
if (RB_INTEGER_TYPE_P(duration)) {
storage->tv_sec = NUM2TIMET(duration);
storage->tv_nsec = 0;

return storage;
}

rb_raise(rb_eArgError, "unable to convert timeout: %"PRIsVALUE, rb_inspect(duration));
duration = rb_to_float(duration);
double value = RFLOAT_VALUE(duration);
time_t seconds = value;

storage->tv_sec = seconds;
storage->tv_nsec = (value - seconds) * 1000000000L;

return storage;
}

static
Expand Down
7 changes: 5 additions & 2 deletions lib/io/event/selector/select.rb
Original file line number Diff line number Diff line change
Expand Up @@ -420,8 +420,11 @@ def select(duration = nil)
duration = 0 unless @ready.empty?
error = nil

if duration && duration > 0.0
start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
if duration
duration = duration/1.0
if duration > 0
start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
end
else
@idle_duration = 0.0
end
Expand Down
2 changes: 1 addition & 1 deletion test/io/event/selector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def transfer
it "raises an error when given an invalid duration" do
expect do
selector.select("invalid")
end.to raise_exception(ArgumentError)
end.to raise_exception
end
end

Expand Down

0 comments on commit 9d99200

Please sign in to comment.