[前][次][番号順一覧][スレッド一覧]

ruby-changes:27633

From: kosaki <ko1@a...>
Date: Sun, 10 Mar 2013 13:00:21 +0900 (JST)
Subject: [ruby-changes:27633] kosaki:r39685 (trunk): * thread_pthread.c (rb_thread_wakeup_timer_thread_fd): add fd

kosaki	2013-03-10 13:00:10 +0900 (Sun, 10 Mar 2013)

  New Revision: 39685

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=39685

  Log:
    * thread_pthread.c (rb_thread_wakeup_timer_thread_fd): add fd
      argument and remove hardcoded dependency of timer_thread_pipe[1].
    * thread_pthread.c (consume_communication_pipe): add fd argument.
    * thread_pthread.c (close_communication_pipe): ditto.
    
    * thread_pthread.c (timer_thread_sleep): adjust the above changes.
    
    * thread_pthread.c (setup_communication_pipe_internal): factor
      out pipe initialize logic.

  Modified files:
    trunk/ChangeLog
    trunk/thread_pthread.c

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 39684)
+++ ChangeLog	(revision 39685)
@@ -1,3 +1,15 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Wed Mar  6 22:36:19 2013  KOSAKI Motohiro  <kosaki.motohiro@g...>
+
+	* thread_pthread.c (rb_thread_wakeup_timer_thread_fd): add fd
+	  argument and remove hardcoded dependency of timer_thread_pipe[1].
+	* thread_pthread.c (consume_communication_pipe): add fd argument.
+	* thread_pthread.c (close_communication_pipe): ditto.
+
+	* thread_pthread.c (timer_thread_sleep): adjust the above changes.
+
+	* thread_pthread.c (setup_communication_pipe_internal): factor
+	  out pipe initialize logic.
+
 Wed Mar  6 22:56:14 2013  KOSAKI Motohiro  <kosaki.motohiro@g...>
 
 	* thread_pthread.c (ubf_select): add to small comments why we
Index: thread_pthread.c
===================================================================
--- thread_pthread.c	(revision 39684)
+++ thread_pthread.c	(revision 39685)
@@ -1155,10 +1155,9 @@ static int check_signal_thread_list(void https://github.com/ruby/ruby/blob/trunk/thread_pthread.c#L1155
 static int timer_thread_pipe[2] = {-1, -1};
 static int timer_thread_pipe_owner_process;
 
-
 /* only use signal-safe system calls here */
-void
-rb_thread_wakeup_timer_thread(void)
+static void
+rb_thread_wakeup_timer_thread_fd(int fd)
 {
     ssize_t result;
 
@@ -1166,7 +1165,7 @@ rb_thread_wakeup_timer_thread(void) https://github.com/ruby/ruby/blob/trunk/thread_pthread.c#L1165
     if (timer_thread_pipe_owner_process == getpid()) {
 	const char *buff = "!";
       retry:
-	if ((result = write(timer_thread_pipe[1], buff, 1)) <= 0) {
+	if ((result = write(fd, buff, 1)) <= 0) {
 	    switch (errno) {
 	      case EINTR: goto retry;
 	      case EAGAIN:
@@ -1185,9 +1184,15 @@ rb_thread_wakeup_timer_thread(void) https://github.com/ruby/ruby/blob/trunk/thread_pthread.c#L1184
     }
 }
 
+void
+rb_thread_wakeup_timer_thread(void)
+{
+    rb_thread_wakeup_timer_thread_fd(timer_thread_pipe[1]);
+}
+
 /* VM-dependent API is not available for this function */
 static void
-consume_communication_pipe(void)
+consume_communication_pipe(int fd)
 {
 #define CCP_READ_BUFF_SIZE 1024
     /* buffer can be shared because no one refers to them. */
@@ -1195,7 +1200,7 @@ consume_communication_pipe(void) https://github.com/ruby/ruby/blob/trunk/thread_pthread.c#L1200
     ssize_t result;
 
     while (1) {
-	result = read(timer_thread_pipe[0], buff, sizeof(buff));
+	result = read(fd, buff, sizeof(buff));
 	if (result == 0) {
 	    return;
 	}
@@ -1213,15 +1218,15 @@ consume_communication_pipe(void) https://github.com/ruby/ruby/blob/trunk/thread_pthread.c#L1218
 }
 
 static void
-close_communication_pipe(void)
+close_communication_pipe(int pipes[2])
 {
-    if (close(timer_thread_pipe[0]) < 0) {
+    if (close(pipes[0]) < 0) {
 	rb_bug_errno("native_stop_timer_thread - close(ttp[0])", errno);
     }
-    if (close(timer_thread_pipe[1]) < 0) {
+    if (close(pipes[1]) < 0) {
 	rb_bug_errno("native_stop_timer_thread - close(ttp[1])", errno);
     }
-    timer_thread_pipe[0] = timer_thread_pipe[1] = -1;
+    pipes[0] = pipes[1] = -1;
 }
 
 #if USE_SLEEPY_TIMER_THREAD
@@ -1241,35 +1246,44 @@ set_nonblock(int fd) https://github.com/ruby/ruby/blob/trunk/thread_pthread.c#L1246
 }
 #endif
 
+#if USE_SLEEPY_TIMER_THREAD
 static void
-setup_communication_pipe(void)
+setup_communication_pipe_internal(int pipes[2])
 {
-#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();
-	}
+    if (pipes[0] != -1) {
+	/* close pipe of parent process */
+	close_communication_pipe(pipes);
+    }
 
-	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]);
+    err = rb_cloexec_pipe(pipes);
+    if (err != 0) {
+	rb_bug_errno("setup_communication_pipe: Failed to create communication pipe for timer thread", errno);
+    }
+    rb_update_max_fd(pipes[0]);
+    rb_update_max_fd(pipes[1]);
+    set_nonblock(pipes[0]);
+    set_nonblock(pipes[1]);
+}
+#endif /* USE_SLEEPY_TIMER_THREAD */
 
-	/* validate pipe on this process */
-	timer_thread_pipe_owner_process = getpid();
+/* communication pipe with timer thread and signal handler */
+static void
+setup_communication_pipe(void)
+{
+#if USE_SLEEPY_TIMER_THREAD
+    if (timer_thread_pipe_owner_process == getpid()) {
+	/* already set up. */
+	return;
     }
+    setup_communication_pipe_internal(timer_thread_pipe);
+
+    /* validate pipe on this process */
+    timer_thread_pipe_owner_process = getpid();
 #endif /* USE_SLEEPY_TIMER_THREAD */
 }
 
-
 /**
  * Let the timer thread sleep a while.
  *
@@ -1301,7 +1315,7 @@ timer_thread_sleep(rb_global_vm_lock_t* https://github.com/ruby/ruby/blob/trunk/thread_pthread.c#L1315
 	/* maybe timeout */
     }
     else if (result > 0) {
-	consume_communication_pipe();
+	consume_communication_pipe(timer_thread_pipe[0]);
     }
     else { /* result < 0 */
 	switch (errno) {

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]