summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorko1 <ko1@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2007-05-23 22:52:19 +0000
committerko1 <ko1@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2007-05-23 22:52:19 +0000
commit7c4ff2d3322753479b3974105e5100b6f4e43001 (patch)
tree826dcf85cb1bf4b8a3fae0448b0be1554e2e0205
parentd086dfc513d319bae72002c8bb7e9da9d6d7cf1e (diff)
* cont.c: support callcc which everyone love.
incomplete. please give me bug reports. * common.mk, inits.c, thread.c: ditto. * yarvcore.c: export thread_mark(). * yarvcore.h: disable value cache option. * eval_intern.h: set th_get_ruby_level_cfp to inline. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12380 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog13
-rw-r--r--common.mk6
-rw-r--r--cont.c302
-rw-r--r--inits.c1
-rw-r--r--thread.c101
-rw-r--r--version.h6
-rw-r--r--yarvcore.c10
7 files changed, 334 insertions, 105 deletions
diff --git a/ChangeLog b/ChangeLog
index e0a015ffff..dc52094cb1 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,16 @@
+Thu May 24 01:54:53 2007 Koichi Sasada <ko1@atdot.net>
+
+ * cont.c: support callcc which everyone love.
+ incomplete. please give me bug reports.
+
+ * common.mk, inits.c, thread.c: ditto.
+
+ * yarvcore.c: export thread_mark().
+
+ * yarvcore.h: disable value cache option.
+
+ * eval_intern.h: set th_get_ruby_level_cfp to inline.
+
Wed May 23 15:39:02 2007 Yukihiro Matsumoto <matz@ruby-lang.org>
* common.mk: add a rule for regsyntax.c.
diff --git a/common.mk b/common.mk
index a897642123..444e857419 100644
--- a/common.mk
+++ b/common.mk
@@ -75,6 +75,7 @@ OBJS = array.$(OBJEXT) \
vm_dump.$(OBJEXT) \
yarvcore.$(OBJEXT) \
thread.$(OBJEXT) \
+ cont.$(OBJEXT) \
$(MISSING)
SCRIPT_ARGS = --dest-dir="$(DESTDIR)" \
@@ -523,6 +524,11 @@ thread.$(OBJEXT): {$(VPATH)}thread.c {$(VPATH)}eval_intern.h \
{$(VPATH)}defines.h {$(VPATH)}intern.h {$(VPATH)}missing.h \
{$(VPATH)}node.h {$(VPATH)}util.h \
{$(VPATH)}rubysig.h {$(VPATH)}st.h {$(VPATH)}dln.h
+cont.$(OBJEXT): {$(VPATH)}cont.c {$(VPATH)}eval_intern.h \
+ {$(VPATH)}ruby.h {$(VPATH)}yarvcore.h config.h \
+ {$(VPATH)}defines.h {$(VPATH)}intern.h {$(VPATH)}missing.h \
+ {$(VPATH)}node.h {$(VPATH)}util.h \
+ {$(VPATH)}rubysig.h {$(VPATH)}st.h {$(VPATH)}dln.h
time.$(OBJEXT): {$(VPATH)}time.c {$(VPATH)}ruby.h config.h \
{$(VPATH)}defines.h {$(VPATH)}intern.h {$(VPATH)}missing.h
unicode.$(OBJEXT): {$(VPATH)}unicode.c {$(VPATH)}regenc.h \
diff --git a/cont.c b/cont.c
new file mode 100644
index 0000000000..099cdde070
--- /dev/null
+++ b/cont.c
@@ -0,0 +1,302 @@
+/**********************************************************************
+
+ cont.c -
+
+ $Author$
+ $Date$
+ created at: Thu May 23 09:03:43 2007
+
+ Copyright (C) 2007 Koichi Sasada
+
+**********************************************************************/
+
+#include "ruby.h"
+#include "yarvcore.h"
+#include "gc.h"
+#include "eval_intern.h"
+
+typedef struct rb_continuation_struct {
+ rb_thread_t saved_thread;
+ rb_jmpbuf_t jmpbuf;
+ VALUE retval;
+ VALUE *vm_stack;
+ VALUE *machine_stack;
+ VALUE *machine_stack_src;
+ int machine_stack_size;
+} rb_continuation_t;
+
+#define GetContPtr(obj, ptr) \
+ Data_Get_Struct(obj, rb_continuation_t, ptr)
+
+NOINLINE(static VALUE cont_capture(volatile int *stat));
+
+void rb_thread_mark(rb_thread_t *th);
+
+static void
+cont_mark(void *ptr)
+{
+ MARK_REPORT_ENTER("cont");
+ if (ptr) {
+ rb_continuation_t *cont = ptr;
+ rb_gc_mark(cont->retval);
+ rb_thread_mark(&cont->saved_thread);
+
+ if (cont->vm_stack) {
+ rb_gc_mark_locations(cont->vm_stack,
+ cont->vm_stack + cont->saved_thread.stack_size);
+ }
+
+ if (cont->machine_stack) {
+ rb_gc_mark_locations(cont->machine_stack,
+ cont->machine_stack + cont->machine_stack_size);
+ }
+ }
+ MARK_REPORT_LEAVE("cont");
+}
+
+static void
+cont_free(void *ptr)
+{
+ FREE_REPORT_ENTER("cont");
+ if (ptr) {
+ rb_continuation_t *cont = ptr;
+ FREE_UNLESS_NULL(cont->machine_stack);
+ FREE_UNLESS_NULL(cont->vm_stack);
+ ruby_xfree(ptr);
+ }
+ FREE_REPORT_LEAVE("cont");
+}
+
+static VALUE
+cont_capture(volatile int *stat)
+{
+ rb_continuation_t *cont;
+ VALUE contval;
+ rb_thread_t *th = GET_THREAD(), *sth;
+ int size;
+
+ contval = Data_Make_Struct(rb_cCont, rb_continuation_t,
+ cont_mark, cont_free, cont);
+
+ /* save context */
+ cont->saved_thread = *th;
+ sth = &cont->saved_thread;
+ sth->stack = 0; /* clear to skip GC marking */
+ cont->vm_stack = ALLOC_N(VALUE, sth->stack_size);
+ MEMCPY(cont->vm_stack, th->stack, VALUE, sth->stack_size);
+
+ rb_gc_set_stack_end(&th->machine_stack_end);
+ if (th->machine_stack_start > th->machine_stack_end) {
+ size = cont->machine_stack_size = th->machine_stack_start - th->machine_stack_end;
+ cont->machine_stack_src = th->machine_stack_end;
+ }
+ else {
+ size = cont->machine_stack_size = th->machine_stack_end - th->machine_stack_start;
+ cont->machine_stack_src = th->machine_stack_start;
+ }
+
+ cont->machine_stack = ALLOC_N(VALUE, size);
+ MEMCPY(cont->machine_stack, cont->machine_stack_src, VALUE, size);
+
+ if (ruby_setjmp(cont->jmpbuf)) {
+ VALUE retval;
+
+ retval = cont->retval;
+ cont->retval = Qnil;
+ *stat = 1;
+ return retval;
+ }
+ else {
+ *stat = 0;
+ return contval;
+ }
+}
+
+static void
+cont_restore_context_1(rb_continuation_t *cont)
+{
+ rb_thread_t *th = GET_THREAD(), *sth = &cont->saved_thread;
+
+ /* restore thread context */
+ MEMCPY(th->stack, cont->vm_stack, VALUE, sth->stack_size);
+
+ th->cfp = sth->cfp;
+ th->safe_level = sth->safe_level;
+ th->raised_flag = sth->raised_flag;
+ th->state = sth->state;
+ th->status = sth->status;
+ th->tag = sth->tag;
+
+ /* restore machine stack */
+ MEMCPY(cont->machine_stack_src, cont->machine_stack,
+ VALUE, cont->machine_stack_size);
+
+ ruby_longjmp(cont->jmpbuf, 1);
+}
+
+NORETURN(NOINLINE(static void restore_context_0(rb_continuation_t *, VALUE *)));
+
+static void
+cont_restore_context_0(rb_continuation_t *cont, VALUE *addr_in_prev_frame)
+{
+#define STACK_PAD_SIZE 1024
+ VALUE space[STACK_PAD_SIZE];
+
+#if STACK_GROW_DIRECTION < 0 /* downward */
+ if (addr_in_prev_frame > cont->machine_stack_src) {
+ cont_restore_context_0(cont, &space[0]);
+ }
+#elif STACK_GROW_DIRECTION > 0 /* upward */
+ if (addr_in_prev_frame < cont->machine_stack_src + cont->machine_stack_size) {
+ cont_restore_context_0(cont, &space[STACK_PAD_SIZE-1]);
+ }
+#else
+ if (addr_in_prev_frame > &space[0]) {
+ /* Stack grows downward */
+ if (addr_in_prev_frame > cont->saved_thread.machine_stack_src) {
+ cont_restore_context_0(cont, &space[0]);
+ }
+ }
+ else {
+ /* Stack grows upward */
+ if (addr_in_prev_frame < cont->machine_stack_src + cont->machine_stack_size) {
+ cont_restore_context_0(cont, &space[STACK_PAD_SIZE-1]);
+ }
+ }
+#endif
+ cont_restore_context_1(cont);
+}
+
+/*
+ * Document-class: Continuation
+ *
+ * Continuation objects are generated by
+ * <code>Kernel#callcc</code>. They hold a return address and execution
+ * context, allowing a nonlocal return to the end of the
+ * <code>callcc</code> block from anywhere within a program.
+ * Continuations are somewhat analogous to a structured version of C's
+ * <code>setjmp/longjmp</code> (although they contain more state, so
+ * you might consider them closer to threads).
+ *
+ * For instance:
+ *
+ * arr = [ "Freddie", "Herbie", "Ron", "Max", "Ringo" ]
+ * callcc{|$cc|}
+ * puts(message = arr.shift)
+ * $cc.call unless message =~ /Max/
+ *
+ * <em>produces:</em>
+ *
+ * Freddie
+ * Herbie
+ * Ron
+ * Max
+ *
+ * This (somewhat contrived) example allows the inner loop to abandon
+ * processing early:
+ *
+ * callcc {|cont|
+ * for i in 0..4
+ * print "\n#{i}: "
+ * for j in i*5...(i+1)*5
+ * cont.call() if j == 17
+ * printf "%3d", j
+ * end
+ * end
+ * }
+ * print "\n"
+ *
+ * <em>produces:</em>
+ *
+ * 0: 0 1 2 3 4
+ * 1: 5 6 7 8 9
+ * 2: 10 11 12 13 14
+ * 3: 15 16
+ */
+
+VALUE rb_cCont;
+
+/*
+ * call-seq:
+ * callcc {|cont| block } => obj
+ *
+ * Generates a <code>Continuation</code> object, which it passes to the
+ * associated block. Performing a <em>cont</em><code>.call</code> will
+ * cause the <code>callcc</code> to return (as will falling through the
+ * end of the block). The value returned by the <code>callcc</code> is
+ * the value of the block, or the value passed to
+ * <em>cont</em><code>.call</code>. See class <code>Continuation</code>
+ * for more details. Also see <code>Kernel::throw</code> for
+ * an alternative mechanism for unwinding a call stack.
+ */
+
+static VALUE
+rb_callcc(VALUE self)
+{
+ volatile int called;
+ volatile VALUE val = cont_capture(&called);
+
+ if (called) {
+ return val;
+ }
+ else {
+ return rb_yield(val);
+ }
+}
+
+/*
+ * call-seq:
+ * cont.call(args, ...)
+ * cont[args, ...]
+ *
+ * Invokes the continuation. The program continues from the end of the
+ * <code>callcc</code> block. If no arguments are given, the original
+ * <code>callcc</code> returns <code>nil</code>. If one argument is
+ * given, <code>callcc</code> returns it. Otherwise, an array
+ * containing <i>args</i> is returned.
+ *
+ * callcc {|cont| cont.call } #=> nil
+ * callcc {|cont| cont.call 1 } #=> 1
+ * callcc {|cont| cont.call 1, 2, 3 } #=> [1, 2, 3]
+ */
+
+static VALUE
+rb_cont_call(int argc, VALUE *argv, VALUE contval)
+{
+ rb_continuation_t *cont;
+ rb_thread_t *th = GET_THREAD();
+ GetContPtr(contval, cont);
+
+ if (cont->saved_thread.value != th->value) {
+ rb_raise(rb_eRuntimeError, "continuation called across threads");
+ }
+ /* TODO: check "continuation called across trap" */
+ printf("--> %p\n", cont);
+
+ switch(argc) {
+ case 0:
+ cont->retval = Qnil;
+ break;
+ case 1:
+ cont->retval = argv[0];
+ break;
+ default:
+ cont->retval = rb_ary_new4(argc, argv);
+ break;
+ }
+
+ cont_restore_context_0(cont, (VALUE *)&cont);
+ return Qnil; /* unreachable */
+}
+
+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);
+}
+
diff --git a/inits.c b/inits.c
index 74115b1d92..e6b7572399 100644
--- a/inits.c
+++ b/inits.c
@@ -87,5 +87,6 @@ rb_call_inits()
Init_VM();
Init_ISeq();
Init_Thread();
+ Init_Cont();
Init_version();
}
diff --git a/thread.c b/thread.c
index 230439b722..296e6eb146 100644
--- a/thread.c
+++ b/thread.c
@@ -2444,100 +2444,6 @@ rb_barrier_release(VALUE self)
return n ? UINT2NUM(n) : Qfalse;
}
-
-/*
- * Document-class: Continuation
- *
- * Continuation objects are generated by
- * <code>Kernel#callcc</code>. They hold a return address and execution
- * context, allowing a nonlocal return to the end of the
- * <code>callcc</code> block from anywhere within a program.
- * Continuations are somewhat analogous to a structured version of C's
- * <code>setjmp/longjmp</code> (although they contain more state, so
- * you might consider them closer to threads).
- *
- * For instance:
- *
- * arr = [ "Freddie", "Herbie", "Ron", "Max", "Ringo" ]
- * callcc{|$cc|}
- * puts(message = arr.shift)
- * $cc.call unless message =~ /Max/
- *
- * <em>produces:</em>
- *
- * Freddie
- * Herbie
- * Ron
- * Max
- *
- * This (somewhat contrived) example allows the inner loop to abandon
- * processing early:
- *
- * callcc {|cont|
- * for i in 0..4
- * print "\n#{i}: "
- * for j in i*5...(i+1)*5
- * cont.call() if j == 17
- * printf "%3d", j
- * end
- * end
- * }
- * print "\n"
- *
- * <em>produces:</em>
- *
- * 0: 0 1 2 3 4
- * 1: 5 6 7 8 9
- * 2: 10 11 12 13 14
- * 3: 15 16
- */
-
-VALUE rb_cCont;
-
-/*
- * call-seq:
- * callcc {|cont| block } => obj
- *
- * Generates a <code>Continuation</code> object, which it passes to the
- * associated block. Performing a <em>cont</em><code>.call</code> will
- * cause the <code>callcc</code> to return (as will falling through the
- * end of the block). The value returned by the <code>callcc</code> is
- * the value of the block, or the value passed to
- * <em>cont</em><code>.call</code>. See class <code>Continuation</code>
- * for more details. Also see <code>Kernel::throw</code> for
- * an alternative mechanism for unwinding a call stack.
- */
-
-static VALUE
-rb_callcc(VALUE self)
-{
- rb_notimplement();
- return Qnil;
-}
-
-/*
- * call-seq:
- * cont.call(args, ...)
- * cont[args, ...]
- *
- * Invokes the continuation. The program continues from the end of the
- * <code>callcc</code> block. If no arguments are given, the original
- * <code>callcc</code> returns <code>nil</code>. If one argument is
- * given, <code>callcc</code> returns it. Otherwise, an array
- * containing <i>args</i> is returned.
- *
- * callcc {|cont| cont.call } #=> nil
- * callcc {|cont| cont.call 1 } #=> 1
- * callcc {|cont| cont.call 1, 2, 3 } #=> [1, 2, 3]
- */
-
-static VALUE
-rb_cont_call(int argc, VALUE *argv, VALUE cont)
-{
- rb_notimplement();
- return Qnil;
-}
-
/* variables for recursive traversals */
static ID recursive_key;
@@ -3001,13 +2907,6 @@ Init_Thread(void)
recursive_key = rb_intern("__recursive_key__");
rb_eThreadError = rb_define_class("ThreadError", rb_eStandardError);
- 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);
-
/* trace */
rb_define_global_function("set_trace_func", set_trace_func, 1);
rb_define_method(rb_cThread, "set_trace_func", thread_set_trace_func_m, 1);
diff --git a/version.h b/version.h
index ed65381b26..f9583e1f0b 100644
--- a/version.h
+++ b/version.h
@@ -1,7 +1,7 @@
#define RUBY_VERSION "1.9.0"
-#define RUBY_RELEASE_DATE "2007-05-23"
+#define RUBY_RELEASE_DATE "2007-05-24"
#define RUBY_VERSION_CODE 190
-#define RUBY_RELEASE_CODE 20070523
+#define RUBY_RELEASE_CODE 20070524
#define RUBY_PATCHLEVEL 0
#define RUBY_VERSION_MAJOR 1
@@ -9,7 +9,7 @@
#define RUBY_VERSION_TEENY 0
#define RUBY_RELEASE_YEAR 2007
#define RUBY_RELEASE_MONTH 5
-#define RUBY_RELEASE_DAY 23
+#define RUBY_RELEASE_DAY 24
#ifdef RUBY_EXTERN
RUBY_EXTERN const char ruby_version[];
diff --git a/yarvcore.c b/yarvcore.c
index 0d2dcecd81..21346f909e 100644
--- a/yarvcore.c
+++ b/yarvcore.c
@@ -192,7 +192,9 @@ vm_mark(void *ptr)
MARK_UNLESS_NULL(vm->mark_object_ary);
MARK_UNLESS_NULL(vm->last_status);
MARK_UNLESS_NULL(vm->loaded_features);
- if (vm->loading_table) rb_mark_tbl(vm->loading_table);
+ if (vm->loading_table) {
+ rb_mark_tbl(vm->loading_table);
+ }
mark_event_hooks(vm->event_hooks);
}
@@ -311,6 +313,12 @@ thread_mark(void *ptr)
MARK_REPORT_LEAVE("thread");
}
+void
+rb_thread_mark(void *ptr)
+{
+ thread_mark(ptr);
+}
+
static VALUE
thread_alloc(VALUE klass)
{