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

ruby-changes:35588

From: akr <ko1@a...>
Date: Sun, 21 Sep 2014 14:14:54 +0900 (JST)
Subject: [ruby-changes:35588] akr:r47670 (trunk): * thread_pthread.c (native_set_thread_name): New function to

akr	2014-09-21 14:14:47 +0900 (Sun, 21 Sep 2014)

  New Revision: 47670

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

  Log:
    * thread_pthread.c (native_set_thread_name): New function to
      set thread name visible with ps command on GNU/Linux.
      Ex. ps -o %c -L
    
    * thread.c (thread_start_func_2): Call native_set_thread_name at
      beginning.
      (rb_thread_inspect_msg): Extract from rb_thread_inspect.

  Modified files:
    trunk/ChangeLog
    trunk/thread.c
    trunk/thread_pthread.c
    trunk/thread_win32.c
Index: thread_win32.c
===================================================================
--- thread_win32.c	(revision 47669)
+++ thread_win32.c	(revision 47670)
@@ -794,4 +794,9 @@ rb_nativethread_self(void) https://github.com/ruby/ruby/blob/trunk/thread_win32.c#L794
     return GetCurrentThread();
 }
 
+static void
+native_set_thread_name(rb_thread_t *th)
+{
+}
+
 #endif /* THREAD_SYSTEM_DEPENDENT_IMPLEMENTATION */
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 47669)
+++ ChangeLog	(revision 47670)
@@ -1,3 +1,13 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Sun Sep 21 14:11:23 2014  Tanaka Akira  <akr@f...>
+
+	* thread_pthread.c (native_set_thread_name): New function to
+	  set thread name visible with ps command on GNU/Linux.
+	  Ex. ps -o %c -L
+
+	* thread.c (thread_start_func_2): Call native_set_thread_name at
+	  beginning.
+	  (rb_thread_inspect_msg): Extract from rb_thread_inspect.
+
 Sun Sep 21 12:49:11 2014  Eric Wong  <e@8...>
 
 	* iseq.c (rb_iseq_defined_string): trim redundant semi-colon
Index: thread_pthread.c
===================================================================
--- thread_pthread.c	(revision 47669)
+++ thread_pthread.c	(revision 47670)
@@ -1447,6 +1447,39 @@ timer_thread_sleep(rb_global_vm_lock_t* https://github.com/ruby/ruby/blob/trunk/thread_pthread.c#L1447
 # define SET_THREAD_NAME(name) (void)0
 #endif
 
+static VALUE rb_thread_inspect_msg(VALUE thread, int show_enclosure, int show_location, int show_status);
+
+static void
+native_set_thread_name(rb_thread_t *th)
+{
+#if defined(__linux__) && defined(PR_SET_NAME)
+    VALUE str;
+    char *name, *p;
+    char buf[16];
+    size_t len;
+
+    str = rb_thread_inspect_msg(th->self, 0, 1, 0);
+    name = StringValueCStr(str);
+    if (*name == '@')
+        name++;
+    p = strrchr(name, '/'); /* show only the basename of the path. */
+    if (p && p[1])
+        name = p + 1;
+
+    len = strlen(name);
+    if (len < sizeof(buf)) {
+        memcpy(buf, name, len);
+        buf[len] = '\0';
+    }
+    else {
+        memcpy(buf, name, sizeof(buf)-2);
+        buf[sizeof(buf)-2] = '*';
+        buf[sizeof(buf)-1] = '\0';
+    }
+    SET_THREAD_NAME(buf);
+#endif
+}
+
 static void *
 thread_timer(void *p)
 {
Index: thread.c
===================================================================
--- thread.c	(revision 47669)
+++ thread.c	(revision 47670)
@@ -571,6 +571,7 @@ thread_start_func_2(rb_thread_t *th, VAL https://github.com/ruby/ruby/blob/trunk/thread.c#L571
 	TH_PUSH_TAG(th);
 	if ((state = EXEC_TAG()) == 0) {
 	    SAVE_ROOT_JMPBUF(th, {
+                native_set_thread_name(th);
 		if (!th->first_func) {
 		    GetProcPtr(th->first_proc, proc);
 		    th->errinfo = Qnil;
@@ -2701,15 +2702,8 @@ rb_thread_safe_level(VALUE thread) https://github.com/ruby/ruby/blob/trunk/thread.c#L2702
     return INT2NUM(th->safe_level);
 }
 
-/*
- * call-seq:
- *   thr.inspect   -> string
- *
- * Dump the name, id, and status of _thr_ to a string.
- */
-
 static VALUE
-rb_thread_inspect(VALUE thread)
+rb_thread_inspect_msg(VALUE thread, int show_enclosure, int show_location, int show_status)
 {
     VALUE cname = rb_class_path(rb_obj_class(thread));
     rb_thread_t *th;
@@ -2718,8 +2712,11 @@ rb_thread_inspect(VALUE thread) https://github.com/ruby/ruby/blob/trunk/thread.c#L2712
 
     GetThreadPtr(thread, th);
     status = thread_status_name(th);
-    str = rb_sprintf("#<%"PRIsVALUE":%p", cname, (void *)thread);
-    if (!th->first_func && th->first_proc) {
+    if (show_enclosure)
+        str = rb_sprintf("#<%"PRIsVALUE":%p", cname, (void *)thread);
+    else
+        str = rb_str_new(NULL, 0);
+    if (show_location && !th->first_func && th->first_proc) {
 	long i;
 	VALUE v, loc = rb_proc_location(th->first_proc);
 	if (!NIL_P(loc)) {
@@ -2730,12 +2727,28 @@ rb_thread_inspect(VALUE thread) https://github.com/ruby/ruby/blob/trunk/thread.c#L2727
 	    }
 	}
     }
-    rb_str_catf(str, " %s>", status);
+    if (show_status || show_enclosure)
+        rb_str_catf(str, " %s%s",
+                show_status ? status : "",
+                show_enclosure ? ">" : "");
     OBJ_INFECT(str, thread);
 
     return str;
 }
 
+/*
+ * call-seq:
+ *   thr.inspect   -> string
+ *
+ * Dump the name, id, and status of _thr_ to a string.
+ */
+
+static VALUE
+rb_thread_inspect(VALUE thread)
+{
+    return rb_thread_inspect_msg(thread, 1, 1, 1);
+}
+
 static VALUE
 threadptr_local_aref(rb_thread_t *th, ID id)
 {

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

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