From 383a2579c3027e579f64e08597bae1097619baf4 Mon Sep 17 00:00:00 2001 From: kosaki Date: Sun, 10 Mar 2013 03:59:49 +0000 Subject: * thread_pthread.c (rb_thread_create_timer_thread): factor out creating communication pipe logic into separate function. * thread_pthread.c (setup_communication_pipe): new helper function. * thread_pthread.c (set_nonblock): moves a definition before setup_communication_pipe. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@39683 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- thread_pthread.c | 85 +++++++++++++++++++++++++++++++------------------------- 1 file changed, 47 insertions(+), 38 deletions(-) (limited to 'thread_pthread.c') diff --git a/thread_pthread.c b/thread_pthread.c index 4ae7e2abe4..c986f223c0 100644 --- a/thread_pthread.c +++ b/thread_pthread.c @@ -1216,6 +1216,52 @@ close_communication_pipe(void) timer_thread_pipe[0] = timer_thread_pipe[1] = -1; } +#if USE_SLEEPY_TIMER_THREAD +static void +set_nonblock(int fd) +{ + int oflags; + int err; + + oflags = fcntl(fd, F_GETFL); + if (oflags == -1) + rb_sys_fail(0); + oflags |= O_NONBLOCK; + err = fcntl(fd, F_SETFL, oflags); + if (err == -1) + rb_sys_fail(0); +} +#endif + +static void +setup_communication_pipe(void) +{ +#if USE_SLEEPY_TIMER_THREAD + int err; + + /* communication pipe with timer thread and signal handler */ + if (timer_thread_pipe_owner_process != getpid()) { + if (timer_thread_pipe[0] != -1) { + /* close pipe of parent process */ + close_communication_pipe(); + } + + err = rb_cloexec_pipe(timer_thread_pipe); + if (err != 0) { + rb_bug_errno("setup_communication_pipe: Failed to create communication pipe for timer thread", errno); + } + rb_update_max_fd(timer_thread_pipe[0]); + rb_update_max_fd(timer_thread_pipe[1]); + set_nonblock(timer_thread_pipe[0]); + set_nonblock(timer_thread_pipe[1]); + + /* validate pipe on this process */ + timer_thread_pipe_owner_process = getpid(); + } +#endif /* USE_SLEEPY_TIMER_THREAD */ +} + + /** * Let the timer thread sleep a while. * @@ -1318,23 +1364,6 @@ thread_timer(void *p) return NULL; } -static void -set_nonblock(int fd) -{ -#if defined(HAVE_FCNTL) && defined(F_GETFL) && defined(F_SETFL) && defined(O_NONBLOCK) - int oflags; - int err; - - oflags = fcntl(fd, F_GETFL); - if (oflags == -1) - rb_sys_fail(0); - oflags |= O_NONBLOCK; - err = fcntl(fd, F_SETFL, oflags); - if (err == -1) - rb_sys_fail(0); -#endif -} - static void rb_thread_create_timer_thread(void) { @@ -1364,27 +1393,7 @@ rb_thread_create_timer_thread(void) # endif #endif -#if USE_SLEEPY_TIMER_THREAD - /* communication pipe with timer thread and signal handler */ - if (timer_thread_pipe_owner_process != getpid()) { - if (timer_thread_pipe[0] != -1) { - /* close pipe of parent process */ - close_communication_pipe(); - } - - err = rb_cloexec_pipe(timer_thread_pipe); - if (err != 0) { - rb_bug_errno("thread_timer: Failed to create communication pipe for timer thread", errno); - } - rb_update_max_fd(timer_thread_pipe[0]); - rb_update_max_fd(timer_thread_pipe[1]); - set_nonblock(timer_thread_pipe[0]); - set_nonblock(timer_thread_pipe[1]); - - /* validate pipe on this process */ - timer_thread_pipe_owner_process = getpid(); - } -#endif /* USE_SLEEPY_TIMER_THREAD */ + setup_communication_pipe(); /* create timer thread */ if (timer_thread_id) { -- cgit v1.2.3