Skip to content

Commit 0549019

Browse files
committed
Move some logic to truffleposix library.
1 parent ea64cb3 commit 0549019

File tree

3 files changed

+28
-24
lines changed

3 files changed

+28
-24
lines changed

src/main/c/truffleposix/truffleposix.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ SUCH DAMAGE.
4646
#include <dirent.h>
4747
#include <errno.h>
4848
#include <fcntl.h>
49+
#include <poll.h>
4950
#include <pwd.h>
5051
#include <spawn.h>
5152
#include <stdint.h>
@@ -110,6 +111,15 @@ static void mark_ready_from_set(fd_set *set, int nfds, int *fds) {
110111
}
111112
}
112113

114+
int truffleposix_poll(int fd, int events, int timeout) {
115+
struct pollfd fds;
116+
117+
fds.fd = fd;
118+
fds.events = events;
119+
120+
return poll(&fds, 1, timeout);
121+
}
122+
113123
int truffleposix_select(int nread, int *readfds, int nwrite, int *writefds,
114124
int nexcept, int *exceptfds, long timeout_us) {
115125
struct timeval timeout;

src/main/ruby/truffleruby/core/posix.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ def self.attach_function_eagerly(native_name, argument_types, return_type,
215215
attach_function :opendir, [:string], :pointer
216216
attach_function :pipe, [:pointer], :int
217217
attach_function :poll, [:pointer, :long, :int], :int, LIBC, true
218+
attach_function :truffleposix_poll, [:pointer, :long, :int], :int, LIBTRUFFLEPOSIX, true
218219
attach_function :read, [:int, :pointer, :size_t], :ssize_t, LIBC, true
219220
attach_function :readlink, [:string, :pointer, :size_t], :ssize_t
220221
attach_function :realpath, [:string, :pointer], :pointer

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

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -222,38 +222,31 @@ def self.poll(io, events, timeout)
222222
remaining_timeout = -1
223223
end
224224

225-
pollfd = Primitive.io_thread_buffer_allocate(POLLFD_SIZE)
226225
begin
227-
pollfd.put_int(POLLFD_FD_OFFSET, Primitive.io_fd(io))
228-
pollfd.put_uint16(POLLFD_EVENTS_OFFSET, events)
229-
begin
230-
primitive_result = Truffle::POSIX.poll(pollfd, 1, remaining_timeout)
231-
result =
232-
if primitive_result < 0
233-
errno = Errno.errno
234-
if errno == Errno::EINTR::Errno
235-
if timeout
236-
# Update timeout
237-
now = Process.clock_gettime(Process::CLOCK_MONOTONIC, :millisecond)
238-
if now >= deadline
239-
false # timeout
240-
else
241-
remaining_timeout = deadline - now
242-
:retry
243-
end
226+
primitive_result = Truffle::POSIX.truffleposix_poll(Primitive.io_fd(io), events, remaining_timeout)
227+
result =
228+
if primitive_result < 0
229+
errno = Errno.errno
230+
if errno == Errno::EINTR::Errno
231+
if timeout
232+
# Update timeout
233+
now = Process.clock_gettime(Process::CLOCK_MONOTONIC, :millisecond)
234+
if now >= deadline
235+
false # timeout
244236
else
237+
remaining_timeout = deadline - now
245238
:retry
246239
end
247240
else
248-
Errno.handle_errno(errno)
241+
:retry
249242
end
250243
else
251-
primitive_result > 0
244+
Errno.handle_errno(errno)
252245
end
253-
end while result == :retry
254-
ensure
255-
Primitive.io_thread_buffer_free(pollfd)
256-
end
246+
else
247+
primitive_result > 0
248+
end
249+
end while result == :retry
257250

258251
result
259252
end

0 commit comments

Comments
 (0)