2006-12-14 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / libgc / darwin_stop_world.c
index 53604450328647f25bcdc9b4679d891dd2a38baf..04bb415e02624dafba3620141fa0810a05a7dbbf 100644 (file)
@@ -75,12 +75,14 @@ void GC_push_all_stacks() {
   ptr_t lo, hi;
 #if defined(POWERPC)
   ppc_thread_state_t state;
+  mach_msg_type_number_t thread_state_count = PPC_THREAD_STATE_COUNT;
 #elif defined(I386)
   i386_thread_state_t state;
+  mach_msg_type_number_t thread_state_count = i386_THREAD_STATE_COUNT;
 #else
 # error FIXME for non-x86 || ppc architectures
-#endif
   mach_msg_type_number_t thread_state_count = MACHINE_THREAD_STATE_COUNT;
+#endif
   
   me = pthread_self();
   if (!GC_thr_initialized) GC_thr_init();
@@ -94,7 +96,7 @@ void GC_push_all_stacks() {
        /* Get the thread state (registers, etc) */
        r = thread_get_state(
                             p->stop_info.mach_thread,
-                            MACHINE_THREAD_STATE,
+                            GC_MACH_THREAD_STATE_FLAVOR,
                             (natural_t*)&state,
                             &thread_state_count);
        if(r != KERN_SUCCESS) ABORT("thread_get_state failed");
@@ -167,6 +169,7 @@ void GC_push_all_stacks() {
 
 void GC_push_all_stacks() {
     int i;
+       task_t my_task;
     kern_return_t r;
     mach_port_t me;
     ptr_t lo, hi;
@@ -176,7 +179,8 @@ void GC_push_all_stacks() {
     me = mach_thread_self();
     if (!GC_thr_initialized) GC_thr_init();
     
-    r = task_threads(current_task(), &act_list, &listcount);
+       my_task = current_task();
+    r = task_threads(my_task, &act_list, &listcount);
     if(r != KERN_SUCCESS) ABORT("task_threads failed");
     for(i = 0; i < listcount; i++) {
       thread_act_t thread = act_list[i];
@@ -191,7 +195,7 @@ void GC_push_all_stacks() {
        ppc_thread_state64_t info;
 #      endif
        mach_msg_type_number_t outCount = THREAD_STATE_MAX;
-       r = thread_get_state(thread, MACHINE_THREAD_STATE,
+       r = thread_get_state(thread, GC_MACH_THREAD_STATE_FLAVOR,
                             (natural_t *)&info, &outCount);
        if(r != KERN_SUCCESS) continue;
 
@@ -234,7 +238,7 @@ void GC_push_all_stacks() {
        WARN("This is completely untested and likely will not work\n", 0);
        i386_thread_state_t info;
        mach_msg_type_number_t outCount = THREAD_STATE_MAX;
-       r = thread_get_state(thread, MACHINE_THREAD_STATE,
+       r = thread_get_state(thread, GC_MACH_THREAD_STATE_FLAVOR,
                             (natural_t *)&info, &outCount);
        if(r != KERN_SUCCESS) continue;
 
@@ -265,9 +269,11 @@ void GC_push_all_stacks() {
                  (unsigned long) hi
                 );
 #     endif
-      GC_push_all_stack(lo, hi); 
+      GC_push_all_stack(lo, hi);
+         mach_port_deallocate(my_task, thread);
     } /* for(p=GC_threads[i]...) */
-    vm_deallocate(current_task(), (vm_address_t)act_list, sizeof(thread_t) * listcount);
+    vm_deallocate(my_task, (vm_address_t)act_list, sizeof(thread_t) * listcount);
+       mach_port_deallocate(my_task, me);
 }
 #endif /* !DARWIN_DONT_PARSE_STACK */
 
@@ -361,6 +367,8 @@ int GC_suspend_thread_list(thread_act_array_t act_list, int count,
     } 
     if (!found) GC_mach_threads_count++;
   }
+  
+  mach_port_deallocate(current_task(), my_thread);
   return changed;
 }
 
@@ -370,6 +378,7 @@ void GC_stop_world()
 {
   int i, changes;
     GC_thread p;
+       task_t my_task = current_task();
     mach_port_t my_thread = mach_thread_self();
     kern_return_t kern_result;
     thread_act_array_t act_list, prev_list;
@@ -404,16 +413,31 @@ void GC_stop_world()
       prev_list = NULL;
       prevcount = 0;
       do {
-       int result;
-       kern_result = task_threads(current_task(), &act_list, &listcount);
-       result = GC_suspend_thread_list(act_list, listcount,
-                                       prev_list, prevcount);
-       changes = result;
-       prev_list = act_list;
-       prevcount = listcount;
-        vm_deallocate(current_task(), (vm_address_t)act_list, sizeof(thread_t) * listcount);
+       int result;               
+       kern_result = task_threads(my_task, &act_list, &listcount);
+       
+       if(kern_result == KERN_SUCCESS) {       
+               result = GC_suspend_thread_list(act_list, listcount,
+                                                                               prev_list, prevcount);
+               changes = result;
+               
+               if(prev_list != NULL) {
+                       for(i = 0; i < prevcount; i++)
+                               mach_port_deallocate(my_task, prev_list[i]);
+                       
+                       vm_deallocate(my_task, (vm_address_t)prev_list, sizeof(thread_t) * prevcount);
+               }
+               
+               prev_list = act_list;
+               prevcount = listcount;
+       }               
       } while (changes);
-      
+     
+         for(i = 0; i < listcount; i++)
+                 mach_port_deallocate(my_task, act_list[i]);
+         
+         vm_deallocate(my_task, (vm_address_t)act_list, sizeof(thread_t) * listcount);
+         
  
 #   ifdef MPROTECT_VDB
       if(GC_incremental) {
@@ -428,12 +452,15 @@ void GC_stop_world()
     #if DEBUG_THREADS
       GC_printf1("World stopped from 0x%lx\n", my_thread);
     #endif
+         
+         mach_port_deallocate(my_task, my_thread);
 }
 
 /* Caller holds allocation lock, and has held it continuously since    */
 /* the world stopped.                                                  */
 void GC_start_world()
 {
+  task_t my_task = current_task();
   mach_port_t my_thread = mach_thread_self();
   int i, j;
   GC_thread p;
@@ -454,7 +481,7 @@ void GC_start_world()
       }
 #   endif
 
-    kern_result = task_threads(current_task(), &act_list, &listcount);
+    kern_result = task_threads(my_task, &act_list, &listcount);
     for(i = 0; i < listcount; i++) {
       thread_act_t thread = act_list[i];
       if (thread != my_thread &&
@@ -482,8 +509,12 @@ void GC_start_world()
          } 
        }
       }
+         
+         mach_port_deallocate(my_task, thread);
     }
-    vm_deallocate(current_task(), (vm_address_t)act_list, sizeof(thread_t) * listcount);
+    vm_deallocate(my_task, (vm_address_t)act_list, sizeof(thread_t) * listcount);
+       
+       mach_port_deallocate(my_task, my_thread);
 #   if DEBUG_THREADS
      GC_printf0("World started\n");
 #   endif