ext/kgio/accept.c in kgio-2.6.0 vs ext/kgio/accept.c in kgio-2.7.0

- old
+ new

@@ -8,11 +8,11 @@ static VALUE cClientSocket; static VALUE cKgio_Socket; static VALUE mSocketMethods; static VALUE iv_kgio_addr; -#if defined(__linux__) +#if defined(__linux__) && defined(HAVE_RB_THREAD_BLOCKING_REGION) static int accept4_flags = SOCK_CLOEXEC; #else /* ! linux */ static int accept4_flags = SOCK_CLOEXEC | SOCK_NONBLOCK; #endif /* ! linux */ @@ -123,10 +123,14 @@ { int rv; /* always use non-blocking accept() under 1.8 for green threads */ set_nonblocking(a->fd); + + /* created sockets are always non-blocking under 1.8, too */ + a->flags |= SOCK_NONBLOCK; + TRAP_BEG; rv = (int)xaccept(a); TRAP_END; return rv; } @@ -277,16 +281,15 @@ * Returns nil on EAGAIN, and raises on other errors. * * An optional +klass+ argument may be specified to override the * Kgio::Socket-class on a successful return value. * - * An optional +flags+ argument may also be specifed to override the - * value of +Kgio.accept_cloexec+ and +Kgio.accept_nonblock+. +flags+ - * is a bitmask that may contain any combination of: + * An optional +flags+ argument may also be specified. + * +flags+ is a bitmask that may contain any combination of: * - * - Kgio::SOCK_CLOEXEC - close-on-exec flag - * - Kgio::SOCK_NONBLOCK - non-blocking flag + * - Kgio::SOCK_CLOEXEC - close-on-exec flag (enabled by default) + * - Kgio::SOCK_NONBLOCK - non-blocking flag (unimportant) */ static VALUE tcp_tryaccept(int argc, VALUE *argv, VALUE self) { struct sockaddr_storage addr; socklen_t addrlen = sizeof(struct sockaddr_storage); @@ -314,16 +317,15 @@ * accept(2) (or accept4(2)) system call to avoid thundering herds. * * An optional +klass+ argument may be specified to override the * Kgio::Socket-class on a successful return value. * - * An optional +flags+ argument may also be specifed to override the - * value of +Kgio.accept_cloexec+ and +Kgio.accept_nonblock+. +flags+ - * is a bitmask that may contain any combination of: + * An optional +flags+ argument may also be specified. + * +flags+ is a bitmask that may contain any combination of: * - * - Kgio::SOCK_CLOEXEC - close-on-exec flag - * - Kgio::SOCK_NONBLOCK - non-blocking flag + * - Kgio::SOCK_CLOEXEC - close-on-exec flag (enabled by default) + * - Kgio::SOCK_NONBLOCK - non-blocking flag (unimportant) */ static VALUE tcp_accept(int argc, VALUE *argv, VALUE self) { struct sockaddr_storage addr; socklen_t addrlen = sizeof(struct sockaddr_storage); @@ -348,16 +350,15 @@ * Kgio::LOCALHOST) on success. * * An optional +klass+ argument may be specified to override the * Kgio::Socket-class on a successful return value. * - * An optional +flags+ argument may also be specifed to override the - * value of +Kgio.accept_cloexec+ and +Kgio.accept_nonblock+. +flags+ - * is a bitmask that may contain any combination of: + * An optional +flags+ argument may also be specified. + * +flags+ is a bitmask that may contain any combination of: * - * - Kgio::SOCK_CLOEXEC - close-on-exec flag - * - Kgio::SOCK_NONBLOCK - non-blocking flag + * - Kgio::SOCK_CLOEXEC - close-on-exec flag (enabled by default) + * - Kgio::SOCK_NONBLOCK - non-blocking flag (unimportant) */ static VALUE unix_tryaccept(int argc, VALUE *argv, VALUE self) { struct accept_args a; @@ -383,16 +384,15 @@ * accept(2) (or accept4(2)) system call to avoid thundering herds. * * An optional +klass+ argument may be specified to override the * Kgio::Socket-class on a successful return value. * - * An optional +flags+ argument may also be specifed to override the - * value of +Kgio.accept_cloexec+ and +Kgio.accept_nonblock+. +flags+ - * is a bitmask that may contain any combination of: + * An optional +flags+ argument may also be specified. + * +flags+ is a bitmask that may contain any combination of: * - * - Kgio::SOCK_CLOEXEC - close-on-exec flag - * - Kgio::SOCK_NONBLOCK - non-blocking flag + * - Kgio::SOCK_CLOEXEC - close-on-exec flag (enabled by default) + * - Kgio::SOCK_NONBLOCK - non-blocking flag (unimportant) */ static VALUE unix_accept(int argc, VALUE *argv, VALUE self) { struct accept_args a; @@ -407,10 +407,12 @@ * * Kgio.accept_cloexec? -> true or false * * Returns true if newly accepted Kgio::Sockets are created with the * FD_CLOEXEC file descriptor flag, false if not. + * + * Deprecated, use the per-socket flags for kgio_*accept instead. */ static VALUE get_cloexec(VALUE mod) { return (accept4_flags & SOCK_CLOEXEC) == SOCK_CLOEXEC ? Qtrue : Qfalse; } @@ -421,10 +423,12 @@ * * Kgio.accept_nonblock? -> true or false * * Returns true if newly accepted Kgio::Sockets are created with the * O_NONBLOCK file status flag, false if not. + * + * Deprecated, use the per-socket flags for kgio_*accept instead. */ static VALUE get_nonblock(VALUE mod) { return (accept4_flags & SOCK_NONBLOCK)==SOCK_NONBLOCK ? Qtrue : Qfalse; } @@ -442,10 +446,12 @@ * and UNIXServer#kgio_tryaccept * default to being created with the FD_CLOEXEC file descriptor flag. * * This is on by default, as there is little reason to deal to enable * it for client sockets on a socket server. + * + * Deprecated, use the per-socket flags for kgio_*accept instead. */ static VALUE set_cloexec(VALUE mod, VALUE boolean) { switch (TYPE(boolean)) { case T_TRUE: @@ -474,10 +480,14 @@ * * This defaults to +false+ for GNU/Linux where MSG_DONTWAIT is * available (and on newer GNU/Linux, accept4() may also set * the non-blocking flag. This defaults to +true+ on non-GNU/Linux * systems. + * + * This is always true on Ruby implementations using user-space threads. + * + * Deprecated, use the per-socket flags for kgio_*accept instead. */ static VALUE set_nonblock(VALUE mod, VALUE boolean) { switch (TYPE(boolean)) { case T_TRUE: @@ -495,17 +505,21 @@ { VALUE cUNIXServer, cTCPServer; VALUE mKgio = rb_define_module("Kgio"); /* - * this maps to the SOCK_NONBLOCK constant in Linux for setting - * the non-blocking flag on newly accepted sockets. + * Maps to the SOCK_NONBLOCK constant in Linux for setting + * the non-blocking flag on newly accepted sockets. This is + * usually unnecessary as sockets are made non-blocking + * whenever non-blocking methods are used. */ rb_define_const(mKgio, "SOCK_NONBLOCK", INT2NUM(SOCK_NONBLOCK)); /* - * this maps to the SOCK_CLOEXEC constant in Linux for setting - * the close-on-exec flag on newly accepted descriptors. + * Maps to the SOCK_CLOEXEC constant in Linux for setting + * the close-on-exec flag on newly accepted descriptors. This + * is enabled by default, and there is usually no reason to + * disable close-on-exec for accepted sockets. */ rb_define_const(mKgio, "SOCK_CLOEXEC", INT2NUM(SOCK_CLOEXEC)); localhost = rb_const_get(mKgio, rb_intern("LOCALHOST")); cKgio_Socket = rb_const_get(mKgio, rb_intern("Socket"));