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