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

ruby-changes:47544

From: nobu <ko1@a...>
Date: Sat, 26 Aug 2017 09:30:09 +0900 (JST)
Subject: [ruby-changes:47544] nobu:r59660 (trunk): thread_win32.c: set thread name

nobu	2017-08-26 09:30:03 +0900 (Sat, 26 Aug 2017)

  New Revision: 59660

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

  Log:
    thread_win32.c: set thread name
    
    * thread_win32.c (native_set_another_thread_name): set thread name
      by SetThreadDescription.
    
    * win32/win32.c (rb_w32_set_thread_description): dynamically try
      SetThreadDescription.

  Modified files:
    trunk/thread.c
    trunk/thread_pthread.c
    trunk/thread_win32.c
    trunk/win32/win32.c
Index: thread_win32.c
===================================================================
--- thread_win32.c	(revision 59659)
+++ thread_win32.c	(revision 59660)
@@ -669,6 +669,10 @@ ubf_handle(void *ptr) https://github.com/ruby/ruby/blob/trunk/thread_win32.c#L669
     }
 }
 
+int rb_w32_set_thread_description(HANDLE th, const WCHAR *name);
+int rb_w32_set_thread_description_str(HANDLE th, VALUE name);
+#define native_set_another_thread_name rb_w32_set_thread_description_str
+
 static struct {
     HANDLE id;
     HANDLE lock;
@@ -679,6 +683,7 @@ static unsigned long __stdcall https://github.com/ruby/ruby/blob/trunk/thread_win32.c#L683
 timer_thread_func(void *dummy)
 {
     thread_debug("timer_thread\n");
+    rb_w32_set_thread_description(GetCurrentThread(), L"ruby-timer-thread");
     while (WaitForSingleObject(timer_thread.lock, TIME_QUANTUM_USEC/1000) ==
 	   WAIT_TIMEOUT) {
 	timer_thread_function(dummy);
Index: thread_pthread.c
===================================================================
--- thread_pthread.c	(revision 59659)
+++ thread_pthread.c	(revision 59660)
@@ -1540,6 +1540,17 @@ native_set_thread_name(rb_thread_t *th) https://github.com/ruby/ruby/blob/trunk/thread_pthread.c#L1540
 #endif
 }
 
+static VALUE
+native_set_another_thread_name(rb_nativethread_id_t thread_id, VALUE name)
+{
+#ifdef SET_ANOTHER_THREAD_NAME
+    const char *s = "";
+    if (!NIL_P(name)) s = RSTRING_PTR(name);
+    SET_ANOTHER_THREAD_NAME(thread_id, s);
+#endif
+    return name;
+}
+
 static void *
 thread_timer(void *p)
 {
Index: thread.c
===================================================================
--- thread.c	(revision 59659)
+++ thread.c	(revision 59660)
@@ -2932,9 +2932,6 @@ rb_thread_getname(VALUE thread) https://github.com/ruby/ruby/blob/trunk/thread.c#L2932
 static VALUE
 rb_thread_setname(VALUE thread, VALUE name)
 {
-#ifdef SET_ANOTHER_THREAD_NAME
-    const char *s = "";
-#endif
     rb_thread_t *target_th = rb_thread_ptr(thread);
 
     if (!NIL_P(name)) {
@@ -2946,16 +2943,11 @@ rb_thread_setname(VALUE thread, VALUE na https://github.com/ruby/ruby/blob/trunk/thread.c#L2943
 		     rb_enc_name(enc));
 	}
 	name = rb_str_new_frozen(name);
-#ifdef SET_ANOTHER_THREAD_NAME
-	s = RSTRING_PTR(name);
-#endif
     }
     target_th->name = name;
-#if defined(SET_ANOTHER_THREAD_NAME)
     if (threadptr_initialized(target_th)) {
-	SET_ANOTHER_THREAD_NAME(target_th->thread_id, s);
+	native_set_another_thread_name(target_th->thread_id, name);
     }
-#endif
     return name;
 }
 
Index: win32/win32.c
===================================================================
--- win32/win32.c	(revision 59659)
+++ win32/win32.c	(revision 59660)
@@ -7857,6 +7857,48 @@ rb_w32_pow(double x, double y) https://github.com/ruby/ruby/blob/trunk/win32/win32.c#L7857
 }
 #endif
 
+int
+rb_w32_set_thread_description(HANDLE th, const WCHAR *name)
+{
+    int result = FALSE;
+    typedef HRESULT (WINAPI *set_thread_description_func)(HANDLE, PCWSTR);
+    static set_thread_description_func set_thread_description =
+	(set_thread_description_func)-1;
+    if (set_thread_description == (set_thread_description_func)-1) {
+	set_thread_description = (set_thread_description_func)
+	    get_proc_address("kernel32", "SetThreadDescription", NULL);
+    }
+    if (set_thread_description != (set_thread_description_func)-1) {
+	result = set_thread_description(th, name);
+    }
+    return result;
+}
+
+int
+rb_w32_set_thread_description_str(HANDLE th, VALUE name)
+{
+    int idx, result = FALSE;
+    WCHAR *s;
+
+    if (NIL_P(name)) {
+	rb_w32_set_thread_description(th, L"");
+	return;
+    }
+    s = (WCHAR *)StringValueCStr(name);
+    idx = rb_enc_get_index(name);
+    if (idx == ENCINDEX_UTF_16LE) {
+	result = rb_w32_set_thread_description(th, s);
+    }
+    else {
+	name = rb_str_conv_enc(name, rb_enc_from_index(idx), rb_utf8_encoding());
+	s = mbstr_to_wstr(CP_UTF8, RSTRING_PTR(name), RSTRING_LEN(name)+1, NULL);
+	result = rb_w32_set_thread_description(th, s);
+	free(s);
+    }
+    RB_GC_GUARD(name);
+    return result;
+}
+
 VALUE (*const rb_f_notimplement_)(int, const VALUE *, VALUE) = rb_f_notimplement;
 
 #if RUBY_MSVCRT_VERSION < 120

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

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