[macruby-changes] [2502] MacRuby/trunk

source_changes at macosforge.org source_changes at macosforge.org
Sun Sep 6 18:38:32 PDT 2009


Revision: 2502
          http://trac.macosforge.org/projects/ruby/changeset/2502
Author:   lsansonetti at apple.com
Date:     2009-09-06 18:38:32 -0700 (Sun, 06 Sep 2009)
Log Message:
-----------
exposing missing Thread methods

Modified Paths:
--------------
    MacRuby/trunk/thread.c
    MacRuby/trunk/vm.cpp
    MacRuby/trunk/vm.h

Modified: MacRuby/trunk/thread.c
===================================================================
--- MacRuby/trunk/thread.c	2009-09-06 05:25:46 UTC (rev 2501)
+++ MacRuby/trunk/thread.c	2009-09-07 01:38:32 UTC (rev 2502)
@@ -502,10 +502,9 @@
  */
 
 static VALUE
-rb_thread_abort_exc(VALUE thread)
+rb_thread_abort_exc(VALUE thread, SEL sel)
 {
-    // TODO
-    return Qnil;
+    return GetThreadPtr(thread)->abort_on_exception ? Qtrue : Qfalse;
 }
 
 /*
@@ -518,10 +517,10 @@
  */
 
 static VALUE
-rb_thread_abort_exc_set(VALUE thread, VALUE val)
+rb_thread_abort_exc_set(VALUE thread, SEL sel, VALUE val)
 {
-    // TODO
-    return Qnil;
+    GetThreadPtr(thread)->abort_on_exception = RTEST(val);
+    return val;
 }
 
 /*
@@ -643,10 +642,9 @@
  */
 
 static VALUE
-rb_thread_safe_level(VALUE thread)
+rb_thread_safe_level(VALUE thread, SEL sel)
 {
-    // TODO
-    return Qnil;
+    return INT2FIX(rb_vm_thread_safe_level(GetThreadPtr(thread)));
 }
 
 /*
@@ -780,10 +778,13 @@
  */
 
 static VALUE
-rb_thread_priority(VALUE thread)
+rb_thread_priority(VALUE thread, SEL sel)
 {
-    // TODO
-    return Qnil;
+    // FIXME this doesn't really minic what 1.9 does, but do we care?
+    struct sched_param param;
+    pthread_assert(pthread_getschedparam(GetThreadPtr(thread)->thread,
+		NULL, &param));
+    return INT2FIX(param.sched_priority);
 }
 
 /*
@@ -809,9 +810,29 @@
  */
 
 static VALUE
-rb_thread_priority_set(VALUE thread, VALUE prio)
+rb_thread_priority_set(VALUE thread, SEL sel, VALUE prio)
 {
-    // TODO
+    // FIXME this doesn't really minic what 1.9 does, but do we care?
+    int policy;
+    struct sched_param param;
+    pthread_assert(pthread_getschedparam(GetThreadPtr(thread)->thread,
+		&policy, &param));
+ 
+    const int max = sched_get_priority_max(policy);
+    const int min = sched_get_priority_min(policy);
+
+    int priority = FIX2INT(prio);
+    if (min > priority) {
+	priority = min;
+    }
+    else if (max > priority) {
+	priority = max;
+    }
+
+    param.sched_priority = priority;
+    pthread_assert(pthread_setschedparam(GetThreadPtr(thread)->thread,
+		policy, &param));
+
     return Qnil;
 }
 
@@ -1469,14 +1490,14 @@
     rb_objc_define_method(rb_cThread, "[]=", rb_thread_aset, 2);
     rb_objc_define_method(rb_cThread, "key?", rb_thread_key_p, 1);
     rb_objc_define_method(rb_cThread, "keys", rb_thread_keys, 0);
-    rb_define_method(rb_cThread, "priority", rb_thread_priority, 0);
-    rb_define_method(rb_cThread, "priority=", rb_thread_priority_set, 1);
+    rb_objc_define_method(rb_cThread, "priority", rb_thread_priority, 0);
+    rb_objc_define_method(rb_cThread, "priority=", rb_thread_priority_set, 1);
     rb_objc_define_method(rb_cThread, "status", rb_thread_status, 0);
     rb_objc_define_method(rb_cThread, "alive?", rb_thread_alive_p, 0);
     rb_objc_define_method(rb_cThread, "stop?", rb_thread_stop_p, 0);
-    rb_define_method(rb_cThread, "abort_on_exception", rb_thread_abort_exc, 0);
-    rb_define_method(rb_cThread, "abort_on_exception=", rb_thread_abort_exc_set, 1);
-    rb_define_method(rb_cThread, "safe_level", rb_thread_safe_level, 0);
+    rb_objc_define_method(rb_cThread, "abort_on_exception", rb_thread_abort_exc, 0);
+    rb_objc_define_method(rb_cThread, "abort_on_exception=", rb_thread_abort_exc_set, 1);
+    rb_objc_define_method(rb_cThread, "safe_level", rb_thread_safe_level, 0);
     rb_objc_define_method(rb_cThread, "group", rb_thread_group, 0);
 
     rb_objc_define_method(rb_cThread, "inspect", rb_thread_inspect, 0);

Modified: MacRuby/trunk/vm.cpp
===================================================================
--- MacRuby/trunk/vm.cpp	2009-09-06 05:25:46 UTC (rev 2501)
+++ MacRuby/trunk/vm.cpp	2009-09-07 01:38:32 UTC (rev 2502)
@@ -4769,6 +4769,13 @@
 }
 
 extern "C"
+int
+rb_vm_thread_safe_level(rb_vm_thread_t *thread)
+{
+    return ((RoxorVM *)thread->vm)->get_safe_level();
+}
+
+extern "C"
 void 
 rb_vm_set_safe_level(int level)
 {
@@ -5056,8 +5063,6 @@
     // Release the thread now.
     rb_objc_release((void *)thread);
 
-    pthread_cleanup_push(rb_vm_thread_destructor, (void *)thread);
-
     rb_vm_thread_t *t = GetThreadPtr(thread);
 
     // Normally the pthread ID is set into the VM structure in the other
@@ -5065,6 +5070,8 @@
     // assignment!
     t->thread = pthread_self();
 
+    pthread_cleanup_push(rb_vm_thread_destructor, (void *)thread);
+
     try {
 	VALUE val = rb_vm_block_eval(t->body, t->argc, t->argv);
 	GC_WB(&t->value, val);
@@ -5090,6 +5097,15 @@
     GET_CORE()->unregister_thread(thread);
     rb_objc_gc_unregister_thread();
 
+#if 0
+    if (t->exception != Qnil) {
+	if (t->abort_on_exception || GET_CORE()->get_abort_on_exception()) {
+ 	    // TODO: move the exception to the main thread
+	    //rb_exc_raise(t->exception);
+	}
+    }
+#endif
+
     return NULL;
 }
 
@@ -5167,6 +5183,7 @@
     t->exception = Qnil;
     t->status = THREAD_ALIVE;
     t->in_cond_wait = false;
+    t->abort_on_exception = false;
     t->group = Qnil; // will be set right after
     t->mutexes = Qnil;
 

Modified: MacRuby/trunk/vm.h
===================================================================
--- MacRuby/trunk/vm.h	2009-09-06 05:25:46 UTC (rev 2501)
+++ MacRuby/trunk/vm.h	2009-09-07 01:38:32 UTC (rev 2502)
@@ -142,6 +142,7 @@
     pthread_cond_t sleep_cond;
     rb_vm_thread_status_t status;
     bool in_cond_wait;
+    bool abort_on_exception;	// per-local state, global one is in RoxorCore
     VALUE locals;	// a Hash object or Qnil
     VALUE exception;	// killed-by exception or Qnil 
     VALUE group;	// always a ThreadGroup object
@@ -262,6 +263,7 @@
 VALUE rb_vm_loaded_features(void);
 int rb_vm_safe_level(void);
 void rb_vm_set_safe_level(int level);
+int rb_vm_thread_safe_level(rb_vm_thread_t *thread);
 VALUE rb_vm_top_self(void);
 void rb_vm_const_is_defined(ID path);
 VALUE rb_vm_resolve_const_value(VALUE val, VALUE klass, ID name);
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20090906/f9446808/attachment.html>


More information about the macruby-changes mailing list