1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

file.c: non-blocking open

* file.c (rb_file_load_ok): open in non-blocking mode withoout
  releasing GVL.  don't care about others than regular files and
  directories.  [ruby-dev:49272] [Bug #11559]
* ruby.c (load_file_internal): ditto.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@52139 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2015-10-16 06:54:38 +00:00
parent 6291c6ad77
commit abf832f435
6 changed files with 32 additions and 25 deletions

View file

@ -1,3 +1,11 @@
Fri Oct 16 15:54:37 2015 Nobuyoshi Nakada <nobu@ruby-lang.org>
* file.c (rb_file_load_ok): open in non-blocking mode withoout
releasing GVL. don't care about others than regular files and
directories. [ruby-dev:49272] [Bug #11559]
* ruby.c (load_file_internal): ditto.
Thu Oct 15 23:56:03 2015 Nobuyoshi Nakada <nobu@ruby-lang.org>
* proc.c (rb_sym_to_proc): make void env.

View file

@ -728,7 +728,7 @@ compile.$(OBJEXT): {$(VPATH)}opt_sc.inc {$(VPATH)}optunifs.inc
win32/win32.$(OBJEXT): {$(VPATH)}win32/win32.c {$(VPATH)}win32/file.h \
{$(VPATH)}dln.h {$(VPATH)}dln_find.c {$(VPATH)}encindex.h \
{$(VPATH)}internal.h {$(VPATH)}util.h $(RUBY_H_INCLUDES) $(PLATFORM_D)
win32/file.$(OBJEXT): {$(VPATH)}win32/file.c {$(VPATH)}win32/file.h {$(VPATH)}thread.h \
win32/file.$(OBJEXT): {$(VPATH)}win32/file.c {$(VPATH)}win32/file.h \
$(RUBY_H_INCLUDES) $(PLATFORM_D)
$(NEWLINE_C): $(srcdir)/enc/trans/newline.trans $(srcdir)/tool/transcode-tblgen.rb
@ -1462,7 +1462,6 @@ file.$(OBJEXT): {$(VPATH)}missing.h
file.$(OBJEXT): {$(VPATH)}oniguruma.h
file.$(OBJEXT): {$(VPATH)}st.h
file.$(OBJEXT): {$(VPATH)}subst.h
file.$(OBJEXT): {$(VPATH)}thread.h
file.$(OBJEXT): {$(VPATH)}util.h
gc.$(OBJEXT): $(CCAN_DIR)/check_type/check_type.h
gc.$(OBJEXT): $(CCAN_DIR)/container_of/container_of.h

20
file.c
View file

@ -26,7 +26,6 @@
#include "internal.h"
#include "ruby/io.h"
#include "ruby/util.h"
#include "ruby/thread.h"
#include "dln.h"
#include "encindex.h"
@ -5665,25 +5664,24 @@ rb_path_check(const char *path)
}
#ifndef _WIN32
static void *
loadopen_func(void *arg)
{
return (void *)(VALUE)rb_cloexec_open((const char *)arg, O_RDONLY, 0);
}
int
rb_file_load_ok(const char *path)
{
int ret = 1;
int fd;
fd = (int)(VALUE)rb_thread_call_without_gvl(loadopen_func, (void *)path, RUBY_UBF_IO, 0);
int mode = (O_RDONLY |
#if defined O_NONBLOCK
O_NONBLOCK |
#elif defined O_NDELAY
O_NDELAY |
#endif
0);
int fd = rb_cloexec_open(path, mode, 0);
if (fd == -1) return 0;
rb_update_max_fd(fd);
#if !defined DOSISH
{
struct stat st;
if (fstat(fd, &st) || !S_ISREG(st.st_mode)) {
if (fstat(fd, &st) || S_ISDIR(st.st_mode)) {
ret = 0;
}
}

5
ruby.c
View file

@ -1739,6 +1739,11 @@ load_file_internal(VALUE arg)
}
else {
int fd, mode = O_RDONLY;
#if defined O_NONBLOCK
mode |= O_NONBLOCK;
#elif defined O_NDELAY
mod |= O_NDELAY;
#endif
#if defined DOSISH || defined __CYGWIN__
{
const char *ext = strrchr(fname, '.');

View file

@ -702,7 +702,12 @@ class TestRequire < Test::Unit::TestCase
assert_separately(["-", f.path], <<-END, timeout: 3)
th = Thread.current
Thread.start {begin sleep(0.001) end until th.stop?; th.raise(IOError)}
assert_raise(IOError) {load(ARGV[0])}
assert_nothing_raised do
begin
load(ARGV[0])
rescue IOError
end
end
END
}
end unless /mswin|mingw/ =~ RUBY_PLATFORM

View file

@ -4,7 +4,6 @@
#endif
#include "ruby/ruby.h"
#include "ruby/encoding.h"
#include "ruby/thread.h"
#include "internal.h"
#include <winbase.h>
#include <wchar.h>
@ -700,14 +699,6 @@ rb_readlink(VALUE path, rb_encoding *resultenc)
return str;
}
static void *
loadopen_func(void *wpath)
{
return (void *)CreateFileW(wpath, GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
}
int
rb_file_load_ok(const char *path)
{
@ -725,8 +716,9 @@ rb_file_load_ok(const char *path)
ret = 0;
}
else {
HANDLE h = (HANDLE)rb_thread_call_without_gvl(loadopen_func, (void *)wpath,
RUBY_UBF_IO, 0);
HANDLE h = CreateFileW(wpath, GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (h != INVALID_HANDLE_VALUE) {
CloseHandle(h);
}