implemented Setup.hs to build boehm cpp libs and install them;
[hs-boehmgc.git] / gc-7.2 / darwin_stop_world.c
1 /*
2  * Copyright (c) 1994 by Xerox Corporation.  All rights reserved.
3  * Copyright (c) 1996 by Silicon Graphics.  All rights reserved.
4  * Copyright (c) 1998 by Fergus Henderson.  All rights reserved.
5  * Copyright (c) 2000-2010 by Hewlett-Packard Development Company.
6  * All rights reserved.
7  *
8  * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
9  * OR IMPLIED.  ANY USE IS AT YOUR OWN RISK.
10  *
11  * Permission is hereby granted to use or copy this program
12  * for any purpose,  provided the above notices are retained on all copies.
13  * Permission to modify the code and to distribute modified code is granted,
14  * provided the above notices are retained, and a notice that the code was
15  * modified is included with the above copyright notice.
16  */
17
18 #include "private/pthread_support.h"
19
20 /* This probably needs more porting work to ppc64. */
21
22 #if defined(GC_DARWIN_THREADS)
23
24 /* From "Inside Mac OS X - Mach-O Runtime Architecture" published by Apple
25    Page 49:
26    "The space beneath the stack pointer, where a new stack frame would normally
27    be allocated, is called the red zone. This area as shown in Figure 3-2 may
28    be used for any purpose as long as a new stack frame does not need to be
29    added to the stack."
30
31    Page 50: "If a leaf procedure's red zone usage would exceed 224 bytes, then
32    it must set up a stack frame just like routines that call other routines."
33 */
34 #ifdef POWERPC
35 # if CPP_WORDSZ == 32
36 #   define PPC_RED_ZONE_SIZE 224
37 # elif CPP_WORDSZ == 64
38 #   define PPC_RED_ZONE_SIZE 320
39 # endif
40 #endif
41
42 #ifndef DARWIN_DONT_PARSE_STACK
43
44 typedef struct StackFrame {
45   unsigned long savedSP;
46   unsigned long savedCR;
47   unsigned long savedLR;
48   unsigned long reserved[2];
49   unsigned long savedRTOC;
50 } StackFrame;
51
52 GC_INNER ptr_t GC_FindTopOfStack(unsigned long stack_start)
53 {
54   StackFrame *frame;
55
56   if (stack_start == 0) {
57 # ifdef POWERPC
58 #   if CPP_WORDSZ == 32
59       __asm__ __volatile__ ("lwz %0,0(r1)" : "=r" (frame));
60 #   else
61       __asm__ __volatile__ ("ld %0,0(r1)" : "=r" (frame));
62 #   endif
63 # endif
64   } else {
65     frame = (StackFrame *)stack_start;
66   }
67
68 # ifdef DEBUG_THREADS
69     /* GC_printf("FindTopOfStack start at sp = %p\n", frame); */
70 # endif
71   while (frame->savedSP != 0) {
72     /* if there are no more stack frames, stop */
73
74     frame = (StackFrame*)frame->savedSP;
75
76     /* we do these next two checks after going to the next frame
77        because the LR for the first stack frame in the loop
78        is not set up on purpose, so we shouldn't check it. */
79     if ((frame->savedLR & ~0x3) == 0 || (frame->savedLR & ~0x3) == ~0x3)
80       break; /* if the next LR is bogus, stop */
81   }
82 # ifdef DEBUG_THREADS
83     /* GC_printf("FindTopOfStack finish at sp = %p\n", frame); */
84 # endif
85   return (ptr_t)frame;
86 }
87
88 #endif /* !DARWIN_DONT_PARSE_STACK */
89
90 /* GC_query_task_threads controls whether to obtain the list of */
91 /* the threads from the kernel or to use GC_threads table.      */
92 #ifdef GC_NO_THREADS_DISCOVERY
93 # define GC_query_task_threads FALSE
94 #elif defined(GC_DISCOVER_TASK_THREADS)
95 # define GC_query_task_threads TRUE
96 #else
97   STATIC GC_bool GC_query_task_threads = FALSE;
98 #endif /* !GC_NO_THREADS_DISCOVERY */
99
100 /* Use implicit threads registration (all task threads excluding the GC */
101 /* special ones are stoped and scanned).  Should be called before       */
102 /* GC_INIT() (or, at least, before going multi-threaded).  Deprecated.  */
103 GC_API void GC_CALL GC_use_threads_discovery(void)
104 {
105 # if defined(GC_NO_THREADS_DISCOVERY) || defined(DARWIN_DONT_PARSE_STACK)
106     ABORT("Darwin task-threads-based stop and push unsupported");
107 # else
108     GC_ASSERT(!GC_need_to_lock);
109 #   ifndef GC_DISCOVER_TASK_THREADS
110       GC_query_task_threads = TRUE;
111 #   endif
112     GC_init_parallel(); /* just to be consistent with Win32 one */
113 # endif
114 }
115
116 /* Evaluates the stack range for a given thread.  Returns the lower     */
117 /* bound and sets *phi to the upper one.                                */
118 STATIC ptr_t GC_stack_range_for(ptr_t *phi, thread_act_t thread, GC_thread p,
119                                 GC_bool thread_blocked, mach_port_t my_thread)
120 {
121   ptr_t lo;
122   if (thread == my_thread) {
123     GC_ASSERT(!thread_blocked);
124     lo = GC_approx_sp();
125 #   ifndef DARWIN_DONT_PARSE_STACK
126       *phi = GC_FindTopOfStack(0);
127 #   endif
128
129   } else if (thread_blocked) {
130     lo = p->stop_info.stack_ptr;
131 #   ifndef DARWIN_DONT_PARSE_STACK
132       *phi = p->topOfStack;
133 #   endif
134
135   } else {
136     /* MACHINE_THREAD_STATE_COUNT does not seem to be defined       */
137     /* everywhere.  Hence we use our own version.  Alternatively,   */
138     /* we could use THREAD_STATE_MAX (but seems to be not optimal). */
139     kern_return_t kern_result;
140     mach_msg_type_number_t thread_state_count = GC_MACH_THREAD_STATE_COUNT;
141     GC_THREAD_STATE_T state;
142
143     /* Get the thread state (registers, etc) */
144     kern_result = thread_get_state(thread, GC_MACH_THREAD_STATE,
145                                    (natural_t *)&state,
146                                    &thread_state_count);
147 #   ifdef DEBUG_THREADS
148       GC_log_printf("thread_get_state returns value = %d\n", kern_result);
149 #   endif
150     if (kern_result != KERN_SUCCESS)
151       ABORT("thread_get_state failed");
152
153 #   if defined(I386)
154       lo = (void *)state.THREAD_FLD(esp);
155 #     ifndef DARWIN_DONT_PARSE_STACK
156         *phi = GC_FindTopOfStack(state.THREAD_FLD(esp));
157 #     endif
158       GC_push_one(state.THREAD_FLD(eax));
159       GC_push_one(state.THREAD_FLD(ebx));
160       GC_push_one(state.THREAD_FLD(ecx));
161       GC_push_one(state.THREAD_FLD(edx));
162       GC_push_one(state.THREAD_FLD(edi));
163       GC_push_one(state.THREAD_FLD(esi));
164       GC_push_one(state.THREAD_FLD(ebp));
165
166 #   elif defined(X86_64)
167       lo = (void *)state.THREAD_FLD(rsp);
168 #     ifndef DARWIN_DONT_PARSE_STACK
169         *phi = GC_FindTopOfStack(state.THREAD_FLD(rsp));
170 #     endif
171       GC_push_one(state.THREAD_FLD(rax));
172       GC_push_one(state.THREAD_FLD(rbx));
173       GC_push_one(state.THREAD_FLD(rcx));
174       GC_push_one(state.THREAD_FLD(rdx));
175       GC_push_one(state.THREAD_FLD(rdi));
176       GC_push_one(state.THREAD_FLD(rsi));
177       GC_push_one(state.THREAD_FLD(rbp));
178       /* GC_push_one(state.THREAD_FLD(rsp)); */
179       GC_push_one(state.THREAD_FLD(r8));
180       GC_push_one(state.THREAD_FLD(r9));
181       GC_push_one(state.THREAD_FLD(r10));
182       GC_push_one(state.THREAD_FLD(r11));
183       GC_push_one(state.THREAD_FLD(r12));
184       GC_push_one(state.THREAD_FLD(r13));
185       GC_push_one(state.THREAD_FLD(r14));
186       GC_push_one(state.THREAD_FLD(r15));
187
188 #   elif defined(POWERPC)
189       lo = (void *)(state.THREAD_FLD(r1) - PPC_RED_ZONE_SIZE);
190 #     ifndef DARWIN_DONT_PARSE_STACK
191         *phi = GC_FindTopOfStack(state.THREAD_FLD(r1));
192 #     endif
193       GC_push_one(state.THREAD_FLD(r0));
194       GC_push_one(state.THREAD_FLD(r2));
195       GC_push_one(state.THREAD_FLD(r3));
196       GC_push_one(state.THREAD_FLD(r4));
197       GC_push_one(state.THREAD_FLD(r5));
198       GC_push_one(state.THREAD_FLD(r6));
199       GC_push_one(state.THREAD_FLD(r7));
200       GC_push_one(state.THREAD_FLD(r8));
201       GC_push_one(state.THREAD_FLD(r9));
202       GC_push_one(state.THREAD_FLD(r10));
203       GC_push_one(state.THREAD_FLD(r11));
204       GC_push_one(state.THREAD_FLD(r12));
205       GC_push_one(state.THREAD_FLD(r13));
206       GC_push_one(state.THREAD_FLD(r14));
207       GC_push_one(state.THREAD_FLD(r15));
208       GC_push_one(state.THREAD_FLD(r16));
209       GC_push_one(state.THREAD_FLD(r17));
210       GC_push_one(state.THREAD_FLD(r18));
211       GC_push_one(state.THREAD_FLD(r19));
212       GC_push_one(state.THREAD_FLD(r20));
213       GC_push_one(state.THREAD_FLD(r21));
214       GC_push_one(state.THREAD_FLD(r22));
215       GC_push_one(state.THREAD_FLD(r23));
216       GC_push_one(state.THREAD_FLD(r24));
217       GC_push_one(state.THREAD_FLD(r25));
218       GC_push_one(state.THREAD_FLD(r26));
219       GC_push_one(state.THREAD_FLD(r27));
220       GC_push_one(state.THREAD_FLD(r28));
221       GC_push_one(state.THREAD_FLD(r29));
222       GC_push_one(state.THREAD_FLD(r30));
223       GC_push_one(state.THREAD_FLD(r31));
224
225 #   elif defined(ARM32)
226       lo = (void *)state.__sp;
227 #     ifndef DARWIN_DONT_PARSE_STACK
228         *phi = GC_FindTopOfStack(state.__sp);
229 #     endif
230       GC_push_one(state.__r[0]);
231       GC_push_one(state.__r[1]);
232       GC_push_one(state.__r[2]);
233       GC_push_one(state.__r[3]);
234       GC_push_one(state.__r[4]);
235       GC_push_one(state.__r[5]);
236       GC_push_one(state.__r[6]);
237       GC_push_one(state.__r[7]);
238       GC_push_one(state.__r[8]);
239       GC_push_one(state.__r[9]);
240       GC_push_one(state.__r[10]);
241       GC_push_one(state.__r[11]);
242       GC_push_one(state.__r[12]);
243       /* GC_push_one(state.__sp); */
244       GC_push_one(state.__lr);
245       /* GC_push_one(state.__pc); */
246       GC_push_one(state.__cpsr);
247
248 #   else
249 #     error FIXME for non-x86 || ppc || arm architectures
250 #   endif
251   } /* thread != my_thread */
252
253 # ifdef DARWIN_DONT_PARSE_STACK
254     /* p is guaranteed to be non-NULL regardless of GC_query_task_threads. */
255     *phi = (p->flags & MAIN_THREAD) != 0 ? GC_stackbottom : p->stack_end;
256 # endif
257 # ifdef DEBUG_THREADS
258     GC_log_printf("Darwin: Stack for thread 0x%lx = [%p,%p)\n",
259                   (unsigned long)thread, lo, *phi);
260 # endif
261   return lo;
262 }
263
264 GC_INNER void GC_push_all_stacks(void)
265 {
266   int i;
267   ptr_t lo, hi;
268   task_t my_task = current_task();
269   mach_port_t my_thread = mach_thread_self();
270   GC_bool found_me = FALSE;
271   int nthreads = 0;
272   word total_size = 0;
273   mach_msg_type_number_t listcount = (mach_msg_type_number_t)THREAD_TABLE_SZ;
274   if (!GC_thr_initialized)
275     GC_thr_init();
276
277 # ifndef DARWIN_DONT_PARSE_STACK
278     if (GC_query_task_threads) {
279       kern_return_t kern_result;
280       thread_act_array_t act_list = 0;
281
282       /* Obtain the list of the threads from the kernel.  */
283       kern_result = task_threads(my_task, &act_list, &listcount);
284       if (kern_result != KERN_SUCCESS)
285         ABORT("task_threads failed");
286
287       for (i = 0; i < (int)listcount; i++) {
288         thread_act_t thread = act_list[i];
289         lo = GC_stack_range_for(&hi, thread, NULL, FALSE, my_thread);
290         GC_ASSERT(lo <= hi);
291         total_size += hi - lo;
292         GC_push_all_stack(lo, hi);
293         nthreads++;
294         if (thread == my_thread)
295           found_me = TRUE;
296         mach_port_deallocate(my_task, thread);
297       } /* for (i=0; ...) */
298
299       vm_deallocate(my_task, (vm_address_t)act_list,
300                     sizeof(thread_t) * listcount);
301     } else
302 # endif /* !DARWIN_DONT_PARSE_STACK */
303   /* else */ {
304     for (i = 0; i < (int)listcount; i++) {
305       GC_thread p;
306       for (p = GC_threads[i]; p != NULL; p = p->next)
307         if ((p->flags & FINISHED) == 0) {
308           thread_act_t thread = (thread_act_t)p->stop_info.mach_thread;
309           lo = GC_stack_range_for(&hi, thread, p, (GC_bool)p->thread_blocked,
310                                   my_thread);
311           GC_ASSERT(lo <= hi);
312           total_size += hi - lo;
313           GC_push_all_stack_sections(lo, hi, p->traced_stack_sect);
314           nthreads++;
315           if (thread == my_thread)
316             found_me = TRUE;
317         }
318     } /* for (i=0; ...) */
319   }
320
321   mach_port_deallocate(my_task, my_thread);
322   if (GC_print_stats == VERBOSE)
323     GC_log_printf("Pushed %d thread stacks\n", nthreads);
324   if (!found_me && !GC_in_thread_creation)
325     ABORT("Collecting from unknown thread");
326   GC_total_stacksize = total_size;
327 }
328
329 #ifndef GC_NO_THREADS_DISCOVERY
330
331 # ifdef MPROTECT_VDB
332     STATIC mach_port_t GC_mach_handler_thread = 0;
333     STATIC GC_bool GC_use_mach_handler_thread = FALSE;
334
335     GC_INNER void GC_darwin_register_mach_handler_thread(mach_port_t thread)
336     {
337       GC_mach_handler_thread = thread;
338       GC_use_mach_handler_thread = TRUE;
339     }
340 # endif /* MPROTECT_VDB */
341
342 # ifndef GC_MAX_MACH_THREADS
343 #   define GC_MAX_MACH_THREADS THREAD_TABLE_SZ
344 # endif
345
346   struct GC_mach_thread {
347     thread_act_t thread;
348     GC_bool already_suspended;
349   };
350
351   struct GC_mach_thread GC_mach_threads[GC_MAX_MACH_THREADS];
352   STATIC int GC_mach_threads_count = 0;
353   /* FIXME: it is better to implement GC_mach_threads as a hash set.  */
354
355 /* returns true if there's a thread in act_list that wasn't in old_list */
356 STATIC GC_bool GC_suspend_thread_list(thread_act_array_t act_list, int count,
357                                       thread_act_array_t old_list,
358                                       int old_count, mach_port_t my_thread)
359 {
360   int i;
361   int j = -1;
362   GC_bool changed = FALSE;
363
364   for (i = 0; i < count; i++) {
365     thread_act_t thread = act_list[i];
366     GC_bool found;
367     struct thread_basic_info info;
368     mach_msg_type_number_t outCount;
369     kern_return_t kern_result;
370
371     if (thread == my_thread
372 #       ifdef MPROTECT_VDB
373           || (GC_mach_handler_thread == thread && GC_use_mach_handler_thread)
374 #       endif
375         ) {
376       /* Don't add our and the handler threads. */
377       continue;
378     }
379 #   ifdef PARALLEL_MARK
380       if (GC_is_mach_marker(thread))
381         continue; /* ignore the parallel marker threads */
382 #   endif
383
384 #   ifdef DEBUG_THREADS
385       GC_log_printf("Attempting to suspend thread 0x%lx\n",
386                     (unsigned long)thread);
387 #   endif
388     /* find the current thread in the old list */
389     found = FALSE;
390     {
391       int last_found = j; /* remember the previous found thread index */
392
393       /* Search for the thread starting from the last found one first.  */
394       while (++j < old_count)
395         if (old_list[j] == thread) {
396           found = TRUE;
397           break;
398         }
399       if (!found) {
400         /* If not found, search in the rest (beginning) of the list.    */
401         for (j = 0; j < last_found; j++)
402           if (old_list[j] == thread) {
403             found = TRUE;
404             break;
405           }
406
407         if (!found) {
408           /* add it to the GC_mach_threads list */
409           if (GC_mach_threads_count == GC_MAX_MACH_THREADS)
410             ABORT("Too many threads");
411           GC_mach_threads[GC_mach_threads_count].thread = thread;
412           /* default is not suspended */
413           GC_mach_threads[GC_mach_threads_count].already_suspended = FALSE;
414           changed = TRUE;
415         }
416       }
417     }
418
419     outCount = THREAD_INFO_MAX;
420     kern_result = thread_info(thread, THREAD_BASIC_INFO,
421                               (thread_info_t)&info, &outCount);
422     if (kern_result != KERN_SUCCESS) {
423       /* The thread may have quit since the thread_threads() call we  */
424       /* mark already suspended so it's not dealt with anymore later. */
425       if (!found)
426         GC_mach_threads[GC_mach_threads_count++].already_suspended = TRUE;
427       continue;
428     }
429 #   ifdef DEBUG_THREADS
430       GC_log_printf("Thread state for 0x%lx = %d\n", (unsigned long)thread,
431                     info.run_state);
432 #   endif
433     if (info.suspend_count != 0) {
434       /* thread is already suspended. */
435       if (!found)
436         GC_mach_threads[GC_mach_threads_count++].already_suspended = TRUE;
437       continue;
438     }
439
440 #   ifdef DEBUG_THREADS
441       GC_log_printf("Suspending 0x%lx\n", (unsigned long)thread);
442 #   endif
443     kern_result = thread_suspend(thread);
444     if (kern_result != KERN_SUCCESS) {
445       /* The thread may have quit since the thread_threads() call we  */
446       /* mark already suspended so it's not dealt with anymore later. */
447       if (!found)
448         GC_mach_threads[GC_mach_threads_count++].already_suspended = TRUE;
449       continue;
450     }
451     if (!found)
452       GC_mach_threads_count++;
453   }
454   return changed;
455 }
456
457 #endif /* !GC_NO_THREADS_DISCOVERY */
458
459 /* Caller holds allocation lock.        */
460 GC_INNER void GC_stop_world(void)
461 {
462   unsigned i;
463   task_t my_task = current_task();
464   mach_port_t my_thread = mach_thread_self();
465   kern_return_t kern_result;
466
467 # ifdef DEBUG_THREADS
468     GC_log_printf("Stopping the world from thread 0x%lx\n",
469                   (unsigned long)my_thread);
470 # endif
471 # ifdef PARALLEL_MARK
472     if (GC_parallel) {
473       /* Make sure all free list construction has stopped before we     */
474       /* start.  No new construction can start, since free list         */
475       /* construction is required to acquire and release the GC lock    */
476       /* before it starts, and we have the lock.                        */
477       GC_acquire_mark_lock();
478       GC_ASSERT(GC_fl_builder_count == 0);
479       /* We should have previously waited for it to become zero. */
480     }
481 # endif /* PARALLEL_MARK */
482
483   if (GC_query_task_threads) {
484 #   ifndef GC_NO_THREADS_DISCOVERY
485       GC_bool changed;
486       thread_act_array_t act_list, prev_list;
487       mach_msg_type_number_t listcount, prevcount;
488
489       /* Clear out the mach threads list table.  We do not need to      */
490       /* really clear GC_mach_threads[] as it is used only in the range */
491       /* from 0 to GC_mach_threads_count-1, inclusive.                  */
492       GC_mach_threads_count = 0;
493
494       /* Loop stopping threads until you have gone over the whole list  */
495       /* twice without a new one appearing.  thread_create() won't      */
496       /* return (and thus the thread stop) until the new thread exists, */
497       /* so there is no window whereby you could stop a thread,         */
498       /* recognize it is stopped, but then have a new thread it created */
499       /* before stopping show up later.                                 */
500       changed = TRUE;
501       prev_list = NULL;
502       prevcount = 0;
503       do {
504         kern_result = task_threads(my_task, &act_list, &listcount);
505
506         if (kern_result == KERN_SUCCESS) {
507           changed = GC_suspend_thread_list(act_list, listcount, prev_list,
508                                            prevcount, my_thread);
509
510           if (prev_list != NULL) {
511             for (i = 0; i < prevcount; i++)
512               mach_port_deallocate(my_task, prev_list[i]);
513
514             vm_deallocate(my_task, (vm_address_t)prev_list,
515                           sizeof(thread_t) * prevcount);
516           }
517
518           /* Repeat while having changes. */
519           prev_list = act_list;
520           prevcount = listcount;
521         }
522       } while (changed);
523
524       GC_ASSERT(prev_list != 0);
525       for (i = 0; i < prevcount; i++)
526         mach_port_deallocate(my_task, prev_list[i]);
527       vm_deallocate(my_task, (vm_address_t)act_list,
528                     sizeof(thread_t) * listcount);
529 #   endif /* !GC_NO_THREADS_DISCOVERY */
530
531   } else {
532     for (i = 0; i < THREAD_TABLE_SZ; i++) {
533       GC_thread p;
534
535       for (p = GC_threads[i]; p != NULL; p = p->next) {
536         if ((p->flags & FINISHED) == 0 && !p->thread_blocked &&
537              p->stop_info.mach_thread != my_thread) {
538
539           kern_result = thread_suspend(p->stop_info.mach_thread);
540           if (kern_result != KERN_SUCCESS)
541             ABORT("thread_suspend failed");
542         }
543       }
544     }
545   }
546
547 # ifdef MPROTECT_VDB
548     if(GC_incremental) {
549       GC_mprotect_stop();
550     }
551 # endif
552 # ifdef PARALLEL_MARK
553     if (GC_parallel)
554       GC_release_mark_lock();
555 # endif
556
557 # ifdef DEBUG_THREADS
558     GC_log_printf("World stopped from 0x%lx\n", (unsigned long)my_thread);
559 # endif
560   mach_port_deallocate(my_task, my_thread);
561 }
562
563 GC_INLINE void GC_thread_resume(thread_act_t thread)
564 {
565   kern_return_t kern_result;
566 # if defined(DEBUG_THREADS) || defined(GC_ASSERTIONS)
567     struct thread_basic_info info;
568     mach_msg_type_number_t outCount = THREAD_INFO_MAX;
569     kern_result = thread_info(thread, THREAD_BASIC_INFO,
570                               (thread_info_t)&info, &outCount);
571     if (kern_result != KERN_SUCCESS)
572       ABORT("thread_info failed");
573 # endif
574 # ifdef DEBUG_THREADS
575     GC_log_printf("Resuming thread 0x%lx with state %d\n",
576                   (unsigned long)thread, info.run_state);
577 # endif
578   /* Resume the thread */
579   kern_result = thread_resume(thread);
580   if (kern_result != KERN_SUCCESS)
581     ABORT("thread_resume failed");
582 }
583
584 /* Caller holds allocation lock, and has held it continuously since     */
585 /* the world stopped.                                                   */
586 GC_INNER void GC_start_world(void)
587 {
588   task_t my_task = current_task();
589   int i;
590 # ifdef DEBUG_THREADS
591     GC_log_printf("World starting\n");
592 # endif
593 # ifdef MPROTECT_VDB
594     if(GC_incremental) {
595       GC_mprotect_resume();
596     }
597 # endif
598
599   if (GC_query_task_threads) {
600 #   ifndef GC_NO_THREADS_DISCOVERY
601       int j = GC_mach_threads_count;
602       kern_return_t kern_result;
603       thread_act_array_t act_list;
604       mach_msg_type_number_t listcount;
605
606       kern_result = task_threads(my_task, &act_list, &listcount);
607       if (kern_result != KERN_SUCCESS)
608         ABORT("task_threads failed");
609
610       for (i = 0; i < (int)listcount; i++) {
611         thread_act_t thread = act_list[i];
612         int last_found = j;        /* The thread index found during the   */
613                                    /* previous iteration (count value     */
614                                    /* means no thread found yet).         */
615
616         /* Search for the thread starting from the last found one first.  */
617         while (++j < GC_mach_threads_count) {
618           if (GC_mach_threads[j].thread == thread)
619             break;
620         }
621         if (j >= GC_mach_threads_count) {
622           /* If not found, search in the rest (beginning) of the list.    */
623           for (j = 0; j < last_found; j++) {
624             if (GC_mach_threads[j].thread == thread)
625               break;
626           }
627         }
628
629         if (j != last_found) {
630           /* The thread is found in GC_mach_threads.      */
631           if (GC_mach_threads[j].already_suspended) {
632 #           ifdef DEBUG_THREADS
633               GC_log_printf("Not resuming already suspended thread 0x%lx\n",
634                             (unsigned long)thread);
635 #           endif
636           } else {
637             GC_thread_resume(thread);
638           }
639         }
640
641         mach_port_deallocate(my_task, thread);
642       }
643       vm_deallocate(my_task, (vm_address_t)act_list,
644                     sizeof(thread_t) * listcount);
645 #   endif /* !GC_NO_THREADS_DISCOVERY */
646
647   } else {
648     mach_port_t my_thread = mach_thread_self();
649
650     for (i = 0; i < THREAD_TABLE_SZ; i++) {
651       GC_thread p;
652       for (p = GC_threads[i]; p != NULL; p = p->next) {
653         if ((p->flags & FINISHED) == 0 && !p->thread_blocked &&
654              p->stop_info.mach_thread != my_thread)
655           GC_thread_resume(p->stop_info.mach_thread);
656       }
657     }
658
659     mach_port_deallocate(my_task, my_thread);
660   }
661
662 # ifdef DEBUG_THREADS
663     GC_log_printf("World started\n");
664 # endif
665 }
666
667 #endif /* GC_DARWIN_THREADS */