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

ruby-changes:34324

From: nobu <ko1@a...>
Date: Wed, 11 Jun 2014 17:37:51 +0900 (JST)
Subject: [ruby-changes:34324] nobu:r46405 (trunk): thread_pthread.c: timer thread flag

nobu	2014-06-11 17:37:44 +0900 (Wed, 11 Jun 2014)

  New Revision: 46405

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

  Log:
    thread_pthread.c: timer thread flag
    
    * thread_pthread.c (timer_thread): add a flag to tell timer thread
      is created, since 0 may be a valid value as pthread_t.

  Modified files:
    trunk/thread.c
    trunk/thread_pthread.c
    trunk/thread_win32.c
Index: thread_win32.c
===================================================================
--- thread_win32.c	(revision 46404)
+++ thread_win32.c	(revision 46405)
@@ -692,14 +692,17 @@ ubf_handle(void *ptr) https://github.com/ruby/ruby/blob/trunk/thread_win32.c#L692
     w32_set_event(th->native_thread_data.interrupt_event);
 }
 
-static HANDLE timer_thread_id = 0;
-static HANDLE timer_thread_lock;
+static struct {
+    HANDLE id;
+    HANDLE lock;
+} timer_thread;
+#define TIMER_THREAD_CREATED_P() (timer_thread.id != 0)
 
 static unsigned long __stdcall
 timer_thread_func(void *dummy)
 {
     thread_debug("timer_thread\n");
-    while (WaitForSingleObject(timer_thread_lock, TIME_QUANTUM_USEC/1000) ==
+    while (WaitForSingleObject(timer_thread.lock, TIME_QUANTUM_USEC/1000) ==
 	   WAIT_TIMEOUT) {
 	timer_thread_function(dummy);
     }
@@ -716,13 +719,13 @@ rb_thread_wakeup_timer_thread(void) https://github.com/ruby/ruby/blob/trunk/thread_win32.c#L719
 static void
 rb_thread_create_timer_thread(void)
 {
-    if (timer_thread_id == 0) {
-	if (!timer_thread_lock) {
-	    timer_thread_lock = CreateEvent(0, TRUE, FALSE, 0);
+    if (timer_thread.id == 0) {
+	if (!timer_thread.lock) {
+	    timer_thread.lock = CreateEvent(0, TRUE, FALSE, 0);
 	}
-	timer_thread_id = w32_create_thread(1024 + (THREAD_DEBUG ? BUFSIZ : 0),
+	timer_thread.id = w32_create_thread(1024 + (THREAD_DEBUG ? BUFSIZ : 0),
 					    timer_thread_func, 0);
-	w32_resume_thread(timer_thread_id);
+	w32_resume_thread(timer_thread.id);
     }
 }
 
@@ -731,10 +734,10 @@ native_stop_timer_thread(int close_anywa https://github.com/ruby/ruby/blob/trunk/thread_win32.c#L734
 {
     int stopped = --system_working <= 0;
     if (stopped) {
-	SetEvent(timer_thread_lock);
-	native_thread_join(timer_thread_id);
-	CloseHandle(timer_thread_lock);
-	timer_thread_lock = 0;
+	SetEvent(timer_thread.lock);
+	native_thread_join(timer_thread.id);
+	CloseHandle(timer_thread.lock);
+	timer_thread.lock = 0;
     }
     return stopped;
 }
@@ -742,9 +745,9 @@ native_stop_timer_thread(int close_anywa https://github.com/ruby/ruby/blob/trunk/thread_win32.c#L745
 static void
 native_reset_timer_thread(void)
 {
-    if (timer_thread_id) {
-	CloseHandle(timer_thread_id);
-	timer_thread_id = 0;
+    if (timer_thread.id) {
+	CloseHandle(timer_thread.id);
+	timer_thread.id = 0;
     }
 }
 
Index: thread_pthread.c
===================================================================
--- thread_pthread.c	(revision 46404)
+++ thread_pthread.c	(revision 46405)
@@ -45,7 +45,11 @@ static void native_cond_wait(rb_nativeth https://github.com/ruby/ruby/blob/trunk/thread_pthread.c#L45
 static void native_cond_initialize(rb_nativethread_cond_t *cond, int flags);
 static void native_cond_destroy(rb_nativethread_cond_t *cond);
 static void rb_thread_wakeup_timer_thread_low(void);
-static pthread_t timer_thread_id;
+static struct {
+    pthread_t id;
+    int created;
+} timer_thread;
+#define TIMER_THREAD_CREATED_P() (timer_thread.created != 0)
 
 #define RB_CONDATTR_CLOCK_MONOTONIC 1
 
@@ -1172,7 +1176,7 @@ ubf_select(void *ptr) https://github.com/ruby/ruby/blob/trunk/thread_pthread.c#L1176
      * In the other hands, we shouldn't call rb_thread_wakeup_timer_thread()
      * if running on timer thread because it may make endless wakeups.
      */
-    if (!pthread_equal(pthread_self(), timer_thread_id))
+    if (!pthread_equal(pthread_self(), timer_thread.id))
 	rb_thread_wakeup_timer_thread();
     ubf_select_each(th);
 }
@@ -1477,7 +1481,7 @@ thread_timer(void *p) https://github.com/ruby/ruby/blob/trunk/thread_pthread.c#L1481
 static void
 rb_thread_create_timer_thread(void)
 {
-    if (!timer_thread_id) {
+    if (!timer_thread.created) {
 	int err;
 #ifdef HAVE_PTHREAD_ATTR_INIT
 	pthread_attr_t attr;
@@ -1507,18 +1511,19 @@ rb_thread_create_timer_thread(void) https://github.com/ruby/ruby/blob/trunk/thread_pthread.c#L1511
 #endif /* USE_SLEEPY_TIMER_THREAD */
 
 	/* create timer thread */
-	if (timer_thread_id) {
+	if (timer_thread.created) {
 	    rb_bug("rb_thread_create_timer_thread: Timer thread was already created\n");
 	}
 #ifdef HAVE_PTHREAD_ATTR_INIT
-	err = pthread_create(&timer_thread_id, &attr, thread_timer, &GET_VM()->gvl);
+	err = pthread_create(&timer_thread.id, &attr, thread_timer, &GET_VM()->gvl);
 #else
-	err = pthread_create(&timer_thread_id, NULL, thread_timer, &GET_VM()->gvl);
+	err = pthread_create(&timer_thread.id, NULL, thread_timer, &GET_VM()->gvl);
 #endif
 	if (err != 0) {
 	    fprintf(stderr, "[FATAL] Failed to create timer thread: %s\n", strerror(err));
 	    exit(EXIT_FAILURE);
 	}
+	timer_thread.created = 1;
 #ifdef HAVE_PTHREAD_ATTR_INIT
 	pthread_attr_destroy(&attr);
 #endif
@@ -1535,9 +1540,9 @@ native_stop_timer_thread(int close_anywa https://github.com/ruby/ruby/blob/trunk/thread_pthread.c#L1540
     if (stopped) {
 	/* join */
 	rb_thread_wakeup_timer_thread();
-	native_thread_join(timer_thread_id);
+	native_thread_join(timer_thread.id);
 	if (TT_DEBUG) fprintf(stderr, "joined timer thread\n");
-	timer_thread_id = 0;
+	timer_thread.created = 0;
 
 	/* close communication pipe */
 	if (close_anyway) {
Index: thread.c
===================================================================
--- thread.c	(revision 46404)
+++ thread.c	(revision 46405)
@@ -3725,7 +3725,7 @@ timer_thread_function(void *arg) https://github.com/ruby/ruby/blob/trunk/thread.c#L3725
 void
 rb_thread_stop_timer_thread(int close_anyway)
 {
-    if (timer_thread_id && native_stop_timer_thread(close_anyway)) {
+    if (TIMER_THREAD_CREATED_P() && native_stop_timer_thread(close_anyway)) {
 	native_reset_timer_thread();
     }
 }

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

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