1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* cont.c: separate Continuation and Fiber from core.

* ext/continuation/*, ext/fiber/*: ditto.
* include/ruby/ruby.h: remove rb_cFiber.
* include/ruby/intern.h: add the rb_fiber_new() declaration.
* enumerator.c (next_init): fix to use rb_fiber_new().
* test/ruby/test_enumerator.rb: remove next? tests.
* test/ruby/test_continuation.rb: add a require 'continuation'.
* test/ruby/test_fiber.rb: add a require 'fiber'.



git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13259 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
ko1 2007-08-25 02:03:44 +00:00
parent cfaf3d9a1a
commit 977d66ec99
12 changed files with 83 additions and 40 deletions

View file

@ -1,3 +1,21 @@
Sat Aug 25 10:59:19 2007 Koichi Sasada <ko1@atdot.net>
* cont.c: separate Continuation and Fiber from core.
* ext/continuation/*, ext/fiber/*: ditto.
* include/ruby/ruby.h: remove rb_cFiber.
* include/ruby/intern.h: add the rb_fiber_new() declaration.
* enumerator.c (next_init): fix to use rb_fiber_new().
* test/ruby/test_enumerator.rb: remove next? tests.
* test/ruby/test_continuation.rb: add a require 'continuation'.
* test/ruby/test_fiber.rb: add a require 'fiber'.
Sat Aug 25 10:20:28 2007 Nobuyoshi Nakada <nobu@ruby-lang.org>
* common.mk (prelude.o): depends on vm_core.h now.

61
cont.c
View file

@ -34,10 +34,9 @@ typedef struct rb_context_struct {
int alive;
} rb_context_t;
VALUE rb_cCont;
VALUE rb_cFiber;
VALUE rb_cFiberCore;
VALUE rb_eFiberError;
static VALUE rb_cContinuation;
static VALUE rb_cFiber;
static VALUE rb_eFiberError;
#define GetContPtr(obj, ptr) \
Data_Get_Struct(obj, rb_context_t, ptr)
@ -151,9 +150,6 @@ cont_new(VALUE klass)
contval = Data_Make_Struct(klass, rb_context_t,
cont_mark, cont_free, cont);
RUBY_GC_INFO("cont alloc: %p (klass: %s)\n", cont,
klass == rb_cFiber ? "Fiber": "Continuation");
cont->self = contval;
cont->alive = Qtrue;
@ -173,7 +169,7 @@ cont_capture(volatile int *stat)
volatile VALUE contval;
vm_stack_to_heap(th);
cont = cont_new(rb_cCont);
cont = cont_new(rb_cContinuation);
contval = cont->self;
sth = &cont->saved_thread;
@ -475,6 +471,12 @@ rb_cont_call(int argc, VALUE *argv, VALUE contval)
#define FIBER_STACK_SIZE (4 * 1024)
VALUE
rb_fiber_new(VALUE (*func)(ANYARGS), VALUE obj)
{
return rb_block_call(rb_cFiber, rb_intern("new"), 0, 0, func, obj);
}
static VALUE
rb_fiber_s_new(VALUE self)
{
@ -590,7 +592,7 @@ rb_fiber_current()
rb_thread_t *th = GET_THREAD();
if (th->fiber == 0) {
/* save root */
rb_context_t *cont = cont_new(rb_cFiberCore);
rb_context_t *cont = cont_new(rb_cFiber);
cont->prev = Qnil;
th->root_fiber = th->fiber = cont->self;
}
@ -720,30 +722,29 @@ rb_fiber_s_current(VALUE klass)
void
Init_Cont(void)
{
rb_cCont = rb_define_class("Continuation", rb_cObject);
rb_undef_alloc_func(rb_cCont);
rb_undef_method(CLASS_OF(rb_cCont), "new");
rb_define_method(rb_cCont, "call", rb_cont_call, -1);
rb_define_method(rb_cCont, "[]", rb_cont_call, -1);
rb_define_global_function("callcc", rb_callcc, 0);
rb_cFiber = rb_define_class("Fiber", rb_cObject);
rb_undef_alloc_func(rb_cFiber);
rb_define_method(rb_cFiber, "resume", rb_fiber_m_resume, -1);
rb_define_method(rb_cFiber, "alive?", rb_fiber_alive_p, 0);
rb_define_singleton_method(rb_cFiber, "current", rb_fiber_s_current, 0);
rb_define_singleton_method(rb_cFiber, "yield", rb_fiber_s_yield, -1);
rb_define_singleton_method(rb_cFiber, "new", rb_fiber_s_new, 0);
rb_cFiberCore = rb_define_class_under(rb_cFiber, "Core", rb_cObject);
rb_undef_alloc_func(rb_cFiberCore);
rb_define_method(rb_cFiberCore, "transfer", rb_fiber_m_transfer, -1);
rb_define_method(rb_cFiberCore, "alive?", rb_fiber_alive_p, 0);
rb_define_singleton_method(rb_cFiberCore, "current", rb_fiber_s_current, 0);
rb_define_singleton_method(rb_cFiberCore, "new", rb_fiber_s_new, 0);
rb_eFiberError = rb_define_class("FiberError", rb_eStandardError);
}
void
Init_Continuation_body(void)
{
rb_cContinuation = rb_define_class("Continuation", rb_cObject);
rb_undef_alloc_func(rb_cContinuation);
rb_undef_method(CLASS_OF(rb_cContinuation), "new");
rb_define_method(rb_cContinuation, "call", rb_cont_call, -1);
rb_define_method(rb_cContinuation, "[]", rb_cont_call, -1);
rb_define_global_function("callcc", rb_callcc, 0);
}
void
Init_Fiber_body(void)
{
rb_define_method(rb_cFiber, "resume", rb_fiber_m_resume, -1);
rb_define_method(rb_cFiber, "transfer", rb_fiber_m_transfer, -1);
rb_define_method(rb_cFiber, "alive?", rb_fiber_alive_p, 0);
rb_define_singleton_method(rb_cFiber, "yield", rb_fiber_s_yield, -1);
rb_define_singleton_method(rb_cFiber, "current", rb_fiber_s_current, 0);
}

View file

@ -391,7 +391,7 @@ next_init(VALUE obj, struct enumerator *e)
{
VALUE curr = rb_fiber_current();
e->dst = curr;
e->fib = rb_block_call(rb_cFiber, rb_intern("new"), 0, 0, next_i, obj);
e->fib = rb_fiber_new(next_i, obj);
}
/*

View file

@ -0,0 +1,8 @@
void Init_Continuation_body(void);
void
Init_continuation(void)
{
Init_Continuation_body();
}

View file

@ -0,0 +1,3 @@
require 'mkmf'
create_makefile('continuation')

3
ext/fiber/extconf.rb Normal file
View file

@ -0,0 +1,3 @@
require 'mkmf'
create_makefile('fiber')

8
ext/fiber/fiber.c Normal file
View file

@ -0,0 +1,8 @@
void Init_Fiber_body(void);
void
Init_fiber(void)
{
Init_Fiber_body();
}

View file

@ -148,6 +148,7 @@ VALUE rb_singleton_class(VALUE);
int rb_cmpint(VALUE, VALUE, VALUE);
NORETURN(void rb_cmperr(VALUE, VALUE));
/* cont.c */
VALUE rb_fiber_new(VALUE (*)(ANYARGS), VALUE);
VALUE rb_fiber_resume(VALUE fib, int argc, VALUE *args);
VALUE rb_fiber_yield(int argc, VALUE *args);
VALUE rb_fiber_current(void);

View file

@ -761,7 +761,6 @@ RUBY_EXTERN VALUE rb_cCont;
RUBY_EXTERN VALUE rb_cDir;
RUBY_EXTERN VALUE rb_cData;
RUBY_EXTERN VALUE rb_cFalseClass;
RUBY_EXTERN VALUE rb_cFiber;
RUBY_EXTERN VALUE rb_cFile;
RUBY_EXTERN VALUE rb_cFixnum;
RUBY_EXTERN VALUE rb_cFloat;

View file

@ -1,4 +1,6 @@
require 'test/unit'
require 'continuation'
require 'fiber'
class TestContinuation < Test::Unit::TestCase
def test_create

View file

@ -24,14 +24,13 @@ class TestEnumerator < Test::Unit::TestCase
assert_raise(StopIteration){e.next}
end
def test_next?
def test_loop
e = 3.times
assert_equal true, e.next?
3.times{|i|
assert_equal true, e.next?
assert_equal i, e.next
i = 0
loop{
assert_equal(i, e.next)
i += 1
}
assert_equal false, e.next?
end
def test_nested_itaration

View file

@ -1,4 +1,5 @@
require 'test/unit'
require 'fiber'
class TestFiber < Test::Unit::TestCase
def test_normal
@ -118,11 +119,11 @@ class TestFiber < Test::Unit::TestCase
def test_transfer
ary = []
f2 = nil
f1 = Fiber::Core.new{
f1 = Fiber.new{
ary << f2.transfer(:foo)
:ok
}
f2 = Fiber::Core.new{
f2 = Fiber.new{
ary << f1.transfer(:baz)
:ng
}