Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / libgc / darwin_stop_world.c
1 #include "private/pthread_support.h"
2
3 # if defined(GC_DARWIN_THREADS)
4
5 #include <AvailabilityMacros.h>
6 #include "mono/utils/mono-compiler.h"
7
8 /* From "Inside Mac OS X - Mach-O Runtime Architecture" published by Apple
9    Page 49:
10    "The space beneath the stack pointer, where a new stack frame would normally
11    be allocated, is called the red zone. This area as shown in Figure 3-2 may
12    be used for any purpose as long as a new stack frame does not need to be
13    added to the stack."
14    
15    Page 50: "If a leaf procedure's red zone usage would exceed 224 bytes, then
16    it must set up a stack frame just like routines that call other routines."
17 */
18 #ifdef POWERPC
19 # if CPP_WORDSZ == 32
20 #   define PPC_RED_ZONE_SIZE 224
21 # elif CPP_WORDSZ == 64
22 #   define PPC_RED_ZONE_SIZE 320
23 # endif
24 #endif
25
26 typedef struct StackFrame {
27   unsigned long savedSP;
28   unsigned long savedCR;
29   unsigned long savedLR;
30   unsigned long reserved[2];
31   unsigned long savedRTOC;
32 } StackFrame;
33
34 unsigned long FindTopOfStack(unsigned int stack_start) {
35   StackFrame    *frame;
36   
37   if (stack_start == 0) {
38 # ifdef POWERPC
39 #   if CPP_WORDSZ == 32
40       __asm__ volatile("lwz     %0,0(r1)" : "=r" (frame));
41 #   else
42       __asm__ volatile("ldz     %0,0(r1)" : "=r" (frame));
43 #   endif
44 # endif
45   } else {
46     frame = (StackFrame *)stack_start;
47   }
48
49 # ifdef DEBUG_THREADS
50     /* GC_printf1("FindTopOfStack start at sp = %p\n", frame); */
51 # endif
52   do {
53     if (frame->savedSP == 0) break;
54                 /* if there are no more stack frames, stop */
55
56     frame = (StackFrame*)frame->savedSP;
57
58     /* we do these next two checks after going to the next frame
59        because the LR for the first stack frame in the loop
60        is not set up on purpose, so we shouldn't check it. */
61     if ((frame->savedLR & ~3) == 0) break; /* if the next LR is bogus, stop */
62     if ((~(frame->savedLR) & ~3) == 0) break; /* ditto */
63   } while (1); 
64
65 # ifdef DEBUG_THREADS
66     /* GC_printf1("FindTopOfStack finish at sp = %p\n", frame); */
67 # endif
68
69   return (unsigned long)frame;
70 }       
71
72 #ifdef DARWIN_DONT_PARSE_STACK
73 void GC_push_all_stacks() {
74   int i;
75   kern_return_t r;
76   GC_thread p;
77   pthread_t me;
78   ptr_t lo, hi, altstack_lo, altstack_hi;
79 #if defined(POWERPC)
80   ppc_thread_state_t state;
81   mach_msg_type_number_t thread_state_count = PPC_THREAD_STATE_COUNT;
82 #elif defined(I386)
83   i386_thread_state_t state;
84   mach_msg_type_number_t thread_state_count = i386_THREAD_STATE_COUNT;
85 #elif defined(ARM)
86   arm_thread_state_t state;
87   mach_msg_type_number_t thread_state_count = ARM_THREAD_STATE_COUNT;
88 #elif defined(X86_64)
89   x86_thread_state64_t state;
90   mach_msg_type_number_t thread_state_count = x86_THREAD_STATE64_COUNT;
91 #else
92 # error FIXME for non-x86 || ppc architectures
93   mach_msg_type_number_t thread_state_count = MACHINE_THREAD_STATE_COUNT;
94 #endif
95   
96   me = pthread_self();
97   if (!GC_thr_initialized) GC_thr_init();
98   
99   for(i=0;i<THREAD_TABLE_SZ;i++) {
100     for(p=GC_threads[i];p!=0;p=p->next) {
101       if(p -> flags & FINISHED) continue;
102       if(pthread_equal(p->id,me)) {
103         lo = GC_approx_sp();
104       } else {
105         /* Get the thread state (registers, etc) */
106         r = thread_get_state(
107                              p->stop_info.mach_thread,
108                              GC_MACH_THREAD_STATE_FLAVOR,
109                              (natural_t*)&state,
110                              &thread_state_count);
111         if(r != KERN_SUCCESS) continue;
112         
113 #if defined(I386)
114 #if defined (TARGET_IOS) || (MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5)
115
116         lo = state.__esp;
117
118         GC_push_one(state.__eax); 
119         GC_push_one(state.__ebx); 
120         GC_push_one(state.__ecx); 
121         GC_push_one(state.__edx); 
122         GC_push_one(state.__edi); 
123         GC_push_one(state.__esi); 
124         GC_push_one(state.__ebp); 
125 #else
126         lo = state.esp;
127
128         GC_push_one(state.eax); 
129         GC_push_one(state.ebx); 
130         GC_push_one(state.ecx); 
131         GC_push_one(state.edx); 
132         GC_push_one(state.edi); 
133         GC_push_one(state.esi); 
134         GC_push_one(state.ebp); 
135 #endif
136 #elif defined(X86_64)
137           lo = state.__rsp;
138           GC_push_one(state.__rax);
139           GC_push_one(state.__rbx);
140           GC_push_one(state.__rcx);
141           GC_push_one(state.__rdx);
142           GC_push_one(state.__rdi);
143           GC_push_one(state.__rsi);
144           GC_push_one(state.__rbp);
145           GC_push_one(state.__r8);
146           GC_push_one(state.__r9);
147           GC_push_one(state.__r10);
148           GC_push_one(state.__r11);
149           GC_push_one(state.__r12);
150           GC_push_one(state.__r13);
151           GC_push_one(state.__r14);
152           GC_push_one(state.__r15);
153 #elif defined(POWERPC)
154 #if defined(_STRUCT_PPC_EXCEPTION_STATE) && defined(__DARWIN_UNIX03)
155         lo = (void*)(state.__r1 - PPC_RED_ZONE_SIZE);
156         
157         GC_push_one(state.__r0); 
158         GC_push_one(state.__r2); 
159         GC_push_one(state.__r3); 
160         GC_push_one(state.__r4); 
161         GC_push_one(state.__r5); 
162         GC_push_one(state.__r6); 
163         GC_push_one(state.__r7); 
164         GC_push_one(state.__r8); 
165         GC_push_one(state.__r9); 
166         GC_push_one(state.__r10); 
167         GC_push_one(state.__r11); 
168         GC_push_one(state.__r12); 
169         GC_push_one(state.__r13); 
170         GC_push_one(state.__r14); 
171         GC_push_one(state.__r15); 
172         GC_push_one(state.__r16); 
173         GC_push_one(state.__r17); 
174         GC_push_one(state.__r18); 
175         GC_push_one(state.__r19); 
176         GC_push_one(state.__r20); 
177         GC_push_one(state.__r21); 
178         GC_push_one(state.__r22); 
179         GC_push_one(state.__r23); 
180         GC_push_one(state.__r24); 
181         GC_push_one(state.__r25); 
182         GC_push_one(state.__r26); 
183         GC_push_one(state.__r27); 
184         GC_push_one(state.__r28); 
185         GC_push_one(state.__r29); 
186         GC_push_one(state.__r30); 
187         GC_push_one(state.__r31);
188 #else
189         lo = (void*)(state.r1 - PPC_RED_ZONE_SIZE);
190         
191         GC_push_one(state.r0); 
192         GC_push_one(state.r2); 
193         GC_push_one(state.r3); 
194         GC_push_one(state.r4); 
195         GC_push_one(state.r5); 
196         GC_push_one(state.r6); 
197         GC_push_one(state.r7); 
198         GC_push_one(state.r8); 
199         GC_push_one(state.r9); 
200         GC_push_one(state.r10); 
201         GC_push_one(state.r11); 
202         GC_push_one(state.r12); 
203         GC_push_one(state.r13); 
204         GC_push_one(state.r14); 
205         GC_push_one(state.r15); 
206         GC_push_one(state.r16); 
207         GC_push_one(state.r17); 
208         GC_push_one(state.r18); 
209         GC_push_one(state.r19); 
210         GC_push_one(state.r20); 
211         GC_push_one(state.r21); 
212         GC_push_one(state.r22); 
213         GC_push_one(state.r23); 
214         GC_push_one(state.r24); 
215         GC_push_one(state.r25); 
216         GC_push_one(state.r26); 
217         GC_push_one(state.r27); 
218         GC_push_one(state.r28); 
219         GC_push_one(state.r29); 
220         GC_push_one(state.r30); 
221         GC_push_one(state.r31);
222 #endif
223 #elif defined(ARM)
224         lo = (void*)state.__sp;
225
226         GC_push_one(state.__r[0]);
227         GC_push_one(state.__r[1]);
228         GC_push_one(state.__r[2]);
229         GC_push_one(state.__r[3]);
230         GC_push_one(state.__r[4]);
231         GC_push_one(state.__r[5]);
232         GC_push_one(state.__r[6]);
233         GC_push_one(state.__r[7]);
234         GC_push_one(state.__r[8]);
235         GC_push_one(state.__r[9]);
236         GC_push_one(state.__r[10]);
237         GC_push_one(state.__r[11]);
238         GC_push_one(state.__r[12]);
239         /* GC_push_one(state.__sp);  */
240         GC_push_one(state.__lr);
241         GC_push_one(state.__pc);
242         GC_push_one(state.__cpsr);
243 #else
244 # error FIXME for non-x86 || ppc architectures
245 #endif
246       } /* p != me */
247       if(p->flags & MAIN_THREAD)
248         hi = GC_stackbottom;
249       else
250         hi = p->stack_end;
251
252           if (p->altstack && lo >= p->altstack && lo <= p->altstack + p->altstack_size) {
253                   altstack_lo = lo;
254                   altstack_hi = p->altstack + p->altstack_size;
255                   lo = (char*)p->stack;
256                   hi = (char*)p->stack + p->stack_size;
257           }     else {
258                   altstack_lo = NULL;
259           }
260
261 #if DEBUG_THREADS
262       GC_printf3("Darwin: Stack for thread 0x%lx = [%lx,%lx)\n",
263                  (unsigned long) p -> id,
264                  (unsigned long) lo,
265                  (unsigned long) hi
266                  );
267 #endif
268           if (lo)
269                   GC_push_all_stack(lo,hi);
270           if (altstack_lo)
271                   GC_push_all_stack(altstack_lo,altstack_hi);
272     } /* for(p=GC_threads[i]...) */
273   } /* for(i=0;i<THREAD_TABLE_SZ...) */
274 }
275
276 #else /* !DARWIN_DONT_PARSE_STACK; Use FindTopOfStack() */
277
278 void GC_push_all_stacks() {
279     int i;
280         task_t my_task;
281     kern_return_t r;
282     mach_port_t me;
283     ptr_t lo, hi;
284     thread_act_array_t act_list = 0;
285     mach_msg_type_number_t listcount = 0;
286
287     me = mach_thread_self();
288     if (!GC_thr_initialized) GC_thr_init();
289     
290         my_task = current_task();
291     r = task_threads(my_task, &act_list, &listcount);
292     if(r != KERN_SUCCESS) ABORT("task_threads failed");
293     for(i = 0; i < listcount; i++) {
294       thread_act_t thread = act_list[i];
295       if (thread == me) {
296         lo = GC_approx_sp();
297         hi = (ptr_t)FindTopOfStack(0);
298       } else {
299 #     if defined(POWERPC)
300 #      if CPP_WORDSZ == 32
301         ppc_thread_state_t info;
302 #      else
303         ppc_thread_state64_t info;
304 #      endif
305         mach_msg_type_number_t outCount = THREAD_STATE_MAX;
306         r = thread_get_state(thread, GC_MACH_THREAD_STATE_FLAVOR,
307                              (natural_t *)&info, &outCount);
308         if(r != KERN_SUCCESS) continue;
309
310 #if defined(_STRUCT_PPC_EXCEPTION_STATE)
311         lo = (void*)(info.__r1 - PPC_RED_ZONE_SIZE);
312         hi = (ptr_t)FindTopOfStack(info.__r1);
313
314         GC_push_one(info.__r0); 
315         GC_push_one(info.__r2); 
316         GC_push_one(info.__r3); 
317         GC_push_one(info.__r4); 
318         GC_push_one(info.__r5); 
319         GC_push_one(info.__r6); 
320         GC_push_one(info.__r7); 
321         GC_push_one(info.__r8); 
322         GC_push_one(info.__r9); 
323         GC_push_one(info.__r10); 
324         GC_push_one(info.__r11); 
325         GC_push_one(info.__r12); 
326         GC_push_one(info.__r13); 
327         GC_push_one(info.__r14); 
328         GC_push_one(info.__r15); 
329         GC_push_one(info.__r16); 
330         GC_push_one(info.__r17); 
331         GC_push_one(info.__r18); 
332         GC_push_one(info.__r19); 
333         GC_push_one(info.__r20); 
334         GC_push_one(info.__r21); 
335         GC_push_one(info.__r22); 
336         GC_push_one(info.__r23); 
337         GC_push_one(info.__r24); 
338         GC_push_one(info.__r25); 
339         GC_push_one(info.__r26); 
340         GC_push_one(info.__r27); 
341         GC_push_one(info.__r28); 
342         GC_push_one(info.__r29); 
343         GC_push_one(info.__r30); 
344         GC_push_one(info.__r31);
345 #else
346         lo = (void*)(info.r1 - PPC_RED_ZONE_SIZE);
347         hi = (ptr_t)FindTopOfStack(info.r1);
348
349         GC_push_one(info.r0); 
350         GC_push_one(info.r2); 
351         GC_push_one(info.r3); 
352         GC_push_one(info.r4); 
353         GC_push_one(info.r5); 
354         GC_push_one(info.r6); 
355         GC_push_one(info.r7); 
356         GC_push_one(info.r8); 
357         GC_push_one(info.r9); 
358         GC_push_one(info.r10); 
359         GC_push_one(info.r11); 
360         GC_push_one(info.r12); 
361         GC_push_one(info.r13); 
362         GC_push_one(info.r14); 
363         GC_push_one(info.r15); 
364         GC_push_one(info.r16); 
365         GC_push_one(info.r17); 
366         GC_push_one(info.r18); 
367         GC_push_one(info.r19); 
368         GC_push_one(info.r20); 
369         GC_push_one(info.r21); 
370         GC_push_one(info.r22); 
371         GC_push_one(info.r23); 
372         GC_push_one(info.r24); 
373         GC_push_one(info.r25); 
374         GC_push_one(info.r26); 
375         GC_push_one(info.r27); 
376         GC_push_one(info.r28); 
377         GC_push_one(info.r29); 
378         GC_push_one(info.r30); 
379         GC_push_one(info.r31);
380 #endif
381 #      elif defined(I386) /* !POWERPC */
382         /* FIXME: Remove after testing: */
383         WARN("This is completely untested and likely will not work\n", 0);
384         i386_thread_state_t info;
385         mach_msg_type_number_t outCount = THREAD_STATE_MAX;
386         r = thread_get_state(thread, GC_MACH_THREAD_STATE_FLAVOR,
387                              (natural_t *)&info, &outCount);
388         if(r != KERN_SUCCESS) continue;
389
390 #if defined (TARGET_IOS) || (MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5)
391         lo = (void*)info.__esp;
392         hi = (ptr_t)FindTopOfStack(info.__esp);
393
394         GC_push_one(info.__eax); 
395         GC_push_one(info.__ebx); 
396         GC_push_one(info.__ecx); 
397         GC_push_one(info.__edx); 
398         GC_push_one(info.__edi); 
399         GC_push_one(info.__esi); 
400         GC_push_one(info.__ebp);
401         /* GC_push_one(info.__esp);  */
402         GC_push_one(info.__ss); 
403         GC_push_one(info.__eip); 
404         GC_push_one(info.__cs); 
405         GC_push_one(info.__ds); 
406         GC_push_one(info.__es); 
407         GC_push_one(info.__fs); 
408         GC_push_one(info.__gs); 
409 #else
410         lo = (void*)info.esp;
411         hi = (ptr_t)FindTopOfStack(info.esp);
412
413         GC_push_one(info.eax); 
414         GC_push_one(info.ebx); 
415         GC_push_one(info.ecx); 
416         GC_push_one(info.edx); 
417         GC_push_one(info.edi); 
418         GC_push_one(info.esi); 
419         GC_push_one(info.ebp);
420         /* GC_push_one(info.esp);  */
421         GC_push_one(info.ss); 
422         GC_push_one(info.eip); 
423         GC_push_one(info.cs); 
424         GC_push_one(info.ds); 
425         GC_push_one(info.es); 
426         GC_push_one(info.fs); 
427         GC_push_one(info.gs); 
428 #endif
429 #      elif defined(ARM) /* !I386 */
430         arm_thread_state_t info;
431         mach_msg_type_number_t outCount = THREAD_STATE_MAX;
432         r = thread_get_state(thread, GC_MACH_THREAD_STATE_FLAVOR,
433                              (natural_t *)&info, &outCount);
434         if(r != KERN_SUCCESS) continue;
435
436         lo = (void*)info.__sp;
437         hi = (ptr_t)FindTopOfStack(info.__sp);
438
439         GC_push_one(info.__r[0]); 
440         GC_push_one(info.__r[1]); 
441         GC_push_one(info.__r[2]); 
442         GC_push_one(info.__r[3]); 
443         GC_push_one(info.__r[4]); 
444         GC_push_one(info.__r[5]); 
445         GC_push_one(info.__r[6]); 
446         GC_push_one(info.__r[7]); 
447         GC_push_one(info.__r[8]); 
448         GC_push_one(info.__r[9]); 
449         GC_push_one(info.__r[10]); 
450         GC_push_one(info.__r[11]); 
451         GC_push_one(info.__r[12]); 
452         /* GC_push_one(info.__sp);  */
453         GC_push_one(info.__lr); 
454         GC_push_one(info.__pc); 
455         GC_push_one(info.__cpsr); 
456 #      endif /* !ARM */
457       }
458 #     if DEBUG_THREADS
459        GC_printf3("Darwin: Stack for thread 0x%lx = [%lx,%lx)\n",
460                   (unsigned long) thread,
461                   (unsigned long) lo,
462                   (unsigned long) hi
463                  );
464 #     endif
465       GC_push_all_stack(lo, hi);
466           mach_port_deallocate(my_task, thread);
467     } /* for(p=GC_threads[i]...) */
468     vm_deallocate(my_task, (vm_address_t)act_list, sizeof(thread_t) * listcount);
469         mach_port_deallocate(my_task, me);
470 }
471 #endif /* !DARWIN_DONT_PARSE_STACK */
472
473 static mach_port_t GC_mach_handler_thread;
474 static int GC_use_mach_handler_thread = 0;
475
476 #define SUSPEND_THREADS_SIZE 2048
477 static struct GC_mach_thread GC_mach_threads[SUSPEND_THREADS_SIZE];
478 static int GC_mach_threads_count;
479
480 void GC_stop_init() {
481   int i;
482
483   for (i = 0; i < SUSPEND_THREADS_SIZE; i++) {
484     GC_mach_threads[i].thread = 0;
485     GC_mach_threads[i].already_suspended = 0;
486   }
487   GC_mach_threads_count = 0;
488 }
489
490 /* returns true if there's a thread in act_list that wasn't in old_list */
491 int GC_suspend_thread_list(thread_act_array_t act_list, int count, 
492                            thread_act_array_t old_list, int old_count) {
493   mach_port_t my_thread = mach_thread_self();
494   int i, j;
495
496   int changed = 0;
497
498   for(i = 0; i < count; i++) {
499     thread_act_t thread = act_list[i];
500 #   if DEBUG_THREADS 
501       GC_printf1("Attempting to suspend thread %p\n", thread);
502 #   endif
503     /* find the current thread in the old list */
504     int found = 0;
505     for(j = 0; j < old_count; j++) {
506       thread_act_t old_thread = old_list[j];
507       if (old_thread == thread) {
508         found = 1;
509         break;
510       }
511     }
512     if (!found) {
513       /* add it to the GC_mach_threads list */
514       GC_mach_threads[GC_mach_threads_count].thread = thread;
515       /* default is not suspended */
516       GC_mach_threads[GC_mach_threads_count].already_suspended = 0;
517       changed = 1;
518     }      
519
520     if (thread != my_thread &&
521         (!GC_use_mach_handler_thread
522          || (GC_use_mach_handler_thread
523              && GC_mach_handler_thread != thread))) {
524       struct thread_basic_info info;
525       mach_msg_type_number_t outCount = THREAD_INFO_MAX;
526       kern_return_t kern_result = thread_info(thread, THREAD_BASIC_INFO,
527                                 (thread_info_t)&info, &outCount);
528       if(kern_result != KERN_SUCCESS) {
529         /* the thread may have quit since the thread_threads () call 
530          * we mark already_suspended so it's not dealt with anymore later
531          */
532         if (!found) {
533           GC_mach_threads[GC_mach_threads_count].already_suspended = TRUE;
534           GC_mach_threads_count++;
535         }
536         continue;
537       }
538 #     if DEBUG_THREADS
539         GC_printf2("Thread state for 0x%lx = %d\n", thread, info.run_state);
540 #     endif
541       if (!found) {
542         GC_mach_threads[GC_mach_threads_count].already_suspended = info.suspend_count;
543       }
544       if (info.suspend_count) continue;
545       
546 #     if DEBUG_THREADS
547         GC_printf1("Suspending 0x%lx\n", thread);
548 #     endif
549       /* Suspend the thread */
550       kern_result = thread_suspend(thread);
551       if(kern_result != KERN_SUCCESS) {
552         /* the thread may have quit since the thread_threads () call 
553          * we mark already_suspended so it's not dealt with anymore later
554          */
555         if (!found) {
556           GC_mach_threads[GC_mach_threads_count].already_suspended = TRUE;
557           GC_mach_threads_count++;
558         }
559         continue;
560       }
561     } 
562     if (!found) GC_mach_threads_count++;
563   }
564   
565   mach_port_deallocate(current_task(), my_thread);
566   return changed;
567 }
568
569
570 /* Caller holds allocation lock.        */
571 void GC_stop_world()
572 {
573   int i, changes;
574     GC_thread p;
575         task_t my_task = current_task();
576     mach_port_t my_thread = mach_thread_self();
577     kern_return_t kern_result;
578     thread_act_array_t act_list, prev_list;
579     mach_msg_type_number_t listcount, prevcount;
580     
581     if (GC_notify_event)
582         GC_notify_event (GC_EVENT_PRE_STOP_WORLD);
583         GC_process_togglerefs ();
584
585 #   if DEBUG_THREADS
586       GC_printf1("Stopping the world from 0x%lx\n", mach_thread_self());
587 #   endif
588
589     /* clear out the mach threads list table */
590     GC_stop_init(); 
591        
592     /* Make sure all free list construction has stopped before we start. */
593     /* No new construction can start, since free list construction is   */
594     /* required to acquire and release the GC lock before it starts,    */
595     /* and we have the lock.                                            */
596 #   ifdef PARALLEL_MARK
597       GC_acquire_mark_lock();
598       GC_ASSERT(GC_fl_builder_count == 0);
599       /* We should have previously waited for it to become zero. */
600 #   endif /* PARALLEL_MARK */
601
602       /* Loop stopping threads until you have gone over the whole list
603          twice without a new one appearing. thread_create() won't
604          return (and thus the thread stop) until the new thread
605          exists, so there is no window whereby you could stop a
606          thread, recognise it is stopped, but then have a new thread
607          it created before stopping show up later.
608       */
609       
610       changes = 1;
611       prev_list = NULL;
612       prevcount = 0;
613       do {
614         int result;               
615         kern_result = task_threads(my_task, &act_list, &listcount);
616         
617         if(kern_result == KERN_SUCCESS) {       
618                 result = GC_suspend_thread_list(act_list, listcount,
619                                                                                 prev_list, prevcount);
620                 changes = result;
621                 
622                 if(prev_list != NULL) {
623                         for(i = 0; i < prevcount; i++)
624                                 mach_port_deallocate(my_task, prev_list[i]);
625                         
626                         vm_deallocate(my_task, (vm_address_t)prev_list, sizeof(thread_t) * prevcount);
627                 }
628                 
629                 prev_list = act_list;
630                 prevcount = listcount;
631         }               
632       } while (changes);
633      
634           for(i = 0; i < listcount; i++)
635                   mach_port_deallocate(my_task, act_list[i]);
636           
637           vm_deallocate(my_task, (vm_address_t)act_list, sizeof(thread_t) * listcount);
638           
639  
640 #   ifdef MPROTECT_VDB
641       if(GC_incremental) {
642         extern void GC_mprotect_stop();
643         GC_mprotect_stop();
644       }
645 #   endif
646     
647 #   ifdef PARALLEL_MARK
648       GC_release_mark_lock();
649 #   endif
650     #if DEBUG_THREADS
651       GC_printf1("World stopped from 0x%lx\n", my_thread);
652     #endif
653           
654           mach_port_deallocate(my_task, my_thread);
655
656     if (GC_notify_event)
657         GC_notify_event (GC_EVENT_POST_STOP_WORLD);
658 }
659
660 /* Caller holds allocation lock, and has held it continuously since     */
661 /* the world stopped.                                                   */
662 void GC_start_world()
663 {
664   task_t my_task = current_task();
665   mach_port_t my_thread = mach_thread_self();
666   int i, j;
667   GC_thread p;
668   kern_return_t kern_result;
669   thread_act_array_t act_list;
670   mach_msg_type_number_t listcount;
671   struct thread_basic_info info;
672   mach_msg_type_number_t outCount = THREAD_INFO_MAX;
673
674   if (GC_notify_event)
675       GC_notify_event (GC_EVENT_PRE_START_WORLD);
676   
677 #   if DEBUG_THREADS
678       GC_printf0("World starting\n");
679 #   endif
680
681 #   ifdef MPROTECT_VDB
682       if(GC_incremental) {
683         extern void GC_mprotect_resume();
684         GC_mprotect_resume();
685       }
686 #   endif
687
688     kern_result = task_threads(my_task, &act_list, &listcount);
689     for(i = 0; i < listcount; i++) {
690       thread_act_t thread = act_list[i];
691       if (thread != my_thread &&
692           (!GC_use_mach_handler_thread ||
693            (GC_use_mach_handler_thread && GC_mach_handler_thread != thread))) {
694         for(j = 0; j < GC_mach_threads_count; j++) {
695           if (thread == GC_mach_threads[j].thread) {
696             if (GC_mach_threads[j].already_suspended) {
697 #             if DEBUG_THREADS
698                 GC_printf1("Not resuming already suspended thread %p\n", thread);
699 #             endif
700               continue;
701             }
702             kern_result = thread_info(thread, THREAD_BASIC_INFO,
703                                       (thread_info_t)&info, &outCount);
704             if(kern_result != KERN_SUCCESS) continue;
705 #           if DEBUG_THREADS
706               GC_printf2("Thread state for 0x%lx = %d\n", thread,
707                          info.run_state);
708               GC_printf1("Resuming 0x%lx\n", thread);
709 #           endif
710             /* Resume the thread */
711             kern_result = thread_resume(thread);
712             if(kern_result != KERN_SUCCESS) continue;
713           } 
714         }
715       }
716           
717           mach_port_deallocate(my_task, thread);
718     }
719     vm_deallocate(my_task, (vm_address_t)act_list, sizeof(thread_t) * listcount);
720         
721         mach_port_deallocate(my_task, my_thread);
722
723     if (GC_notify_event)
724         GC_notify_event (GC_EVENT_POST_START_WORLD);
725
726 #   if DEBUG_THREADS
727      GC_printf0("World started\n");
728 #   endif
729 }
730
731 void GC_darwin_register_mach_handler_thread(mach_port_t thread) {
732   GC_mach_handler_thread = thread;
733   GC_use_mach_handler_thread = 1;
734 }
735
736 #endif