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

ruby-changes:7052

From: ko1 <ko1@a...>
Date: Wed, 13 Aug 2008 16:53:54 +0900 (JST)
Subject: [ruby-changes:7052] Ruby:r18570 (trunk): * thread.c, vm_core.h: add manual priority support

ko1	2008-08-13 16:53:35 +0900 (Wed, 13 Aug 2008)

  New Revision: 18570

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

  Log:
    * thread.c, vm_core.h: add manual priority support
      using time slice.  if you enable USE_NATIVE_THREAD_PRIORITY
      macro, this mechanism is ignored.  [ruby-dev:33124]
    * thread_pthread.c, thread_win32.c: ditto.
    * test/ruby/test_thread.rb: fix test parameter.

  Modified files:
    trunk/ChangeLog
    trunk/test/ruby/test_thread.rb
    trunk/thread.c
    trunk/thread_pthread.c
    trunk/thread_win32.c
    trunk/vm_core.h

Index: thread_win32.c
===================================================================
--- thread_win32.c	(revision 18569)
+++ thread_win32.c	(revision 18570)
@@ -499,6 +499,8 @@
     w32_wait_events(&th, 1, 0, 0);
 }
 
+#if USE_NATIVE_THREAD_PRIORITY
+
 static void
 native_thread_apply_priority(rb_thread_t *th)
 {
@@ -516,6 +518,8 @@
     SetThreadPriority(th->thread_id, priority);
 }
 
+#endif /* USE_NATIVE_THREAD_PRIORITY */
+
 static void
 ubf_handle(void *ptr)
 {
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 18569)
+++ ChangeLog	(revision 18570)
@@ -1,3 +1,13 @@
+Wed Aug 13 16:40:57 2008  Koichi Sasada  <ko1@a...>
+
+	* thread.c, vm_core.h: add manual priority support
+	  using time slice.  if you enable USE_NATIVE_THREAD_PRIORITY
+	  macro, this mechanism is ignored.  [ruby-dev:33124]
+
+	* thread_pthread.c, thread_win32.c: ditto.
+
+	* test/ruby/test_thread.rb: fix test parameter.
+
 Wed Aug 13 16:02:14 2008  Shugo Maeda  <shugo@r...>
 
 	* object.c (rb_obj_untrusted): new method Object#untrusted?.
Index: thread_pthread.c
===================================================================
--- thread_pthread.c	(revision 18569)
+++ thread_pthread.c	(revision 18570)
@@ -443,6 +443,9 @@
     }
 }
 
+
+#if USE_NATIVE_THREAD_PRIORITY
+
 static void
 native_thread_apply_priority(rb_thread_t *th)
 {
@@ -469,6 +472,8 @@
 #endif
 }
 
+#endif /* USE_NATIVE_THREAD_PRIORITY */
+
 static void
 ubf_pthread_cond_signal(void *ptr)
 {
Index: vm_core.h
===================================================================
--- vm_core.h	(revision 18569)
+++ vm_core.h	(revision 18570)
@@ -432,6 +432,7 @@
     rb_thread_id_t thread_id;
     enum rb_thread_status status;
     int priority;
+    int slice;
 
     native_thread_data_t native_thread_data;
 
Index: thread.c
===================================================================
--- thread.c	(revision 18569)
+++ thread.c	(revision 18570)
@@ -48,6 +48,12 @@
 #include "vm.h"
 #include "gc.h"
 
+#ifndef USE_NATIVE_THREAD_PRIORITY
+#define USE_NATIVE_THREAD_PRIORITY 0
+#define RUBY_THREAD_PRIORITY_MAX 3
+#define RUBY_THREAD_PRIORITY_MIN -3
+#endif
+
 #ifndef THREAD_DEBUG
 #define THREAD_DEBUG 0
 #endif
@@ -996,8 +1002,26 @@
 	}
 
 	if (timer_interrupt) {
+#if USE_NATIVE_THREAD_PRIORITY
 	    EXEC_EVENT_HOOK(th, RUBY_EVENT_SWITCH, th->cfp->self, 0, 0);
 	    rb_thread_schedule();
+#else
+	    if (th->slice > 0) {
+		th->slice--;
+	    }
+	    else {
+	      reschedule:
+		EXEC_EVENT_HOOK(th, RUBY_EVENT_SWITCH, th->cfp->self, 0, 0);
+		rb_thread_schedule();
+		if (th->slice < 0) {
+		    th->slice++;
+		    goto reschedule;
+		}
+		else {
+		    th->slice = th->priority;
+		}
+	    }
+#endif
 	}
     }
 }
@@ -1847,12 +1871,25 @@
 {
     rb_thread_t *th;
     GetThreadPtr(thread, th);
+    int priority;
 
     rb_secure(4);
 
+#if USE_NATIVE_THREAD_PRIORITY
     th->priority = NUM2INT(prio);
     native_thread_apply_priority(th);
-    return prio;
+#else
+    priority = NUM2INT(prio);
+    if (priority > RUBY_THREAD_PRIORITY_MAX) {
+	priority = RUBY_THREAD_PRIORITY_MAX;
+    }
+    else if (priority < RUBY_THREAD_PRIORITY_MIN) {
+	priority = RUBY_THREAD_PRIORITY_MIN;
+    }
+    th->priority = priority;
+    th->slice = priority;
+#endif
+    return INT2NUM(th->priority);
 }
 
 /* for IO */
Index: test/ruby/test_thread.rb
===================================================================
--- test/ruby/test_thread.rb	(revision 18569)
+++ test/ruby/test_thread.rb	(revision 18570)
@@ -126,7 +126,7 @@
     sleep 0.5
     t1.kill
     t2.kill
-    assert(c1 > c2 * 2, "[ruby-dev:33124]")
+    assert(c1 > c2 * 1.5, "[ruby-dev:33124]")
   end
 
   def test_new

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

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