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:
		
							parent
							
								
									cfaf3d9a1a
								
							
						
					
					
						commit
						977d66ec99
					
				
					 12 changed files with 83 additions and 40 deletions
				
			
		
							
								
								
									
										18
									
								
								ChangeLog
									
										
									
									
									
								
							
							
						
						
									
										18
									
								
								ChangeLog
									
										
									
									
									
								
							|  | @ -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> | Sat Aug 25 10:20:28 2007  Nobuyoshi Nakada  <nobu@ruby-lang.org> | ||||||
| 
 | 
 | ||||||
| 	* common.mk (prelude.o): depends on vm_core.h now. | 	* common.mk (prelude.o): depends on vm_core.h now. | ||||||
|  |  | ||||||
							
								
								
									
										61
									
								
								cont.c
									
										
									
									
									
								
							
							
						
						
									
										61
									
								
								cont.c
									
										
									
									
									
								
							|  | @ -34,10 +34,9 @@ typedef struct rb_context_struct { | ||||||
|     int alive; |     int alive; | ||||||
| } rb_context_t; | } rb_context_t; | ||||||
| 
 | 
 | ||||||
| VALUE rb_cCont; | static VALUE rb_cContinuation; | ||||||
| VALUE rb_cFiber; | static VALUE rb_cFiber; | ||||||
| VALUE rb_cFiberCore; | static VALUE rb_eFiberError; | ||||||
| VALUE rb_eFiberError; |  | ||||||
| 
 | 
 | ||||||
| #define GetContPtr(obj, ptr)  \ | #define GetContPtr(obj, ptr)  \ | ||||||
|   Data_Get_Struct(obj, rb_context_t, 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, |     contval = Data_Make_Struct(klass, rb_context_t, | ||||||
| 			       cont_mark, cont_free, cont); | 			       cont_mark, cont_free, cont); | ||||||
| 
 | 
 | ||||||
|     RUBY_GC_INFO("cont alloc: %p (klass: %s)\n", cont, |  | ||||||
| 		 klass == rb_cFiber ? "Fiber": "Continuation"); |  | ||||||
| 
 |  | ||||||
|     cont->self = contval; |     cont->self = contval; | ||||||
|     cont->alive = Qtrue; |     cont->alive = Qtrue; | ||||||
| 
 | 
 | ||||||
|  | @ -173,7 +169,7 @@ cont_capture(volatile int *stat) | ||||||
|     volatile VALUE contval; |     volatile VALUE contval; | ||||||
| 
 | 
 | ||||||
|     vm_stack_to_heap(th); |     vm_stack_to_heap(th); | ||||||
|     cont = cont_new(rb_cCont); |     cont = cont_new(rb_cContinuation); | ||||||
|     contval = cont->self; |     contval = cont->self; | ||||||
|     sth = &cont->saved_thread; |     sth = &cont->saved_thread; | ||||||
| 
 | 
 | ||||||
|  | @ -475,6 +471,12 @@ rb_cont_call(int argc, VALUE *argv, VALUE contval) | ||||||
| 
 | 
 | ||||||
| #define FIBER_STACK_SIZE (4 * 1024) | #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 | static VALUE | ||||||
| rb_fiber_s_new(VALUE self) | rb_fiber_s_new(VALUE self) | ||||||
| { | { | ||||||
|  | @ -590,7 +592,7 @@ rb_fiber_current() | ||||||
|     rb_thread_t *th = GET_THREAD(); |     rb_thread_t *th = GET_THREAD(); | ||||||
|     if (th->fiber == 0) { |     if (th->fiber == 0) { | ||||||
| 	/* save root */ | 	/* save root */ | ||||||
| 	rb_context_t *cont = cont_new(rb_cFiberCore); | 	rb_context_t *cont = cont_new(rb_cFiber); | ||||||
| 	cont->prev = Qnil; | 	cont->prev = Qnil; | ||||||
| 	th->root_fiber = th->fiber = cont->self; | 	th->root_fiber = th->fiber = cont->self; | ||||||
|     } |     } | ||||||
|  | @ -720,30 +722,29 @@ rb_fiber_s_current(VALUE klass) | ||||||
| void | void | ||||||
| Init_Cont(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_cFiber = rb_define_class("Fiber", rb_cObject); | ||||||
|     rb_undef_alloc_func(rb_cFiber); |     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_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); |     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); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | @ -391,7 +391,7 @@ next_init(VALUE obj, struct enumerator *e) | ||||||
| { | { | ||||||
|     VALUE curr = rb_fiber_current(); |     VALUE curr = rb_fiber_current(); | ||||||
|     e->dst = curr; |     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); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| /*
 | /*
 | ||||||
|  |  | ||||||
							
								
								
									
										8
									
								
								ext/continuation/continuation.c
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								ext/continuation/continuation.c
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,8 @@ | ||||||
|  | 
 | ||||||
|  | void Init_Continuation_body(void); | ||||||
|  | 
 | ||||||
|  | void | ||||||
|  | Init_continuation(void) | ||||||
|  | { | ||||||
|  |     Init_Continuation_body(); | ||||||
|  | } | ||||||
							
								
								
									
										3
									
								
								ext/continuation/extconf.rb
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								ext/continuation/extconf.rb
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,3 @@ | ||||||
|  | require 'mkmf' | ||||||
|  | create_makefile('continuation') | ||||||
|  | 
 | ||||||
							
								
								
									
										3
									
								
								ext/fiber/extconf.rb
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								ext/fiber/extconf.rb
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,3 @@ | ||||||
|  | require 'mkmf' | ||||||
|  | create_makefile('fiber') | ||||||
|  | 
 | ||||||
							
								
								
									
										8
									
								
								ext/fiber/fiber.c
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								ext/fiber/fiber.c
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,8 @@ | ||||||
|  | 
 | ||||||
|  | void Init_Fiber_body(void); | ||||||
|  | 
 | ||||||
|  | void | ||||||
|  | Init_fiber(void) | ||||||
|  | { | ||||||
|  |     Init_Fiber_body(); | ||||||
|  | } | ||||||
|  | @ -148,6 +148,7 @@ VALUE rb_singleton_class(VALUE); | ||||||
| int rb_cmpint(VALUE, VALUE, VALUE); | int rb_cmpint(VALUE, VALUE, VALUE); | ||||||
| NORETURN(void rb_cmperr(VALUE, VALUE)); | NORETURN(void rb_cmperr(VALUE, VALUE)); | ||||||
| /* cont.c */ | /* cont.c */ | ||||||
|  | VALUE rb_fiber_new(VALUE (*)(ANYARGS), VALUE); | ||||||
| VALUE rb_fiber_resume(VALUE fib, int argc, VALUE *args); | VALUE rb_fiber_resume(VALUE fib, int argc, VALUE *args); | ||||||
| VALUE rb_fiber_yield(int argc, VALUE *args); | VALUE rb_fiber_yield(int argc, VALUE *args); | ||||||
| VALUE rb_fiber_current(void); | VALUE rb_fiber_current(void); | ||||||
|  |  | ||||||
|  | @ -761,7 +761,6 @@ RUBY_EXTERN VALUE rb_cCont; | ||||||
| RUBY_EXTERN VALUE rb_cDir; | RUBY_EXTERN VALUE rb_cDir; | ||||||
| RUBY_EXTERN VALUE rb_cData; | RUBY_EXTERN VALUE rb_cData; | ||||||
| RUBY_EXTERN VALUE rb_cFalseClass; | RUBY_EXTERN VALUE rb_cFalseClass; | ||||||
| RUBY_EXTERN VALUE rb_cFiber; |  | ||||||
| RUBY_EXTERN VALUE rb_cFile; | RUBY_EXTERN VALUE rb_cFile; | ||||||
| RUBY_EXTERN VALUE rb_cFixnum; | RUBY_EXTERN VALUE rb_cFixnum; | ||||||
| RUBY_EXTERN VALUE rb_cFloat; | RUBY_EXTERN VALUE rb_cFloat; | ||||||
|  |  | ||||||
|  | @ -1,4 +1,6 @@ | ||||||
| require 'test/unit' | require 'test/unit' | ||||||
|  | require 'continuation' | ||||||
|  | require 'fiber' | ||||||
| 
 | 
 | ||||||
| class TestContinuation < Test::Unit::TestCase | class TestContinuation < Test::Unit::TestCase | ||||||
|   def test_create |   def test_create | ||||||
|  |  | ||||||
|  | @ -24,14 +24,13 @@ class TestEnumerator < Test::Unit::TestCase | ||||||
|     assert_raise(StopIteration){e.next} |     assert_raise(StopIteration){e.next} | ||||||
|   end |   end | ||||||
| 
 | 
 | ||||||
|   def test_next? |   def test_loop | ||||||
|     e = 3.times |     e = 3.times | ||||||
|     assert_equal true, e.next? |     i = 0 | ||||||
|     3.times{|i| |     loop{ | ||||||
|       assert_equal true, e.next? |       assert_equal(i, e.next) | ||||||
|       assert_equal i, e.next |       i += 1 | ||||||
|     } |     } | ||||||
|     assert_equal false, e.next? |  | ||||||
|   end |   end | ||||||
| 
 | 
 | ||||||
|   def test_nested_itaration |   def test_nested_itaration | ||||||
|  |  | ||||||
|  | @ -1,4 +1,5 @@ | ||||||
| require 'test/unit' | require 'test/unit' | ||||||
|  | require 'fiber' | ||||||
| 
 | 
 | ||||||
| class TestFiber < Test::Unit::TestCase | class TestFiber < Test::Unit::TestCase | ||||||
|   def test_normal |   def test_normal | ||||||
|  | @ -118,11 +119,11 @@ class TestFiber < Test::Unit::TestCase | ||||||
|   def test_transfer |   def test_transfer | ||||||
|     ary = [] |     ary = [] | ||||||
|     f2 = nil |     f2 = nil | ||||||
|     f1 = Fiber::Core.new{ |     f1 = Fiber.new{ | ||||||
|       ary << f2.transfer(:foo) |       ary << f2.transfer(:foo) | ||||||
|       :ok |       :ok | ||||||
|     } |     } | ||||||
|     f2 = Fiber::Core.new{ |     f2 = Fiber.new{ | ||||||
|       ary << f1.transfer(:baz) |       ary << f1.transfer(:baz) | ||||||
|       :ng |       :ng | ||||||
|     } |     } | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 ko1
						ko1