Skip to content

Commit 44b5224

Browse files
committed
Tidy up internal logic and return value of IOOperations.poll.
1 parent 4ed78d4 commit 44b5224

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

lib/truffle/io/wait.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,18 @@ def nread
1414

1515
def ready?
1616
ensure_open_and_readable
17-
Truffle::IOOperations.poll(self, Truffle::IOOperations::POLLIN, 0) > 0
17+
Truffle::IOOperations.poll(self, Truffle::IOOperations::POLLIN, 0)
1818
end
1919

2020
def wait(timeout = nil)
2121
ensure_open_and_readable
22-
Truffle::IOOperations.poll(self, Truffle::IOOperations::POLLIN, timeout) > 0 ? self : nil
22+
Truffle::IOOperations.poll(self, Truffle::IOOperations::POLLIN, timeout) ? self : nil
2323
end
2424

2525
alias_method :wait_readable, :wait
2626

2727
def wait_writable(timeout = nil)
2828
ensure_open_and_writable
29-
Truffle::IOOperations.poll(self, Truffle::IOOperations::POLLOUT, timeout) > 0 ? self : nil
29+
Truffle::IOOperations.poll(self, Truffle::IOOperations::POLLOUT, timeout) ? self : nil
3030
end
3131
end

src/main/ruby/truffleruby/core/truffle/io_operations.rb

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,9 @@ def self.select(readables, readable_ios, writables, writable_ios, errorables, er
199199

200200
end
201201

202+
# This method will return a true if poll returned without error
203+
# with an event within the timeout, false if the timeout expired,
204+
# or raises an exception for an errno.
202205
def self.poll(io, events, timeout)
203206
if (events & POLLIN) != 0
204207
return 1 unless io.__send__(:buffer_empty?)
@@ -212,8 +215,9 @@ def self.poll(io, events, timeout)
212215
raise ArgumentError, 'timeout must be positive' if timeout < 0
213216

214217
# Microseconds, rounded down
215-
timeout = remaining_timeout = Integer(timeout * 1_000)
218+
timeout = remaining_timeout = (timeout * 1_000).to_i
216219
start = Process.clock_gettime(Process::CLOCK_MONOTONIC, :millisecond)
220+
deadline = start + timeout
217221
else
218222
remaining_timeout = -1
219223
end
@@ -231,11 +235,10 @@ def self.poll(io, events, timeout)
231235
if timeout
232236
# Update timeout
233237
now = Process.clock_gettime(Process::CLOCK_MONOTONIC, :millisecond)
234-
waited = now - start
235-
if waited >= timeout
236-
nil # timeout
238+
if now >= deadline
239+
false # timeout
237240
else
238-
remaining_timeout = timeout - waited
241+
remaining_timeout = deadline - now
239242
:retry
240243
end
241244
else
@@ -245,7 +248,7 @@ def self.poll(io, events, timeout)
245248
Errno.handle_errno(errno)
246249
end
247250
else
248-
primitive_result
251+
primitive_result > 0
249252
end
250253
end while result == :retry
251254
ensure

0 commit comments

Comments
 (0)