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

* vm.c: fix to recycle thread data (VM stack).

* thread.c: ditto.
* benchmark/bm_vm3_thread_create_join.rb: add loop count.



git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13994 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
ko1 2007-11-21 09:06:06 +00:00
parent 58b1f91b46
commit c79ab68d37
4 changed files with 65 additions and 6 deletions

View file

@ -1,3 +1,11 @@
Wed Nov 21 18:03:49 2007 Koichi Sasada <ko1@atdot.net>
* vm.c: fix to recycle thread data (VM stack).
* thread.c: ditto.
* benchmark/bm_vm3_thread_create_join.rb: add loop count.
Wed Nov 21 18:02:10 2007 Koichi Sasada <ko1@atdot.net>
* benchmark/driver.rb: add path to trunk/lib if driver runner is

View file

@ -1,5 +1,5 @@
i=0
while i<1000 # benchmark loop 3
while i<100_000 # benchmark loop 3
i+=1
Thread.new{
}.join

View file

@ -288,6 +288,7 @@ thread_cleanup_func(void *th_ptr)
extern void ruby_error_print(void);
static VALUE rb_thread_raise(int, VALUE *, rb_thread_t *);
void rb_thread_recycle_stack_release(VALUE *);
static int
thread_start_func_2(rb_thread_t *th, VALUE *stack_start, VALUE *register_stack_start)
@ -359,6 +360,11 @@ thread_start_func_2(rb_thread_t *th, VALUE *stack_start, VALUE *register_stack_s
join_th = join_th->join_list_next;
}
st_delete_wrap(th->vm->living_threads, th->self);
if (!th->root_fiber) {
rb_thread_recycle_stack_release(th->stack);
th->stack = 0;
}
}
thread_cleanup_func(th);
native_mutex_unlock(&th->vm->global_interpreter_lock);
@ -428,7 +434,6 @@ thread_join(rb_thread_t *target_th, double delay)
th->join_list_next = target_th->join_list_head;
target_th->join_list_head = th;
}
while (target_th->status != THREAD_KILLED) {
if (delay == DELAY_INFTY) {
sleep_forever(th);

54
vm.c
View file

@ -1521,6 +1521,51 @@ vm_init2(rb_vm_t *vm)
/* Thread */
#define USE_THREAD_DATA_RECYCLE 1
#if USE_THREAD_DATA_RECYCLE
#define RECYCLE_MAX 64
VALUE *thread_recycle_stack_slot[RECYCLE_MAX];
int thread_recycle_stack_count = 0;
static VALUE *
thread_recycle_stack(int size)
{
if (thread_recycle_stack_count) {
return thread_recycle_stack_slot[--thread_recycle_stack_count];
}
else {
return ALLOC_N(VALUE, size);
}
}
#else
#define thread_recycle_stack(size) ALLOC_N(VALUE, (size))
#endif
void
rb_thread_recycle_stack_release(VALUE *stack)
{
#if USE_THREAD_DATA_RECYCLE
if (thread_recycle_stack_count < RECYCLE_MAX) {
thread_recycle_stack_slot[thread_recycle_stack_count++] = stack;
}
else {
ruby_xfree(stack);
}
#else
ruby_xfree(stack);
#endif
}
static rb_thread_t *
thread_recycle_struct(void)
{
void *p = ALLOC_N(rb_thread_t, 1);
memset(p, 0, sizeof(rb_thread_t));
return p;
}
static void
thread_free(void *ptr)
{
@ -1529,7 +1574,7 @@ thread_free(void *ptr)
if (ptr) {
th = ptr;
if (!th->root_fiber) {
RUBY_FREE_UNLESS_NULL(th->stack);
}
@ -1619,6 +1664,8 @@ static VALUE
thread_alloc(VALUE klass)
{
VALUE volatile obj;
//rb_thread_t *th = thread_recycle_struct();
//obj = Data_Wrap_Struct(klass, rb_thread_mark, thread_free, th);
rb_thread_t *th;
obj = Data_Make_Struct(klass, rb_thread_t,
rb_thread_mark, thread_free, th);
@ -1628,11 +1675,9 @@ thread_alloc(VALUE klass)
static void
th_init2(rb_thread_t *th)
{
MEMZERO(th, rb_thread_t, 1);
/* allocate thread stack */
th->stack_size = RUBY_VM_THREAD_STACK_SIZE;
th->stack = ALLOC_N(VALUE, th->stack_size);
th->stack = thread_recycle_stack(th->stack_size);
th->cfp = (void *)(th->stack + th->stack_size);
th->cfp--;
@ -1815,6 +1860,7 @@ Init_BareVM(void)
/* VM bootstrap: phase 1 */
rb_vm_t *vm = ALLOC(rb_vm_t);
rb_thread_t *th = ALLOC(rb_thread_t);
MEMZERO(th, rb_thread_t, 1);
vm_init2(vm);
ruby_current_vm = vm;