ruby-changes:13945
From: nobu <ko1@a...>
Date: Fri, 13 Nov 2009 16:28:40 +0900 (JST)
Subject: [ruby-changes:13945] Ruby:r25749 (mvm): * thread.c (ruby_native_thread_create): start new thread with
nobu 2009-11-13 16:28:24 +0900 (Fri, 13 Nov 2009) New Revision: 25749 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=25749 Log: * thread.c (ruby_native_thread_create): start new thread with rb_thread_t. split from thread_create_core. * vm.c (rb_vm_start, rb_vm_join): new methods, #start and #join. * vm.c (vm_make_main_thread): make bare main thread. split from ruby_make_bare_vm. Modified files: branches/mvm/ChangeLog branches/mvm/eval_intern.h branches/mvm/thread.c branches/mvm/vm.c Index: mvm/eval_intern.h =================================================================== --- mvm/eval_intern.h (revision 25748) +++ mvm/eval_intern.h (revision 25749) @@ -216,16 +216,17 @@ VALUE ruby_vm_argf(rb_vm_t *vm); +int ruby_native_thread_create(rb_thread_t *); 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); -void ruby_native_cond_signal(pthread_cond_t *cond); -void ruby_native_cond_broadcast(pthread_cond_t *cond); -void ruby_native_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex); -void ruby_native_cond_initialize(pthread_cond_t *cond); -void ruby_native_cond_destroy(pthread_cond_t *cond); +void ruby_native_cond_signal(rb_thread_cond_t *cond); +void ruby_native_cond_broadcast(rb_thread_cond_t *cond); +void ruby_native_cond_wait(rb_thread_cond_t *cond, rb_thread_lock_t *mutex); +void ruby_native_cond_initialize(rb_thread_cond_t *cond); +void ruby_native_cond_destroy(rb_thread_cond_t *cond); VALUE ruby_vm_get_argv(rb_vm_t *vm); const char *ruby_vm_get_inplace_mode(rb_vm_t *vm); Index: mvm/ChangeLog =================================================================== --- mvm/ChangeLog (revision 25748) +++ mvm/ChangeLog (revision 25749) @@ -1,3 +1,13 @@ +Fri Nov 13 16:28:22 2009 Nobuyoshi Nakada <nobu@r...> + + * thread.c (ruby_native_thread_create): start new thread with + rb_thread_t. split from thread_create_core. + + * vm.c (rb_vm_start, rb_vm_join): new methods, #start and #join. + + * vm.c (vm_make_main_thread): make bare main thread. split from + ruby_make_bare_vm. + Fri Nov 13 16:22:00 2009 Nobuyoshi Nakada <nobu@r...> * thread.c (thread_start_func_2): see first_func, not first_proc, Index: mvm/thread.c =================================================================== --- mvm/thread.c (revision 25748) +++ mvm/thread.c (revision 25749) @@ -417,6 +417,21 @@ native_thread_init_stack(th); } +int +ruby_native_thread_create(rb_thread_t *th) +{ + native_mutex_initialize(&th->interrupt_lock); + th->priority = GET_THREAD()->priority; + th->thgroup = GET_THREAD()->thgroup; + +#ifdef HAVE_FCHDIR + th->cwd.fd = ruby_dirfd("."); +#else + th->cwd.path = rb_str_new_shared(GET_THREAD()->cwd.path); +#endif + return native_thread_create(th); +} + static int thread_start_func_2(rb_thread_t *th, VALUE *stack_start, VALUE *register_stack_start) { @@ -548,18 +563,9 @@ th->first_proc = fn ? Qfalse : rb_block_proc(); th->first_args = args; /* GC: shouldn't put before above line */ - th->priority = GET_THREAD()->priority; - th->thgroup = GET_THREAD()->thgroup; - - native_mutex_initialize(&th->interrupt_lock); /* kick thread */ st_insert(th->vm->living_threads, thval, (st_data_t) th->thread_id); -#ifdef HAVE_FCHDIR - th->cwd.fd = ruby_dirfd("."); -#else - th->cwd.path = rb_str_new_shared(GET_THREAD()->cwd.path); -#endif - err = native_thread_create(th); + err = ruby_native_thread_create(th); if (err) { st_delete_wrap(th->vm->living_threads, th->self); th->status = THREAD_KILLED; Index: mvm/vm.c =================================================================== --- mvm/vm.c (revision 25748) +++ mvm/vm.c (revision 25749) @@ -1739,6 +1739,11 @@ rb_thread_t *th; obj = TypedData_Make_Struct(klass, rb_thread_t, &thread_data_type, th); #endif + +#ifdef HAVE_FCHDIR + th->cwd.fd = -1; +#endif + return obj; } @@ -1966,15 +1971,15 @@ --argc; vm_parse_opt(vm, opt); } - if (argc > 0) { + if ((vm->argc = argc) > 0) { int i; char **args, *argp; VALUE argsval = 0; - size_t len = rb_long2int(argc * sizeof(char *)), total = 0; + size_t len = rb_long2int(argc * sizeof(char *)); for (i = 0; i < argc; ++i) { StringValue(argv[i]); argv[i] = rb_str_new_frozen(argv[i]); - rb_long2int(total += RSTRING_LEN(argv[i]) + 1); + rb_long2int(len += RSTRING_LEN(argv[i]) + 1); } args = rb_objspace_xmalloc(vm->objspace, len); argsval = rb_str_wrap((char *)args, len); @@ -1991,6 +1996,45 @@ return self; } +static rb_thread_t *vm_make_main_thread(rb_vm_t *vm); + +static VALUE +vm_create(void *arg) +{ + rb_vm_t *vm = arg; + + ruby_native_thread_lock(&vm->global_vm_lock); + return (VALUE)ruby_vm_run(vm, 0); +} + +static VALUE +rb_vm_start(VALUE self) +{ + rb_vm_t *vm; + rb_thread_t *th; + + GetVMPtr(self, vm); + if (vm->main_thread) rb_raise(rb_eArgError, "alread started"); + th = vm_make_main_thread(vm); + th->first_func = vm_create; + th->first_proc = Qfalse; + th->first_args = (VALUE)vm; + ruby_native_thread_create(th); + ruby_native_thread_unlock(&vm->global_vm_lock); + return self; +} + +static VALUE +rb_vm_join(VALUE self) +{ + rb_vm_t *vm; + int status; + + GetVMPtr(self, vm); + status = ruby_vm_join(vm); + return INT2NUM(status); +} + void Init_VM(void) { @@ -2007,6 +2051,8 @@ rb_cRubyVM = rb_define_class("RubyVM", rb_cObject); rb_define_alloc_func(rb_cRubyVM, rb_vm_s_alloc); rb_define_method(rb_cRubyVM, "initialize", rb_vm_initialize, -1); + rb_define_method(rb_cRubyVM, "start", rb_vm_start, 0); + rb_define_method(rb_cRubyVM, "join", rb_vm_join, 0); /* ::VM::FrozenCore */ fcore = rb_class_new(rb_cBasicObject); @@ -2102,12 +2148,16 @@ rb_gc_register_mark_object(iseqval); #ifdef HAVE_FCHDIR + if (th->cwd.fd == -1) { # ifdef AT_FDCWD - th->cwd.fd = AT_FDCWD; + th->cwd.fd = AT_FDCWD; # endif - th->cwd.fd = ruby_dirfd("."); + th->cwd.fd = ruby_dirfd("."); + } #else - th->cwd.path = ruby_getcwd(); + if (!th->cwd.path) { + th->cwd.path = rb_str_new_cstr(ruby_getcwd()); + } #endif GetISeqPtr(iseqval, iseq); th->cfp->iseq = iseq; @@ -2143,16 +2193,25 @@ vm_init2(vm); + th = vm_make_main_thread(vm); + rb_thread_set_current_raw(th); + ruby_thread_init_stack(th); + + return vm; +} + +static rb_thread_t * +vm_make_main_thread(rb_vm_t *vm) +{ + rb_thread_t *th; + th = rb_objspace_xmalloc(vm->objspace, sizeof(*th)); MEMZERO(th, rb_thread_t, 1); th->vm = vm; - rb_thread_set_current_raw(th); vm->main_thread = th; - th_init(th, 0); - ruby_thread_init_stack(th); - return vm; + return th; } rb_vm_t * -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/