[前][次][番号順一覧][スレッド一覧]

ruby-changes:48337

From: ko1 <ko1@a...>
Date: Thu, 26 Oct 2017 23:21:37 +0900 (JST)
Subject: [ruby-changes:48337] ko1:r60451 (trunk): fix freeing `th->ec` bugs.

ko1	2017-10-26 23:21:31 +0900 (Thu, 26 Oct 2017)

  New Revision: 60451

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=60451

  Log:
    fix freeing `th->ec` bugs.
    
    * vm.c (thread_free): simply call rb_threadptr_root_fiber_release().
    
    * cont.c (rb_threadptr_root_fiber_release): release th->ec (ec->fiber)
      iff root_fiber is NULL. If root_fiber is available, then ignore it
      and root fiber object will free th->ec too.
    
    * cont.c (rb_threadptr_root_fiber_setup): do not set th->root_fiber.
      th->root_fiber will be set if a root fiber object is created.

  Modified files:
    trunk/cont.c
    trunk/vm.c
Index: cont.c
===================================================================
--- cont.c	(revision 60450)
+++ cont.c	(revision 60451)
@@ -440,6 +440,7 @@ fiber_free(void *ptr) https://github.com/ruby/ruby/blob/trunk/cont.c#L440
 {
     rb_fiber_t *fib = ptr;
     RUBY_FREE_ENTER("fiber");
+
     if (fib->cont.saved_ec.local_storage) {
 	st_free_table(fib->cont.saved_ec.local_storage);
     }
@@ -1476,7 +1477,6 @@ rb_threadptr_root_fiber_setup(rb_thread_ https://github.com/ruby/ruby/blob/trunk/cont.c#L1477
     fib->cont.thread_ptr = th;
     fiber_status_set(fib, FIBER_RESUMED); /* skip CREATED */
     th->ec = &fib->cont.saved_ec;
-    th->root_fiber = th->ec->fiber = fib;
 #if FIBER_USE_NATIVE
 #ifdef _WIN32
     if (fib->fib_handle == 0) {
@@ -1486,6 +1486,24 @@ rb_threadptr_root_fiber_setup(rb_thread_ https://github.com/ruby/ruby/blob/trunk/cont.c#L1486
 #endif
 }
 
+void
+rb_threadptr_root_fiber_release(rb_thread_t *th)
+{
+    if (th->root_fiber) {
+	/* ignore. A root fiber object will free th->ec */
+    }
+    else {
+	VM_ASSERT(th->ec->fiber->cont.type == ROOT_FIBER_CONTEXT);
+	VM_ASSERT(th->ec->fiber->cont.self == 0);
+	fiber_free(th->ec->fiber);
+
+	if (th->ec == ruby_current_execution_context_ptr) {
+	    ruby_current_execution_context_ptr = NULL;
+	}
+	th->ec = NULL;
+    }
+}
+
 static inline rb_fiber_t*
 fiber_current(void)
 {
Index: vm.c
===================================================================
--- vm.c	(revision 60450)
+++ vm.c	(revision 60451)
@@ -2415,6 +2415,8 @@ rb_execution_context_mark(const rb_execu https://github.com/ruby/ruby/blob/trunk/vm.c#L2415
 }
 
 void rb_fiber_mark_self(rb_fiber_t *fib);
+void rb_threadptr_root_fiber_setup(rb_thread_t *th);
+void rb_threadptr_root_fiber_release(rb_thread_t *th);
 
 void
 rb_thread_mark(void *ptr)
@@ -2433,7 +2435,7 @@ rb_thread_mark(void *ptr) https://github.com/ruby/ruby/blob/trunk/vm.c#L2435
     RUBY_MARK_UNLESS_NULL(th->pending_interrupt_mask_stack);
     RUBY_MARK_UNLESS_NULL(th->top_self);
     RUBY_MARK_UNLESS_NULL(th->top_wrapper);
-    rb_fiber_mark_self(th->root_fiber);
+    if (th->root_fiber) rb_fiber_mark_self(th->root_fiber);
     RUBY_MARK_UNLESS_NULL(th->stat_insn_usage);
     RUBY_MARK_UNLESS_NULL(th->last_status);
     RUBY_MARK_UNLESS_NULL(th->locking_mutex);
@@ -2456,12 +2458,7 @@ thread_free(void *ptr) https://github.com/ruby/ruby/blob/trunk/vm.c#L2458
 	rb_bug("thread_free: keeping_mutexes must be NULL (%p:%p)", (void *)th, (void *)th->keeping_mutexes);
     }
 
-    if (th->ec->local_storage) {
-	st_free_table(th->ec->local_storage);
-    }
-
-    if (th->ec == ruby_current_execution_context_ptr)
-        ruby_current_execution_context_ptr = NULL;
+    rb_threadptr_root_fiber_release(th);
 
     if (th->vm && th->vm->main_thread == th) {
 	RUBY_GC_INFO("main thread\n");
@@ -2525,8 +2522,6 @@ thread_alloc(VALUE klass) https://github.com/ruby/ruby/blob/trunk/vm.c#L2522
     return obj;
 }
 
-void rb_threadptr_root_fiber_setup(rb_thread_t *th);
-
 static void
 th_init(rb_thread_t *th, VALUE self)
 {

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]