summaryrefslogtreecommitdiff
path: root/ext
diff options
context:
space:
mode:
Diffstat (limited to 'ext')
-rw-r--r--ext/socket/init.c72
1 files changed, 12 insertions, 60 deletions
diff --git a/ext/socket/init.c b/ext/socket/init.c
index ee88e81bae..4c32ab4720 100644
--- a/ext/socket/init.c
+++ b/ext/socket/init.c
@@ -254,76 +254,28 @@ rsock_socket(int domain, int type, int proto)
}
static int
-wait_connectable0(int fd, rb_fdset_t *fds_w, rb_fdset_t *fds_e)
+wait_connectable(int fd)
{
int sockerr;
socklen_t sockerrlen;
+ int r;
for (;;) {
- rb_fd_zero(fds_w);
- rb_fd_zero(fds_e);
-
- rb_fd_set(fd, fds_w);
- rb_fd_set(fd, fds_e);
-
- rb_thread_fd_select(fd+1, 0, fds_w, fds_e, 0);
-
- if (rb_fd_isset(fd, fds_w)) {
+ r = rb_wait_for_single_fd(fd, RB_WAITFD_OUT|RB_WAITFD_PRI, NULL);
+ if ((r > 0) && (r & RB_WAITFD_OUT))
return 0;
+
+ sockerrlen = (socklen_t)sizeof(sockerr);
+ if (getsockopt(fd, SOL_SOCKET, SO_ERROR, (void *)&sockerr,
+ &sockerrlen) == 0) {
+ if (sockerr == 0)
+ continue; /* workaround for winsock */
+ errno = sockerr;
}
- else if (rb_fd_isset(fd, fds_e)) {
- sockerrlen = (socklen_t)sizeof(sockerr);
- if (getsockopt(fd, SOL_SOCKET, SO_ERROR, (void *)&sockerr,
- &sockerrlen) == 0) {
- if (sockerr == 0)
- continue; /* workaround for winsock */
- errno = sockerr;
- }
- return -1;
- }
+ return -1;
}
}
-struct wait_connectable_arg {
- int fd;
- rb_fdset_t fds_w;
- rb_fdset_t fds_e;
-};
-
-#ifdef HAVE_RB_FD_INIT
-static VALUE
-try_wait_connectable(VALUE arg)
-{
- struct wait_connectable_arg *p = (struct wait_connectable_arg *)arg;
- return (VALUE)wait_connectable0(p->fd, &p->fds_w, &p->fds_e);
-}
-
-static VALUE
-wait_connectable_ensure(VALUE arg)
-{
- struct wait_connectable_arg *p = (struct wait_connectable_arg *)arg;
- rb_fd_term(&p->fds_w);
- rb_fd_term(&p->fds_e);
- return Qnil;
-}
-#endif
-
-static int
-wait_connectable(int fd)
-{
- struct wait_connectable_arg arg;
-
- rb_fd_init(&arg.fds_w);
- rb_fd_init(&arg.fds_e);
-#ifdef HAVE_RB_FD_INIT
- arg.fd = fd;
- return (int)rb_ensure(try_wait_connectable, (VALUE)&arg,
- wait_connectable_ensure,(VALUE)&arg);
-#else
- return wait_connectable0(fd, &arg.fds_w, &arg.fds_e);
-#endif
-}
-
#ifdef __CYGWIN__
#define WAIT_IN_PROGRESS 10
#endif