diff options
-rw-r--r-- | ChangeLog | 12 | ||||
-rw-r--r-- | include/ruby/intern.h | 18 | ||||
-rw-r--r-- | thread.c | 46 | ||||
-rw-r--r-- | win32/Makefile.sub | 5 | ||||
-rw-r--r-- | win32/win32.c | 97 |
5 files changed, 139 insertions, 39 deletions
@@ -1,3 +1,15 @@ +Tue Jan 13 16:39:11 2009 NAKAMURA Usaku <usa@ruby-lang.org> + + * include/ruby/intern.h, thread.c, win32/Makefile.sub (rb_fdset_t, + rb_fd_init, rb_fd_term, rb_fd_zero, rb_fd_set, rb_fd_clr, rb_fd_isset, + rb_fd_select, rb_fd_ptr, rb_fd_max, HAVE_RB_FD_INIT): new type, + functions, and macros for Windows. + + * win32/win32.c (extract_fd, rb_w32_select): use rb_fdset_t to expand + fd_array if needed. [ruby-core:19946] + + * win32/win32.c (copy_fd): new funcion for rb_w32_select(). + Tue Jan 13 12:31:54 2009 Yuki Sonoda (Yugui) <yugui@yugui.jp> * tool/file2lastrev.rb (get_revisions): fixes problem with diff --git a/include/ruby/intern.h b/include/ruby/intern.h index 38f44f94cd..7ce73951b1 100644 --- a/include/ruby/intern.h +++ b/include/ruby/intern.h @@ -218,6 +218,24 @@ int rb_fd_select(int, rb_fdset_t *, rb_fdset_t *, rb_fdset_t *, struct timeval * #define rb_fd_ptr(f) ((f)->fdset) #define rb_fd_max(f) ((f)->maxfd) +#elif defined(_WIN32) + +typedef struct { + int capa; + fd_set *fdset; +} rb_fdset_t; + +void rb_fd_init(volatile rb_fdset_t *); +void rb_fd_term(rb_fdset_t *); +#define rb_fd_zero(f) ((f)->fdset->fd_count = 0) +void rb_fd_set(int, rb_fdset_t *); +#define rb_fd_clr(n, f) rb_w32_fdclr(n, (f)->fdset) +#define rb_fd_isset(n, f) rb_w32_fdisset(n, (f)->fdset) +#define rb_fd_select(n, rfds, wfds, efds, timeout) rb_w32_select(n, (rfds) ? ((rb_fdset_t*)rfds)->fdset : NULL, (wfds) ? ((rb_fdset_t*)wfds)->fdset : NULL, (efds) ? ((rb_fdset_t*)efds)->fdset: NULL, timeout) + +#define rb_fd_ptr(f) ((f)->fdset) +#define rb_fd_max(f) ((f)->fdset->fd_count) + #else typedef fd_set rb_fdset_t; @@ -2244,6 +2244,52 @@ rb_fd_select(int n, rb_fdset_t *readfds, rb_fdset_t *writefds, rb_fdset_t *excep #define FD_CLR(i, f) rb_fd_clr(i, f) #define FD_ISSET(i, f) rb_fd_isset(i, f) +#elif defined(_WIN32) + +void +rb_fd_init(volatile rb_fdset_t *set) +{ + set->capa = FD_SETSIZE; + set->fdset = ALLOC(fd_set); + FD_ZERO(set->fdset); +} + +void +rb_fd_term(rb_fdset_t *set) +{ + xfree(set->fdset); + set->fdset = NULL; + set->capa = 0; +} + +void +rb_fd_set(int fd, rb_fdset_t *set) +{ + unsigned int i; + SOCKET s = rb_w32_get_osfhandle(fd); + + for (i = 0; i < set->fdset->fd_count; i++) { + if (set->fdset->fd_array[i] == s) { + return; + } + } + if (set->fdset->fd_count >= set->capa) { + set->capa = (set->fdset->fd_count / FD_SETSIZE + 1) * FD_SETSIZE; + set->fdset = xrealloc(set->fdset, sizeof(unsigned int) + sizeof(SOCKET) * set->capa); + } + set->fdset->fd_array[set->fdset->fd_count++] = s; +} + +#undef FD_ZERO +#undef FD_SET +#undef FD_CLR +#undef FD_ISSET + +#define FD_ZERO(f) rb_fd_zero(f) +#define FD_SET(i, f) rb_fd_set(i, f) +#define FD_CLR(i, f) rb_fd_clr(i, f) +#define FD_ISSET(i, f) rb_fd_isset(i, f) + #endif #if defined(__CYGWIN__) || defined(_WIN32) diff --git a/win32/Makefile.sub b/win32/Makefile.sub index e25a150e20..a3eef90bb3 100644 --- a/win32/Makefile.sub +++ b/win32/Makefile.sub @@ -223,7 +223,11 @@ RUBY = ruby !endif !if !defined(STACK) +!if "$(ARCH)" == "x64" || "$(ARCH)" == "ia64" +STACK = 0x400000 +!else STACK = 0x200000 +!endif !if defined(STACK_COMMIT) STACK = $(STACK),$(STACK_COMMIT) !endif @@ -454,6 +458,7 @@ $(CONFIG_H): $(MKFILES) $(srcdir)/win32/Makefile.sub $(win_srcdir)/Makefile.sub #define RSHIFT(x,y) ((x)>>(int)y) #define FILE_COUNT _cnt #define FILE_READPTR _ptr +#define HAVE_RB_FD_INIT 1 #define RUBY_SETJMP(env) _setjmp(env) #define RUBY_LONGJMP(env,val) longjmp(env,val) #define RUBY_JMP_BUF jmp_buf diff --git a/win32/win32.c b/win32/win32.c index e8907b6647..8ed026df8c 100644 --- a/win32/win32.c +++ b/win32/win32.c @@ -1989,11 +1989,8 @@ rb_w32_fdclr(int fd, fd_set *set) for (i = 0; i < set->fd_count; i++) { if (set->fd_array[i] == s) { - while (i < set->fd_count - 1) { - set->fd_array[i] = set->fd_array[i + 1]; - i++; - } - set->fd_count--; + memmove(&set->fd_array[i], &set->fd_array[i+1], + sizeof(set->fd_array[0]) * (--set->fd_count - i)); break; } } @@ -2021,7 +2018,7 @@ rb_w32_fdisset(int fd, fd_set *set) #undef select static int -extract_fd(fd_set *dst, fd_set *src, int (*func)(SOCKET)) +extract_fd(rb_fdset_t *dst, fd_set *src, int (*func)(SOCKET)) { int s = 0; if (!src || !dst) return 0; @@ -2032,11 +2029,16 @@ extract_fd(fd_set *dst, fd_set *src, int (*func)(SOCKET)) if (!func || (*func)(fd)) { /* move it to dst */ int d; - for (d = 0; d < dst->fd_count; d++) { - if (dst->fd_array[d] == fd) break; + for (d = 0; d < dst->fdset->fd_count; d++) { + if (dst->fdset->fd_array[d] == fd) + break; } - if (d == dst->fd_count && dst->fd_count < FD_SETSIZE) { - dst->fd_array[dst->fd_count++] = fd; + if (d == dst->fdset->fd_count) { + if (dst->fdset->fd_count >= dst->capa) { + dst->capa = (dst->fdset->fd_count / FD_SETSIZE + 1) * FD_SETSIZE; + dst->fdset = xrealloc(dst->fdset, sizeof(unsigned int) + sizeof(SOCKET) * dst->capa); + } + dst->fdset->fd_array[dst->fdset->fd_count++] = fd; } memmove( &src->fd_array[s], @@ -2046,6 +2048,27 @@ extract_fd(fd_set *dst, fd_set *src, int (*func)(SOCKET)) else s++; } + return dst->fdset->fd_count; +} + +static int +copy_fd(fd_set *dst, fd_set *src) +{ + int s; + if (!src || !dst) return 0; + + for (s = 0; s < src->fd_count; ++s) { + SOCKET fd = src->fd_array[s]; + int d; + for (d = 0; d < dst->fd_count; ++d) { + if (dst->fd_array[d] == fd) + break; + } + if (d == dst->fd_count && d < FD_SETSIZE) { + dst->fd_array[dst->fd_count++] = fd; + } + } + return dst->fd_count; } @@ -2186,11 +2209,11 @@ rb_w32_select(int nfds, fd_set *rd, fd_set *wr, fd_set *ex, struct timeval *timeout) { int r; - fd_set pipe_rd; - fd_set cons_rd; - fd_set else_rd; - fd_set else_wr; - fd_set except; + rb_fdset_t pipe_rd; + rb_fdset_t cons_rd; + rb_fdset_t else_rd; + rb_fdset_t else_wr; + rb_fdset_t except; int nonsock = 0; struct timeval limit; @@ -2225,19 +2248,19 @@ rb_w32_select(int nfds, fd_set *rd, fd_set *wr, fd_set *ex, // until some data is read from pipe. but ruby is single threaded system, // so whole system will be blocked forever. - else_rd.fd_count = 0; + rb_fd_init(&else_rd); nonsock += extract_fd(&else_rd, rd, is_not_socket); - pipe_rd.fd_count = 0; - extract_fd(&pipe_rd, &else_rd, is_pipe); // should not call is_pipe for socket + rb_fd_init(&pipe_rd); + extract_fd(&pipe_rd, else_rd.fdset, is_pipe); // should not call is_pipe for socket - cons_rd.fd_count = 0; - extract_fd(&cons_rd, &else_rd, is_console); // ditto + rb_fd_init(&cons_rd); + extract_fd(&cons_rd, else_rd.fdset, is_console); // ditto - else_wr.fd_count = 0; + rb_fd_init(&else_wr); nonsock += extract_fd(&else_wr, wr, is_not_socket); - except.fd_count = 0; + rb_fd_init(&except); extract_fd(&except, ex, is_not_socket); // drop only r = 0; @@ -2256,15 +2279,17 @@ rb_w32_select(int nfds, fd_set *rd, fd_set *wr, fd_set *ex, if (nonsock) { // modifying {else,pipe,cons}_rd is safe because // if they are modified, function returns immediately. - extract_fd(&else_rd, &pipe_rd, is_readable_pipe); - extract_fd(&else_rd, &cons_rd, is_readable_console); + extract_fd(&else_rd, pipe_rd.fdset, is_readable_pipe); + extract_fd(&else_rd, cons_rd.fdset, is_readable_console); } - if (else_rd.fd_count || else_wr.fd_count) { + if (else_rd.fdset->fd_count || else_wr.fdset->fd_count) { r = do_select(nfds, rd, wr, ex, &zero); // polling if (r < 0) break; // XXX: should I ignore error and return signaled handles? - r += extract_fd(rd, &else_rd, NULL); // move all - r += extract_fd(wr, &else_wr, NULL); // move all + r = copy_fd(rd, else_rd.fdset); + r += copy_fd(wr, else_wr.fdset); + if (ex) + r += ex->fd_count; break; } else { @@ -2294,6 +2319,12 @@ rb_w32_select(int nfds, fd_set *rd, fd_set *wr, fd_set *ex, } } + rb_fd_term(&except); + rb_fd_term(&else_wr); + rb_fd_term(&cons_rd); + rb_fd_term(&pipe_rd); + rb_fd_term(&else_rd); + return r; } @@ -4344,10 +4375,6 @@ rb_w32_read(int fd, void *buf, size_t size) if (_get_osfhandle(fd) == -1) { return -1; } - if (!(_osfile(fd) & FOPEN)) { - errno = EBADF; - return -1; - } if (_osfile(fd) & FTEXT) { return _read(fd, buf, size); @@ -4466,10 +4493,6 @@ rb_w32_write(int fd, const void *buf, size_t size) if (_get_osfhandle(fd) == -1) { return -1; } - if (!(_osfile(fd) & FOPEN)) { - errno = EBADF; - return -1; - } if (_osfile(fd) & FTEXT) { return _write(fd, buf, size); @@ -4702,10 +4725,6 @@ rb_w32_isatty(int fd) if (_get_osfhandle(fd) == -1) { return 0; } - if (!(_osfile(fd) & FOPEN)) { - errno = EBADF; - return 0; - } if (!(_osfile(fd) & FDEV)) { errno = ENOTTY; return 0; |