diff --git a/ChangeLog b/ChangeLog index fdac0ae480..8e97b73f48 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +Wed Apr 1 13:46:20 2009 Yukihiro Matsumoto + + * thread.c (rb_thread_fd_select): new function to call select + using rb_fdset_t. + + * io.c (select_internal): use rb_thread_fd_select instead of + rb_thread_select. based on the patch from Kengo Matsuyama. + [ruby-dev:38221] + Wed Apr 1 13:16:19 2009 Yukihiro Matsumoto * process.c (rb_f_sleep): RDoc disambiguation. [ruby-talk:332632] diff --git a/include/ruby/intern.h b/include/ruby/intern.h index 617c11808e..49a27fc74a 100644 --- a/include/ruby/intern.h +++ b/include/ruby/intern.h @@ -244,6 +244,7 @@ typedef fd_set rb_fdset_t; #define rb_fd_clr(n, f) FD_CLR(n, f) #define rb_fd_isset(n, f) FD_ISSET(n, f) #define rb_fd_copy(d, s, n) (*(d) = *(s)) +#define rb_fd_resize(n, f) (void)(f) #define rb_fd_ptr(f) (f) #define rb_fd_init(f) FD_ZERO(f) #define rb_fd_term(f) (void)(f) @@ -327,6 +328,7 @@ VALUE rb_thread_create(VALUE (*)(ANYARGS), void*); void rb_thread_signal_raise(void *, int); void rb_thread_signal_exit(void *); int rb_thread_select(int, fd_set *, fd_set *, fd_set *, struct timeval *); +int rb_thread_fd_select(int, rb_fdset_t *, rb_fdset_t *, rb_fdset_t *, struct timeval *); void rb_thread_wait_for(struct timeval); VALUE rb_thread_current(void); VALUE rb_thread_main(void); diff --git a/io.c b/io.c index 38570eb065..ddfb5e0f43 100644 --- a/io.c +++ b/io.c @@ -6635,7 +6635,7 @@ static VALUE select_internal(VALUE read, VALUE write, VALUE except, struct timeval *tp, rb_fdset_t *fds) { VALUE res, list; - fd_set *rp, *wp, *ep; + rb_fdset_t *rp, *wp, *ep; rb_io_t *fptr; long i; int max = 0, n; @@ -6658,7 +6658,7 @@ select_internal(VALUE read, VALUE write, VALUE except, struct timeval *tp, rb_fd timerec.tv_sec = timerec.tv_usec = 0; tp = &timerec; } - rp = rb_fd_ptr(&fds[0]); + rp = &fds[0]; } else rp = 0; @@ -6671,7 +6671,7 @@ select_internal(VALUE read, VALUE write, VALUE except, struct timeval *tp, rb_fd rb_fd_set(fptr->fd, &fds[1]); if (max < fptr->fd) max = fptr->fd; } - wp = rb_fd_ptr(&fds[1]); + wp = &fds[1]; } else wp = 0; @@ -6690,7 +6690,7 @@ select_internal(VALUE read, VALUE write, VALUE except, struct timeval *tp, rb_fd if (max < fptr->fd) max = fptr->fd; } } - ep = rb_fd_ptr(&fds[2]); + ep = &fds[2]; } else { ep = 0; @@ -6698,7 +6698,7 @@ select_internal(VALUE read, VALUE write, VALUE except, struct timeval *tp, rb_fd max++; - n = rb_thread_select(max, rp, wp, ep, tp); + n = rb_thread_fd_select(max, rp, wp, ep, tp); if (n < 0) { rb_sys_fail(0); } diff --git a/thread.c b/thread.c index 3627811bfb..dacd85b34f 100644 --- a/thread.c +++ b/thread.c @@ -2490,6 +2490,37 @@ rb_thread_select(int max, fd_set * read, fd_set * write, fd_set * except, } +int +rb_thread_fd_select(int max, rb_fdset_t * read, rb_fdset_t * write, rb_fdset_t * except, + struct timeval *timeout) +{ + fd_set *r = NULL, *w = NULL, *e = NULL; + + if (!read && !write && !except) { + if (!timeout) { + rb_thread_sleep_forever(); + return 0; + } + rb_thread_wait_for(*timeout); + return 0; + } + + if (read) { + rb_fd_resize(max - 1, read); + r = rb_fd_ptr(read); + } + if (write) { + rb_fd_resize(max - 1, write); + w = rb_fd_ptr(write); + } + if (except) { + rb_fd_resize(max - 1, except); + e = rb_fd_ptr(except); + } + return do_select(max, r, w, e, timeout); +} + + /* * for GC */