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

ruby-changes:2352

From: ko1@a...
Date: 9 Nov 2007 10:12:30 +0900
Subject: [ruby-changes:2352] ko1 - Ruby:r13843 (trunk): * cont.c: add rb_context_t#type.

ko1	2007-11-09 10:11:49 +0900 (Fri, 09 Nov 2007)

  New Revision: 13843

  Modified files:
    trunk/ChangeLog
    trunk/cont.c

  Log:
    * cont.c: add rb_context_t#type.
    


  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/cont.c?r1=13843&r2=13842
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/ChangeLog?r1=13843&r2=13842

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 13842)
+++ ChangeLog	(revision 13843)
@@ -1,3 +1,7 @@
+Fri Nov  9 10:10:21 2007  Koichi Sasada  <ko1@a...>
+
+	* cont.c: add rb_context_t#type.
+
 Fri Nov  9 10:05:54 2007  Koichi Sasada  <ko1@a...>
 
 	* ruby.c (set_arg0): fix breaking environ bugs.
Index: cont.c
===================================================================
--- cont.c	(revision 13842)
+++ cont.c	(revision 13843)
@@ -15,6 +15,12 @@
 #include "gc.h"
 #include "eval_intern.h"
 
+enum context_type {
+    CONTINUATION_CONTEXT = 0,
+    FIBER_CONTEXT = 1,
+    ROOT_FIBER_CONTEXT = 2,
+};
+
 typedef struct rb_context_struct {
     VALUE self;
     VALUE value;
@@ -29,9 +35,9 @@
     rb_thread_t saved_thread;
     rb_jmpbuf_t jmpbuf;
     int machine_stack_size;
-    /* for cont */
     VALUE prev;
     int alive;
+    enum context_type type;
 } rb_context_t;
 
 static VALUE rb_cContinuation;
@@ -86,10 +92,11 @@
 	RUBY_FREE_UNLESS_NULL(cont->machine_register_stack);
 #endif
 	RUBY_FREE_UNLESS_NULL(cont->vm_stack);
-	if (cont->vm_stack && cont->saved_thread.local_storage) {
-	    /* fiber */
+
+	if (cont->type == FIBER_CONTEXT) {
 	    st_free_table(cont->saved_thread.local_storage);
 	}
+
 	ruby_xfree(ptr);
     }
     RUBY_FREE_LEAVE("cont");
@@ -205,14 +212,7 @@
     rb_thread_t *th = GET_THREAD(), *sth = &cont->saved_thread;
 
     /* restore thread context */
-    if (sth->stack) {
-	/* fiber */
-	th->stack = sth->stack;
-	th->stack_size = sth->stack_size;
-	th->local_storage = sth->local_storage;
-	th->fiber = cont->self;
-    }
-    else {
+    if (cont->type == CONTINUATION_CONTEXT) {
 	/* continuation */
 	VALUE fib;
 
@@ -227,6 +227,13 @@
 	}
 	MEMCPY(th->stack, cont->vm_stack, VALUE, sth->stack_size);
     }
+    else {
+	/* fiber */
+	th->stack = sth->stack;
+	th->stack_size = sth->stack_size;
+	th->local_storage = sth->local_storage;
+	th->fiber = cont->self;
+    }
 
     th->cfp = sth->cfp;
     th->safe_level = sth->safe_level;
@@ -476,15 +483,25 @@
 
 #define FIBER_VM_STACK_SIZE (4 * 1024)
 
+static rb_context_t *
+fiber_alloc(VALUE klass)
+{
+    rb_context_t *cont = cont_new(klass);
+
+    cont->type = FIBER_CONTEXT;
+    cont->prev = Qnil;
+
+    return cont;
+}
+
 static VALUE
-fiber_alloc(VALUE klass, VALUE proc)
+fiber_new(VALUE klass, VALUE proc)
 {
-    rb_context_t *cont = cont_new(klass);
+    rb_context_t *cont = fiber_alloc(klass);
     VALUE contval = cont->self;
     rb_thread_t *th = &cont->saved_thread;
 
     /* initialize */
-    cont->prev = Qnil;
     cont->vm_stack = 0;
 
     th->stack = 0;
@@ -517,13 +534,13 @@
 VALUE
 rb_fiber_new(VALUE (*func)(ANYARGS), VALUE obj)
 {
-    return fiber_alloc(rb_cFiber, rb_proc_new(func, obj));
+    return fiber_new(rb_cFiber, rb_proc_new(func, obj));
 }
 
 static VALUE
 rb_fiber_s_new(VALUE self)
 {
-    return fiber_alloc(self, rb_block_proc());
+    return fiber_new(self, rb_block_proc());
 }
 
 static VALUE
@@ -604,15 +621,15 @@
     rb_thread_t *th = GET_THREAD();
     if (th->fiber == 0) {
 	/* save root */
-	rb_context_t *cont = cont_new(rb_cFiber);
-	cont->prev = Qnil;
+	rb_context_t *cont = fiber_alloc(rb_cFiber);
+	cont->type = ROOT_FIBER_CONTEXT;
 	th->root_fiber = th->fiber = cont->self;
     }
     return th->fiber;
 }
 
 static VALUE
-cont_store(rb_context_t *next_cont)
+fiber_store(rb_context_t *next_cont)
 {
     rb_thread_t *th = GET_THREAD();
     rb_context_t *cont;
@@ -623,8 +640,8 @@
     }
     else {
 	/* create current fiber */
-	cont = cont_new(rb_cFiber); /* no need to allocate vm stack */
-	cont->prev = Qnil;
+	cont = fiber_alloc(rb_cFiber); /* no need to allocate vm stack */
+	cont->type = ROOT_FIBER_CONTEXT;
 	th->root_fiber = th->fiber = cont->self;
     }
 
@@ -665,7 +682,7 @@
 
     cont->value = make_passing_arg(argc, argv);
 
-    if ((value = cont_store(cont)) == Qundef) {
+    if ((value = fiber_store(cont)) == Qundef) {
 	cont_restore_0(cont, &value);
 	rb_bug("rb_fiber_resume: unreachable");
     }

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

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