ruby-changes:7490
From: ko1 <ko1@a...>
Date: Mon, 1 Sep 2008 11:39:27 +0900 (JST)
Subject: [ruby-changes:7490] Ruby:r19010 (mvm): * mvm.c: added.
ko1 2008-09-01 11:37:51 +0900 (Mon, 01 Sep 2008) New Revision: 19010 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=19010 Log: * mvm.c: added. * common.mk: ditto. * eval.c, eval_intern.h, include/ruby/intern.h, include/ruby/mvm.h, include/ruby/public_object.h, include/ruby/ruby.h, io.c, main.c, main.c, pack.c, ruby.c, thread.c, thread_pthread.c, thread_pthread.h, vm.c, vm_core.h: fix Ruby/MVM API. On this commit, only miniruby is built, but not working. Added files: branches/mvm/mvm.c Modified files: branches/mvm/ChangeLog branches/mvm/common.mk branches/mvm/eval.c branches/mvm/eval_intern.h branches/mvm/include/ruby/intern.h branches/mvm/include/ruby/mvm.h branches/mvm/include/ruby/public_object.h branches/mvm/include/ruby/ruby.h branches/mvm/io.c branches/mvm/main.c branches/mvm/pack.c branches/mvm/ruby.c branches/mvm/thread.c branches/mvm/thread_pthread.c branches/mvm/thread_pthread.h branches/mvm/version.h branches/mvm/vm.c branches/mvm/vm_core.h Index: mvm/eval_intern.h =================================================================== --- mvm/eval_intern.h (revision 19009) +++ mvm/eval_intern.h (revision 19010) @@ -231,5 +231,19 @@ void rb_thread_terminate_all(void); VALUE rb_vm_top_self(); VALUE rb_vm_cbase(void); +void rb_vm_thread_terminate_all(rb_vm_t *vm); +VALUE ruby_vm_argf(rb_vm_t *vm); + +void ruby_native_thread_lock_initialize(rb_thread_lock_t *lock); +void ruby_native_thread_lock_destroy(rb_thread_lock_t *lock); +void ruby_native_thread_lock(rb_thread_lock_t *lock); +void ruby_native_thread_unlock(rb_thread_lock_t *lock); +void ruby_native_thread_yield(void); + +VALUE ruby_vm_get_argv(rb_vm_t *vm); +const char *ruby_vm_get_inplace_mode(rb_vm_t *vm); +void ruby_vm_set_inplace_mode(rb_vm_t *vm, const char *suffix); +void ruby_vm_set_argv(rb_vm_t *vm, int argc, char **argv); + #endif /* RUBY_EVAL_INTERN_H */ Index: mvm/include/ruby/mvm.h =================================================================== --- mvm/include/ruby/mvm.h (revision 19009) +++ mvm/include/ruby/mvm.h (revision 19010) @@ -14,59 +14,31 @@ #define HAVE_MVM 1 -#if defined(_WIN32) -/* Win32 thread */ -typedef CRITICAL_SECTION rb_thread_lock_t; -#define RB_THREAD_LOCK_INITIALIZER {} -int ruby_native_thread_lock(rb_thread_lock_t*); -int ruby_native_thread_unlock(rb_thread_lock_t*); -int ruby_native_thread_yield(void); +/* VM type declaration */ +typedef struct rb_vm_struct rb_vm_t; -#elif defined(HAVE_PTHREAD_H) -/* pthread */ -#include <pthread.h> -typedef pthread_mutex_t rb_thread_lock_t; -#define RB_THREAD_LOCK_INITIALIZER PTHREAD_MUTEX_INITIALIZER -#define ruby_native_thread_lock(lock) pthread_mutex_lock(lock) -#define ruby_native_thread_unlock(lock) pthread_mutex_unlock(lock) -#ifdef HAVE_SCHED_YIELD -#define ruby_native_thread_yield() (void)sched_yield() -#else -#define ruby_native_thread_yield() ((void)0) -#endif +/* core API */ +rb_vm_t *ruby_vm_new(int argc, char *argv[]); +int ruby_vm_run(rb_vm_t *vm); +int ruby_vm_start(rb_vm_t *vm); +int ruby_vm_join(rb_vm_t *vm); +int ruby_vm_destruct(rb_vm_t *vm); -#endif +/* initialize API */ +int ruby_vm_init_add_argv(rb_vm_t *vm, const char *arg); +int ruby_vm_init_add_library(rb_vm_t *vm, const char *lib); +int ruby_vm_init_add_library_path(rb_vm_t *vm, const char *path); +int ruby_vm_init_add_expression(rb_vm_t *vm, const char *expr); +int ruby_vm_init_script(rb_vm_t *vm, const char *script); +int ruby_vm_init_verbose(rb_vm_t *vm, int verbose_p); +int ruby_vm_init_debug(rb_vm_t *vm, int debug); +int ruby_vm_init_add_initializer(rb_vm_t *vm, void (*initializer)(rb_vm_t *)); +int ruby_vm_init_stdin(rb_vm_t *vm, int fd); +int ruby_vm_init_stdout(rb_vm_t *vm, int fd); +int ruby_vm_init_stderr(rb_vm_t *vm, int fd); -void ruby_native_thread_lock_initialize(rb_thread_lock_t *); -void ruby_native_thread_lock_destroy(rb_thread_lock_t *); - -typedef struct rb_vm_struct rb_vm_t; -typedef struct rb_thread_struct rb_thread_t; - +/* other API */ void ruby_vm_foreach(int (*)(rb_vm_t *, void *), void *); /* returning false stops iteration */ +void *ruby_vm_specific_ptr(rb_vm_t *, int); -VALUE *ruby_vm_verbose_ptr(rb_vm_t *); -VALUE *ruby_vm_debug_ptr(rb_vm_t *); - -VALUE ruby_vm_get_argv(rb_vm_t*); -void ruby_vm_set_argv(rb_vm_t *, int, char **); -const char *ruby_vm_get_inplace_mode(rb_vm_t *); -void ruby_vm_set_inplace_mode(rb_vm_t *, const char *); -void ruby_vm_prog_init(rb_vm_t *); -VALUE ruby_vm_process_options(rb_vm_t *, int, char **); -VALUE ruby_vm_parse_options(rb_vm_t *, int, char **); - -void rb_vm_clear_trace_func(rb_vm_t *); -void rb_vm_thread_terminate_all(rb_vm_t *); - -rb_vm_t *ruby_vm_new(void); -int ruby_vm_run(rb_vm_t *, VALUE); - -int rb_vm_key_count(void); -int rb_vm_key_create(void); -VALUE *ruby_vm_specific_ptr(rb_vm_t *, int); -VALUE *rb_vm_specific_ptr(int); - -char *ruby_thread_getcwd(rb_thread_t *); - #endif /* RUBY_MVM_H */ Index: mvm/include/ruby/intern.h =================================================================== --- mvm/include/ruby/intern.h (revision 19009) +++ mvm/include/ruby/intern.h (revision 19010) @@ -735,6 +735,9 @@ void rb_frame_pop(void); int rb_frame_method_id_and_class(ID *idp, VALUE *klassp); +int rb_vm_key_count(void); +int rb_vm_key_create(void); + #if defined(__cplusplus) #if 0 { /* satisfy cc-mode */ Index: mvm/include/ruby/public_object.h =================================================================== --- mvm/include/ruby/public_object.h (revision 19009) +++ mvm/include/ruby/public_object.h (revision 19010) @@ -3,6 +3,40 @@ #include "ruby/mvm.h" +/* +public_vars = %w{ + mKernel mComparable mEnumerable mPrecision mErrno mFileTest mGC + mMath mProcess cBasicObject cObject cArray cBignum cBinding + cClass cCont cDir cData cFalseClass cEncoding cEnumerator cFile + cFixnum cFloat cHash cInteger cIO cMatch cMethod cModule + cNameErrorMesg cNilClass cNumeric cProc cRange cRational cComplex + cRegexp cStat cString cStruct cSymbol cThread cTime cTrueClass + cUnboundMethod cMutex cBarrier eException eStandardError + eSystemExit eInterrupt eSignal eFatal eArgError eEOFError + eIndexError eStopIteration eKeyError eRangeError eIOError + eRuntimeError eSecurityError eSystemCallError eThreadError + eTypeError eZeroDivError eNotImpError eNoMemError eNoMethodError + eFloatDomainError eLocalJumpError eSysStackError eRegexpError + eEncCompatError eScriptError eNameError eSyntaxError eLoadError + stdin stdout stderr argf +} + +public_vars2 = %w{ + verbose debug progname +} + +public_vars.each{|var| + puts " rb_vmkey_#{var}," + puts "\#define rb_#{var} (*((VALUE *)rb_vm_specific_ptr(rb_vmkey_#{var})))" +} +public_vars2.each{|var| + puts " rb_vmkey_#{var}," +} + + */ + +VALUE *rb_vm_specific_ptr(int key); + enum ruby_public_object_vmkey { rb_vmkey_mKernel, #define rb_mKernel (*rb_vm_specific_ptr(rb_vmkey_mKernel)) @@ -22,7 +56,6 @@ #define rb_mMath (*rb_vm_specific_ptr(rb_vmkey_mMath)) rb_vmkey_mProcess, #define rb_mProcess (*rb_vm_specific_ptr(rb_vmkey_mProcess)) - rb_vmkey_cBasicObject, #define rb_cBasicObject (*rb_vm_specific_ptr(rb_vmkey_cBasicObject)) rb_vmkey_cObject, @@ -101,7 +134,6 @@ #define rb_cMutex (*rb_vm_specific_ptr(rb_vmkey_cMutex)) rb_vmkey_cBarrier, #define rb_cBarrier (*rb_vm_specific_ptr(rb_vmkey_cBarrier)) - rb_vmkey_eException, #define rb_eException (*rb_vm_specific_ptr(rb_vmkey_eException)) rb_vmkey_eStandardError, @@ -156,7 +188,6 @@ #define rb_eRegexpError (*rb_vm_specific_ptr(rb_vmkey_eRegexpError)) rb_vmkey_eEncCompatError, #define rb_eEncCompatError (*rb_vm_specific_ptr(rb_vmkey_eEncCompatError)) - rb_vmkey_eScriptError, #define rb_eScriptError (*rb_vm_specific_ptr(rb_vmkey_eScriptError)) rb_vmkey_eNameError, @@ -165,7 +196,6 @@ #define rb_eSyntaxError (*rb_vm_specific_ptr(rb_vmkey_eSyntaxError)) rb_vmkey_eLoadError, #define rb_eLoadError (*rb_vm_specific_ptr(rb_vmkey_eLoadError)) - rb_vmkey_stdin, #define rb_stdin (*rb_vm_specific_ptr(rb_vmkey_stdin)) rb_vmkey_stdout, @@ -174,12 +204,14 @@ #define rb_stderr (*rb_vm_specific_ptr(rb_vmkey_stderr)) rb_vmkey_argf, #define rb_argf (*rb_vm_specific_ptr(rb_vmkey_argf)) - rb_vmkey_verbose, +#define ruby_verbose (*rb_vm_specific_ptr(rb_vmkey_verbose)) rb_vmkey_debug, +#define ruby_debug (*rb_vm_specific_ptr(rb_vmkey_debug)) rb_vmkey_progname, ruby_public_object_count }; +#undef RUBY_PUBLIC_OBJECT_ACCESSOR #endif /* RUBY_PUBLIC_OBJECT_H */ Index: mvm/include/ruby/ruby.h =================================================================== --- mvm/include/ruby/ruby.h (revision 19009) +++ mvm/include/ruby/ruby.h (revision 19010) @@ -908,11 +908,6 @@ VALUE rb_equal(VALUE,VALUE); -VALUE *rb_ruby_verbose_ptr(void); -VALUE *rb_ruby_debug_ptr(void); -#define ruby_verbose (*rb_ruby_verbose_ptr()) -#define ruby_debug (*rb_ruby_debug_ptr()) - PRINTF_ARGS(NORETURN(void rb_raise(VALUE, const char*, ...)), 2, 3); PRINTF_ARGS(NORETURN(void rb_fatal(const char*, ...)), 1, 2); PRINTF_ARGS(NORETURN(void rb_bug(const char*, ...)), 1, 2); Index: mvm/ChangeLog =================================================================== --- mvm/ChangeLog (revision 19009) +++ mvm/ChangeLog (revision 19010) @@ -1,3 +1,15 @@ +Mon Sep 1 11:31:26 2008 Koichi Sasada <ko1@a...> + + * mvm.c: added. + + * common.mk: ditto. + + * eval.c, eval_intern.h, include/ruby/intern.h, include/ruby/mvm.h, + include/ruby/public_object.h, include/ruby/ruby.h, io.c, main.c, + main.c, pack.c, ruby.c, thread.c, thread_pthread.c, + thread_pthread.h, vm.c, vm_core.h: fix Ruby/MVM API. + On this commit, only miniruby is built, but not working. + Sun Aug 31 12:41:09 2008 Koichi Sasada <ko1@a...> * sampledriver/main.c: update. Index: mvm/thread_pthread.c =================================================================== --- mvm/thread_pthread.c (revision 19009) +++ mvm/thread_pthread.c (revision 19010) @@ -26,8 +26,6 @@ #include <sys/resource.h> #endif -static void native_mutex_lock(pthread_mutex_t *lock); -static void native_mutex_unlock(pthread_mutex_t *lock); static int native_mutex_trylock(pthread_mutex_t *lock); static void native_cond_signal(pthread_cond_t *cond); @@ -36,8 +34,11 @@ static void native_cond_initialize(pthread_cond_t *cond); static void native_cond_destroy(pthread_cond_t *cond); -static void -native_mutex_lock(pthread_mutex_t *lock) +#define native_mutex_lock ruby_native_thread_lock +#define native_mutex_unlock ruby_native_thread_unlock + +void +ruby_native_thread_lock(pthread_mutex_t *lock) { int r; if ((r = pthread_mutex_lock(lock)) != 0) { @@ -45,8 +46,8 @@ } } -static void -native_mutex_unlock(pthread_mutex_t *lock) +void +ruby_native_thread_unlock(pthread_mutex_t *lock) { int r; if ((r = pthread_mutex_unlock(lock)) != 0) { @@ -128,12 +129,18 @@ #define native_cleanup_pop pthread_cleanup_pop #define native_thread_yield() ruby_native_thread_yield() +void +ruby_native_thread_yield(void) +{ + sched_yield(); +} + #ifndef __CYGWIN__ static void add_signal_thread_list(rb_thread_t *th); #endif static void remove_signal_thread_list(rb_thread_t *th); -static rb_thread_lock_t signal_thread_list_lock = RB_THREAD_LOCK_INITIALIZER;; +static rb_thread_lock_t signal_thread_list_lock; static pthread_key_t ruby_native_thread_key; Index: mvm/thread_pthread.h =================================================================== --- mvm/thread_pthread.h (revision 19009) +++ mvm/thread_pthread.h (revision 19010) @@ -13,6 +13,7 @@ #include <pthread.h> typedef pthread_t rb_thread_id_t; +typedef pthread_mutex_t rb_thread_lock_t; typedef pthread_cond_t rb_thread_cond_t; typedef struct native_thread_data_struct { Index: mvm/vm_core.h =================================================================== --- mvm/vm_core.h (revision 19009) +++ mvm/vm_core.h (revision 19010) @@ -554,6 +554,8 @@ int abort_on_exception; }; +typedef struct rb_thread_struct rb_thread_t; + /* iseq.c */ VALUE rb_iseq_new(NODE*, VALUE, VALUE, VALUE, VALUE); VALUE rb_iseq_new_with_bopt(NODE*, VALUE, VALUE, VALUE, VALUE, VALUE); Index: mvm/io.c =================================================================== --- mvm/io.c (revision 19009) +++ mvm/io.c (revision 19010) @@ -15,6 +15,7 @@ #include "ruby/io.h" #include "ruby/signal.h" #include "vm_core.h" +#include "eval_intern.h" #include <ctype.h> #include <errno.h> @@ -135,7 +136,8 @@ }; static int max_file_descriptor = NOFILE; -static rb_thread_lock_t max_file_descriptor_lock = RB_THREAD_LOCK_INITIALIZER; +static rb_thread_lock_t max_file_descriptor_lock; + #define UPDATE_MAXFD(fd) \ do { \ ruby_native_thread_lock(&max_file_descriptor_lock); \ @@ -144,8 +146,9 @@ } while (0) #define argf_of(obj) (*(struct argf *)DATA_PTR(obj)) -#define ruby_vm_argf(vm) (*ruby_vm_specific_ptr(vm, rb_vmkey_argf)) -#define ARGF argf_of(ruby_vm_argf(GET_VM())) +#define ruby_vm_argf(vm) (*((VALUE *)ruby_vm_specific_ptr(vm, rb_vmkey_argf))) +#define rb_vm_argf() ruby_vm_argf(GET_VM()) +#define ARGF argf_of(rb_vm_argf()) #ifdef _STDIO_USES_IOSTREAM /* GNU libc */ # ifdef _IO_fpos_t @@ -8528,4 +8531,6 @@ sym_open_args = ID2SYM(rb_intern("open_args")); sym_textmode = ID2SYM(rb_intern("textmode")); sym_binmode = ID2SYM(rb_intern("binmode")); + + ruby_native_thread_lock_initialize(&max_file_descriptor_lock); } Index: mvm/pack.c =================================================================== --- mvm/pack.c (revision 19009) +++ mvm/pack.c (revision 19010) @@ -10,6 +10,8 @@ **********************************************************************/ #include "ruby/ruby.h" +#include "eval_intern.h" + #include <sys/types.h> #include <ctype.h> #include <errno.h> @@ -1800,8 +1802,10 @@ char *ptr = RSTRING_PTR(buf); int a = -1,b = -1,c = 0,d; static signed char b64_xtable[256]; - static rb_thread_lock_t b64_xtable_lock = RB_THREAD_LOCK_INITIALIZER; + static rb_thread_lock_t b64_xtable_lock; + ruby_native_thread_lock_initialize(&b64_xtable_lock); + if (b64_xtable_once(&b64_xtable['/'], &b64_xtable_lock)) { int i; Index: mvm/thread.c =================================================================== --- mvm/thread.c (revision 19009) +++ mvm/thread.c (revision 19010) @@ -3841,54 +3841,6 @@ } } -static struct { - rb_thread_lock_t lock; - int last; -} specific_key = { - RB_THREAD_LOCK_INITIALIZER, - (ruby_builtin_object_count + 8) & ~7, -}; - -int -rb_vm_key_count(void) -{ - return specific_key.last; -} - -int -rb_vm_key_create(void) -{ - int key; - native_mutex_lock(&specific_key.lock); - key = specific_key.last++; - native_mutex_unlock(&specific_key.lock); - return key; -} - -VALUE * -ruby_vm_specific_ptr(rb_vm_t *vm, int key) -{ - VALUE *ptr; - long len; - - ptr = vm->specific_storage.ptr; - len = vm->specific_storage.len; - if (!ptr || len <= key) { - long newlen = (key + 8) & ~7; - ptr = realloc(ptr, sizeof(VALUE) * newlen); - vm->specific_storage.ptr = ptr; - vm->specific_storage.len = newlen; - MEMZERO(ptr + len, VALUE, newlen - len); - } - return &ptr[key]; -} - -VALUE * -rb_vm_specific_ptr(int key) -{ - return ruby_vm_specific_ptr(GET_VM(), key); -} - static void update_coverage(rb_event_flag_t event, VALUE proc, VALUE self, ID id, VALUE klass) { Index: mvm/common.mk =================================================================== --- mvm/common.mk (revision 19009) +++ mvm/common.mk (revision 19010) @@ -80,6 +80,7 @@ vm_dump.$(OBJEXT) \ thread.$(OBJEXT) \ cont.$(OBJEXT) \ + mvm.$(OBJEXT) \ $(BUILTIN_ENCOBJS) \ $(BUILTIN_TRANSOBJS) \ $(MISSING) @@ -598,6 +599,8 @@ {$(VPATH)}thread_$(THREAD_MODEL).h {$(VPATH)}gc.h \ {$(VPATH)}eval_intern.h {$(VPATH)}util.h {$(VPATH)}dln.h {$(VPATH)}mvm.h \ {$(VPATH)}parse.y +mvm.$(OBJEXT): {$(VPATH)}mvm.c $(RUBY_H_INCLUDES) $(VM_CORE_H_INCLUDES) \ + {$(VPATH)}st.h {$(VPATH)}thread_$(THREAD_MODEL).h time.$(OBJEXT): {$(VPATH)}time.c $(RUBY_H_INCLUDES) {$(VPATH)}st.h \ {$(VPATH)}encoding.h {$(VPATH)}oniguruma.h util.$(OBJEXT): {$(VPATH)}util.c $(RUBY_H_INCLUDES) {$(VPATH)}st.h \ Index: mvm/main.c =================================================================== --- mvm/main.c (revision 19009) +++ mvm/main.c (revision 19010) @@ -30,8 +30,12 @@ ruby_sysinit(&argc, &argv); { rb_vm_t *vm; + int ret; + RUBY_INIT_STACK; - vm = ruby_vm_new(); - return ruby_vm_run(vm, ruby_vm_parse_options(vm, argc, argv)); + vm = ruby_vm_new(argc, argv); + ret = ruby_vm_run(vm); + ruby_vm_destruct(vm); + return ret; } } Index: mvm/eval.c =================================================================== --- mvm/eval.c (revision 19009) +++ mvm/eval.c (revision 19010) @@ -86,33 +86,6 @@ extern void rb_clear_trace_func(void); -VALUE -ruby_vm_parse_options(rb_vm_t *vm, int argc, char **argv) -{ - int state; - VALUE code = 0; - - Init_stack((void *)&state); - PUSH_TAG(); - if ((state = EXEC_TAG()) == 0) { - SAVE_ROOT_JMPBUF(vm->main_thread, - code = ruby_vm_process_options(vm, argc, argv)); - } - else { - rb_vm_clear_trace_func(vm); - state = error_handle(state); - code = INT2FIX(state); - } - POP_TAG(); - return code; -} - -void * -ruby_options(int argc, char **argv) -{ - return (void *)ruby_vm_parse_options(GET_VM(), argc, argv); -} - static void ruby_finalize_0(rb_vm_t *vm) { @@ -215,56 +188,12 @@ return ruby_vm_cleanup(GET_VM(), ex); } -static int -th_exec_iseq(rb_thread_t *th, VALUE iseq, const char *file) -{ - int state; - - if (!iseq) return 0; - - PUSH_TAG(); - if ((state = EXEC_TAG()) == 0) { - SAVE_ROOT_JMPBUF(th, { - th->base_block = 0; - rb_iseq_eval(iseq); - }); - } - POP_TAG(); - return state; -} - -int -ruby_exec_node(void *n, const char *file) -{ - return th_exec_iseq(GET_THREAD(), (VALUE)n, file); -} - void ruby_stop(int ex) { exit(ruby_cleanup(ex)); } -int -ruby_vm_run(rb_vm_t *vm, VALUE v) -{ - switch (v) { - case Qtrue: return EXIT_SUCCESS; - case Qfalse: return EXIT_FAILURE; - } - if (FIXNUM_P(v)) { - return FIX2INT(v); - } - Init_stack((void *)&v); - return ruby_vm_cleanup(vm, th_exec_iseq(vm->main_thread, v, 0)); -} - -int -ruby_run_node(void *n) -{ - return ruby_vm_run(GET_VM(), (VALUE)n); -} - /* * call-seq: * Module.nesting => array Index: mvm/mvm.c =================================================================== --- mvm/mvm.c (revision 0) +++ mvm/mvm.c (revision 19010) @@ -0,0 +1,287 @@ +/********************************************************************** + + mvm.c - Ruby/MVM C API + + $Author: $ + created at: Sun Aug 31 22:27:03 2008 + + Copyright (C) 2008 Koichi Sasada + +**********************************************************************/ + +#include "ruby/mvm.h" +#include "ruby/ruby.h" +#include "vm_core.h" +#include "eval_intern.h" + +static void vmmgr_add(rb_vm_t *vm); +static void vmmgr_del(rb_vm_t *vm); +static rb_vm_t *vm_new(int argc, char *argv[]); +static void vm_cleanup(); + +/* core API */ + +rb_vm_t * +ruby_vm_new(int argc, char *argv[]) +{ + rb_vm_t *vm = vm_new(argc, argv); + vmmgr_add(vm); + + return vm; +} + +int +ruby_vm_run(rb_vm_t *vm) +{ + return 0; +} + +int +ruby_vm_start(rb_vm_t *vm) +{ + rb_bug("unsupporeted"); + return 0; +} + +int +ruby_vm_join(rb_vm_t *vm) +{ + rb_bug("unsupporeted"); + return 0; +} + +int +ruby_vm_destruct(rb_vm_t *vm) +{ + vm_cleanup(vm); + vmmgr_del(vm); + return 0; +} + + +/* initialize API */ + +int +ruby_vm_init_add_argv(rb_vm_t *vm, const char *arg) +{ + rb_bug("not supported"); + return 1; +} + +int +ruby_vm_init_add_library(rb_vm_t *vm, const char *lib) +{ + rb_bug("not supported"); + return 1; +} + +int +ruby_vm_init_add_library_path(rb_vm_t *vm, const char *path) +{ + rb_bug("not supported"); + return 1; +} + +int +ruby_vm_init_add_expression(rb_vm_t *vm, const char *expr) +{ + rb_bug("not supported"); + return 1; +} + +int +ruby_vm_init_script(rb_vm_t *vm, const char *script) +{ + rb_bug("not supported"); + return 1; +} + +int +ruby_vm_init_verbose(rb_vm_t *vm, int verbose_p) +{ + rb_bug("not supported"); + return 1; +} + +int +ruby_vm_init_debug(rb_vm_t *vm, int debug) +{ + rb_bug("not supported"); + return 1; +} + +int +ruby_vm_init_add_initializer(rb_vm_t *vm, void (*initializer)(rb_vm_t *)) +{ + rb_bug("not supported"); + return 1; +} + +int +ruby_vm_init_stdin(rb_vm_t *vm, int fd) +{ + rb_bug("not supported"); + return 1; +} + +int +ruby_vm_init_stdout(rb_vm_t *vm, int fd) +{ + rb_bug("not supported"); + return 1; +} + +int +ruby_vm_init_stderr(rb_vm_t *vm, int fd) +{ + rb_bug("not supported"); + return 1; +} + + +/* specific key management API */ + +#define MVM_CRITICAL(lock, expr) do { \ + ruby_native_thread_lock(&(lock)); \ + {expr;} \ + ruby_native_thread_unlock(&(lock)); \ +} while (0) + +static struct { + rb_thread_lock_t lock; + int last; +} specific_key; + +int +rb_vm_key_count(void) +{ + return specific_key.last; +} + +int +rb_vm_key_create(void) +{ + int key; + MVM_CRITICAL(specific_key.lock, { + key = specific_key.last++; + }); + return key; +} + +void * +ruby_vm_specific_ptr(rb_vm_t *vm, int key) +{ + VALUE *ptr; + long len; + + ptr = vm->specific_storage.ptr; + len = vm->specific_storage.len; + if (!ptr || len <= key) { + long newlen = (key + 8) & ~7; + ptr = realloc(ptr, sizeof(VALUE) * newlen); + vm->specific_storage.ptr = ptr; + vm->specific_storage.len = newlen; + MEMZERO(ptr + len, VALUE, newlen - len); + } + return &ptr[key]; +} + +VALUE * +rb_vm_specific_ptr(int key) +{ + return ruby_vm_specific_ptr(GET_VM(), key); +} + +/* VM management */ + +static struct { + rb_thread_lock_t lock; + struct vm_list_struct { + struct vm_list_struct *next; + rb_vm_t *vm; + } *list; + rb_vm_t *main; +} vm_manager; + +static void +vmmgr_initialize(rb_vm_t *vm) +{ + /* vm_manager */ + ruby_native_thread_lock_initialize(&vm_manager.lock); + vm_manager.main = vm; + + /* specific_key */ + ruby_native_thread_lock_initialize(&specific_key.lock); + specific_key.last = (ruby_builtin_object_count + 8) & ~7; +} + +static void +vmmgr_add(rb_vm_t *vm) +{ + struct vm_list_struct *entry = + (struct vm_list_struct *)malloc(sizeof(struct vm_list_struct)); + + entry->vm = vm; + + if (vm_manager.main == 0) { + vmmgr_initialize(vm); + } + + MVM_CRITICAL(vm_manager.lock, { + entry->next = vm_manager.list; + vm_manager.list = entry; + }); +} + +static void +vmmgr_del(rb_vm_t *vm) +{ + struct vm_list_struct *entry, *prev = 0; + + for (entry = vm_manager.list; + entry && entry->vm != vm; + prev = entry, entry = entry->next) { + /* nop */ + } + + if (entry) { + MVM_CRITICAL(vm_manager.lock, { + if (entry == vm_manager.list) { + vm_manager.list = entry->next; + } + else { + prev->next = entry->next; + } + }); + } + free(entry); +} + +void +ruby_vm_foreach(int (*func)(rb_vm_t *, void *), void *arg) +{ + if (vm_manager.list) { + MVM_CRITICAL(vm_manager.lock, { + struct vm_list_struct *entry = vm_manager.list; + + while (entry) { + if (func(entry->vm, arg) == 0) { + break; + } + } + }); + } +} + +static rb_vm_t * +vm_new(int argc, char *argv[]) +{ + /* place holder */ + return 0; +} + +static void +vm_cleanup() +{ + /* place holder */ +} Index: mvm/vm.c =================================================================== --- mvm/vm.c (revision 19009) +++ mvm/vm.c (revision 19010) @@ -1342,74 +1342,7 @@ /* vm */ -static struct { - rb_thread_lock_t lock; - st_table *list; - rb_vm_t *main; -} vm_manager = {RB_THREAD_LOCK_INITIALIZER}; - -static void -vm_add(rb_vm_t *vm) -{ - ruby_native_thread_lock(&vm_manager.lock); - st_insert(vm_manager.list, (st_data_t)vm, 0); - ruby_native_thread_unlock(&vm_manager.lock); -} - -static void -vm_del(rb_vm_t *vm) -{ - st_data_t key = (st_data_t)vm, val = 0; - ruby_native_thread_lock(&vm_manager.lock); - st_delete(vm_manager.list, &key, &val); - ruby_native_thread_unlock(&vm_manager.lock); -} - -struct each_vm_arg { - int (*func)(rb_vm_t *, void *); - void *arg; -}; - static int -each_vm(st_data_t vm, st_data_t val, st_data_t arg) -{ - struct each_vm_arg *p = (void *)arg; - if ((*p->func)((rb_vm_t *)vm, p->arg)) return ST_CONTINUE; - return ST_STOP; -} - -void -ruby_vm_foreach(int (*func)(rb_vm_t *, void *), void *arg) -{ - struct each_vm_arg args; - - args.func = func; - args.arg = arg; - ruby_native_thread_lock(&vm_manager.lock); - if (vm_manager.list) { - st_foreach(vm_manager.list, each_vm, (st_data_t)&args); - } - ruby_native_thread_unlock(&vm_manager.lock); -} - -static void -vm_free(void *ptr) -{ - RUBY_FREE_ENTER("vm"); - if (ptr) { - rb_vm_t *vmobj = ptr; - - st_free_table(vmobj->living_threads); - vmobj->living_threads = 0; - vm_del(vmobj); - ruby_native_thread_unlock(&vmobj->global_vm_lock); - ruby_native_thread_lock_destroy(&vmobj->global_vm_lock); - ruby_xfree(ptr); - } - RUBY_FREE_LEAVE("vm"); -} - -static int vm_mark_each_thread_func(st_data_t key, st_data_t value, st_data_t dummy) { VALUE thval = (VALUE)key; @@ -1463,6 +1396,22 @@ RUBY_MARK_LEAVE("vm"); } +static void +vm_free(void *ptr) +{ + RUBY_FREE_ENTER("vm"); + if (ptr) { + rb_vm_t *vmobj = ptr; + + st_free_table(vmobj->living_threads); + vmobj->living_threads = 0; + ruby_native_thread_unlock(&vmobj->global_vm_lock); + ruby_native_thread_lock_destroy(&vmobj->global_vm_lock); + ruby_xfree(ptr); + } + RUBY_FREE_LEAVE("vm"); +} + #if defined(ENABLE_VM_OBJSPACE) && ENABLE_VM_OBJSPACE struct rb_objspace *rb_objspace_alloc(void); #endif @@ -1564,7 +1513,7 @@ rb_queue_destroy(&th->queue.message); rb_queue_destroy(&th->queue.signal); - if (th->vm->main_thread == th && th->vm == vm_manager.main) { + if (th->vm->main_thread == th) { RUBY_GC_INFO("main thread\n"); } else { @@ -1990,21 +1939,10 @@ vm_init2(vm); th->vm = vm; - vm_manager.main = vm; - vm_manager.list = st_init_numtable(); - vm_add(vm); - th_init2(th, 0); ruby_thread_init_stack(th); } -rb_vm_t * -ruby_vm_new(void) -{ - ruby_init(); - return GET_VM(); -} - /* top self */ static VALUE @@ -2027,27 +1965,3 @@ vm->top_self = rb_obj_alloc(rb_cObject); rb_define_singleton_method(rb_vm_top_self(), "to_s", main_to_s, 0); } - -VALUE * -ruby_vm_verbose_ptr(rb_vm_t *vm) -{ - return ruby_vm_specific_ptr(vm, rb_vmkey_verbose); -} - -VALUE * -ruby_vm_debug_ptr(rb_vm_t *vm) -{ - return ruby_vm_specific_ptr(vm, rb_vmkey_debug); -} - -VALUE * -rb_ruby_verbose_ptr(void) -{ - return ruby_vm_verbose_ptr(GET_VM()); -} - -VALUE * -rb_ruby_debug_ptr(void) -{ - return ruby_vm_debug_ptr(GET_VM()); -} Index: mvm/version.h =================================================================== --- mvm/version.h (revision 19009) +++ mvm/version.h (revision 19010) @@ -1,15 +1,15 @@ #define RUBY_VERSION "1.9.0" -#define RUBY_RELEASE_DATE "2008-08-31" +#define RUBY_RELEASE_DATE "2008-09-01" #define RUBY_VERSION_CODE 190 -#define RUBY_RELEASE_CODE 20080831 +#define RUBY_RELEASE_CODE 20080901 #define RUBY_PATCHLEVEL 0 #define RUBY_VERSION_MAJOR 1 #define RUBY_VERSION_MINOR 9 #define RUBY_VERSION_TEENY 0 #define RUBY_RELEASE_YEAR 2008 -#define RUBY_RELEASE_MONTH 8 -#define RUBY_RELEASE_DAY 31 +#define RUBY_RELEASE_MONTH 9 +#define RUBY_RELEASE_DAY 1 #ifdef RUBY_EXTERN RUBY_EXTERN const char ruby_version[]; Index: mvm/ruby.c =================================================================== --- mvm/ruby.c (revision 19009) +++ mvm/ruby.c (revision 19010) @@ -599,6 +599,9 @@ rb_warn("don't know how to dump `%.*s', (insns)", len, str); } +#define RUBY_VM_OBJECT(vm, name) \ + (*((VALUE *)ruby_vm_specific_ptr(vm, rb_vmkey_##name))) + static int proc_options(rb_vm_t *vm, int argc, char **argv, struct cmdline_options *opt) { @@ -629,8 +632,8 @@ goto reswitch; case 'd': - *ruby_vm_specific_ptr(vm, rb_vmkey_debug) = Qtrue; - *ruby_vm_specific_ptr(vm, rb_vmkey_verbose) = Qtrue; + RUBY_VM_OBJECT(vm, debug) = Qtrue; + RUBY_VM_OBJECT(vm, verbose) = Qtrue; s++; goto reswitch; @@ -647,7 +650,7 @@ ruby_show_version(); opt->verbose = 1; case 'w': - *ruby_vm_specific_ptr(vm, rb_vmkey_verbose) = Qtrue; + RUBY_VM_OBJECT(vm, verbose) = Qtrue; s++; goto reswitch; @@ -664,13 +667,13 @@ } switch (v) { case 0: - *ruby_vm_specific_ptr(vm, rb_vmkey_verbose) = Qnil; + RUBY_VM_OBJECT(vm, verbose) = Qnil; break; case 1: - *ruby_vm_specific_ptr(vm, rb_vmkey_verbose) = Qfalse; + RUBY_VM_OBJECT(vm, verbose) = Qfalse; break; default: - *ruby_vm_specific_ptr(vm, rb_vmkey_verbose) = Qtrue; + RUBY_VM_OBJECT(vm, verbose) = Qtrue; break; } } @@ -850,8 +853,8 @@ if (strcmp("copyright", s) == 0) opt->copyright = 1; else if (strcmp("debug", s) == 0) { - *ruby_vm_specific_ptr(vm, rb_vmkey_debug) = Qtrue; - *ruby_vm_specific_ptr(vm, rb_vmkey_verbose) = Qtrue; + RUBY_VM_OBJECT(vm, debug) = Qtrue; + RUBY_VM_OBJECT(vm, verbose) = Qtrue; } else if (strncmp("enable", s, n = 6) == 0 && (!s[n] || s[n] == '-' || s[n] == '=')) { @@ -882,7 +885,7 @@ opt->version = 1; else if (strcmp("verbose", s) == 0) { opt->verbose = 1; - *ruby_vm_specific_ptr(vm, rb_vmkey_verbose) = Qtrue; + RUBY_VM_OBJECT(vm, verbose) = Qtrue; } else if (strcmp("yydebug", s) == 0) opt->yydebug = 1; @@ -952,7 +955,7 @@ return i; } -#define rb_progname (*ruby_vm_specific_ptr(vm, rb_vmkey_progname)) +#define rb_progname (RUBY_VM_OBJECT(vm, progname)) VALUE rb_argv0; static VALUE @@ -1541,7 +1544,7 @@ NODE *tree; if (argv[0]) { /* for the time being */ - *ruby_vm_specific_ptr(vm, rb_vmkey_progname) = rb_tainted_str_new2(argv[0]); + RUBY_VM_OBJECT(vm, progname) = rb_tainted_str_new2(argv[0]); } args.vm = vm; args.argc = argc; @@ -1550,7 +1553,7 @@ opt.ext.enc.index = -1; tree = (NODE *)rb_vm_call_cfunc(vm->top_self, process_options, (VALUE)&args, - 0, *ruby_vm_specific_ptr(vm, rb_vmkey_progname)); + 0, RUBY_VM_OBJECT(vm, progname)); rb_define_readonly_boolean("$-p", opt.do_print); rb_define_readonly_boolean("$-l", opt.do_line); @@ -1563,7 +1566,7 @@ { rb_vm_t *vm = GET_VM(); VALUE result = ruby_vm_process_options(vm, argc, argv); - rb_argv0 = rb_str_new4(*ruby_vm_specific_ptr(vm, rb_vmkey_progname)); + rb_argv0 = rb_str_new4(RUBY_VM_OBJECT(vm, progname)); return (void *)result; } -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/