(2012/09/21 15:10), ko1 (Koichi Sasada) wrote:
> では,どうやって解決するかというと難しいですね.タイマスレッドが動いている間は free しない,とか,そういうのになりそうな感じ.

勘で書いてみたんですが,こんな感じでしょうか.再現が出来なさそうなので,
テストの書きようが無さそう.


Index: vm_core.h
===================================================================
--- vm_core.h	(revision 37007)
+++ vm_core.h	(working copy)
@@ -312,6 +312,7 @@ typedef struct rb_vm_struct {
     int thread_abort_on_exception;
     unsigned long trace_flag;
     volatile int sleeper;
+    volatile struct rb_thread_struct *timer_thread_target;

     /* object management */
     VALUE mark_object_ary;
Index: thread.c
===================================================================
--- thread.c	(revision 37007)
+++ thread.c	(working copy)
@@ -3388,9 +3388,13 @@ timer_thread_function(void *arg)
     rb_vm_t *vm = GET_VM(); /* TODO: fix me for Multi-VM */

     /* for time slice */
+    vm->timer_thread_target = vm->running_thread;
+    {
     if (!vm->running_thread->yielding) {
 	RUBY_VM_SET_TIMER_INTERRUPT(vm->running_thread);
     }
+    }
+    vm->timer_thread_target = 0;

     /* check signal */
     rb_threadptr_check_signal(vm->main_thread);
Index: vm.c
===================================================================
--- vm.c	(revision 37007)
+++ vm.c	(working copy)
@@ -1724,6 +1724,9 @@ thread_free(void *ptr)
 		free(th->altstack);
 	    }
 #endif
+	    while (GET_VM()->timer_thread_target == ptr) {
+		rb_thread_schedule(); /* Timer thread seeing my thread */
+	    }
 	    ruby_xfree(ptr);
 	}
         if (ruby_current_thread == th)



-- 
// SASADA Koichi at atdot dot net