Upgrade Boehm GC to 7.2alpha4.
[cacao.git] / src / mm / boehm-gc / 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-2009 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 #ifdef DARWIN_DONT_PARSE_STACK
43
44 GC_INNER void GC_push_all_stacks(void)
45 {
46   int i;
47   kern_return_t r;
48   GC_thread p;
49   pthread_t me;
50   ptr_t lo, hi;
51   word total_size = 0;
52   GC_THREAD_STATE_T state;
53   /* MACHINE_THREAD_STATE_COUNT doesn't seem to be defined everywhere.  */
54   /* Hence we use our own version.                                      */
55   mach_msg_type_number_t thread_state_count = GC_MACH_THREAD_STATE_COUNT;
56
57   me = pthread_self();
58   if (!GC_thr_initialized)
59     GC_thr_init();
60
61   for(i = 0; i < THREAD_TABLE_SZ; i++) {
62     for(p = GC_threads[i]; p != 0; p = p->next) {
63       if(p->flags & FINISHED) continue;
64       if(pthread_equal(p->id, me)) {
65         lo = GC_approx_sp();
66       } else {
67         /* Get the thread state (registers, etc) */
68         r = thread_get_state(p->stop_info.mach_thread, GC_MACH_THREAD_STATE,
69                              (natural_t*)&state, &thread_state_count);
70
71 #       ifdef DEBUG_THREADS
72           GC_printf("thread_get_state return value = %d\n", r);
73 #       endif
74
75         if(r != KERN_SUCCESS)
76           ABORT("thread_get_state failed");
77
78 #       if defined(I386)
79           lo = (void*)state . THREAD_FLD (esp);
80           GC_push_one(state . THREAD_FLD (eax));
81           GC_push_one(state . THREAD_FLD (ebx));
82           GC_push_one(state . THREAD_FLD (ecx));
83           GC_push_one(state . THREAD_FLD (edx));
84           GC_push_one(state . THREAD_FLD (edi));
85           GC_push_one(state . THREAD_FLD (esi));
86           GC_push_one(state . THREAD_FLD (ebp));
87
88 #       elif defined(X86_64)
89           lo = (void*)state . THREAD_FLD (rsp);
90           GC_push_one(state . THREAD_FLD (rax));
91           GC_push_one(state . THREAD_FLD (rbx));
92           GC_push_one(state . THREAD_FLD (rcx));
93           GC_push_one(state . THREAD_FLD (rdx));
94           GC_push_one(state . THREAD_FLD (rdi));
95           GC_push_one(state . THREAD_FLD (rsi));
96           GC_push_one(state . THREAD_FLD (rbp));
97           GC_push_one(state . THREAD_FLD (rsp));
98           GC_push_one(state . THREAD_FLD (r8));
99           GC_push_one(state . THREAD_FLD (r9));
100           GC_push_one(state . THREAD_FLD (r10));
101           GC_push_one(state . THREAD_FLD (r11));
102           GC_push_one(state . THREAD_FLD (r12));
103           GC_push_one(state . THREAD_FLD (r13));
104           GC_push_one(state . THREAD_FLD (r14));
105           GC_push_one(state . THREAD_FLD (r15));
106           GC_push_one(state . THREAD_FLD (rip));
107           GC_push_one(state . THREAD_FLD (rflags));
108           GC_push_one(state . THREAD_FLD (cs));
109           GC_push_one(state . THREAD_FLD (fs));
110           GC_push_one(state . THREAD_FLD (gs));
111
112 #       elif defined(POWERPC)
113           lo = (void*)(state . THREAD_FLD (r1) - PPC_RED_ZONE_SIZE);
114
115           GC_push_one(state . THREAD_FLD (r0));
116           GC_push_one(state . THREAD_FLD (r2));
117           GC_push_one(state . THREAD_FLD (r3));
118           GC_push_one(state . THREAD_FLD (r4));
119           GC_push_one(state . THREAD_FLD (r5));
120           GC_push_one(state . THREAD_FLD (r6));
121           GC_push_one(state . THREAD_FLD (r7));
122           GC_push_one(state . THREAD_FLD (r8));
123           GC_push_one(state . THREAD_FLD (r9));
124           GC_push_one(state . THREAD_FLD (r10));
125           GC_push_one(state . THREAD_FLD (r11));
126           GC_push_one(state . THREAD_FLD (r12));
127           GC_push_one(state . THREAD_FLD (r13));
128           GC_push_one(state . THREAD_FLD (r14));
129           GC_push_one(state . THREAD_FLD (r15));
130           GC_push_one(state . THREAD_FLD (r16));
131           GC_push_one(state . THREAD_FLD (r17));
132           GC_push_one(state . THREAD_FLD (r18));
133           GC_push_one(state . THREAD_FLD (r19));
134           GC_push_one(state . THREAD_FLD (r20));
135           GC_push_one(state . THREAD_FLD (r21));
136           GC_push_one(state . THREAD_FLD (r22));
137           GC_push_one(state . THREAD_FLD (r23));
138           GC_push_one(state . THREAD_FLD (r24));
139           GC_push_one(state . THREAD_FLD (r25));
140           GC_push_one(state . THREAD_FLD (r26));
141           GC_push_one(state . THREAD_FLD (r27));
142           GC_push_one(state . THREAD_FLD (r28));
143           GC_push_one(state . THREAD_FLD (r29));
144           GC_push_one(state . THREAD_FLD (r30));
145           GC_push_one(state . THREAD_FLD (r31));
146
147 #       elif defined(ARM32)
148           lo = (void*)state.__sp;
149
150           GC_push_one(state.__r[0]);
151           GC_push_one(state.__r[1]);
152           GC_push_one(state.__r[2]);
153           GC_push_one(state.__r[3]);
154           GC_push_one(state.__r[4]);
155           GC_push_one(state.__r[5]);
156           GC_push_one(state.__r[6]);
157           GC_push_one(state.__r[7]);
158           GC_push_one(state.__r[8]);
159           GC_push_one(state.__r[9]);
160           GC_push_one(state.__r[10]);
161           GC_push_one(state.__r[11]);
162           GC_push_one(state.__r[12]);
163           /* GC_push_one(state.__sp); */
164           GC_push_one(state.__lr);
165           GC_push_one(state.__pc);
166           GC_push_one(state.__cpsr);
167
168 #       else
169 #         error FIXME for non-x86 || ppc || arm architectures
170 #       endif
171       } /* p != me */
172       if(p->flags & MAIN_THREAD)
173         hi = GC_stackbottom;
174       else
175         hi = p->stack_end;
176 #     ifdef DEBUG_THREADS
177         GC_printf("Darwin: Stack for thread 0x%lx = [%p,%p)\n",
178                   (unsigned long) p -> id, lo, hi);
179 #     endif
180       GC_push_all_stack_frames(lo, hi, p -> activation_frame);
181       total_size += hi - lo; /* lo <= hi */
182     } /* for(p=GC_threads[i]...) */
183   } /* for(i=0;i<THREAD_TABLE_SZ...) */
184   GC_total_stacksize = total_size;
185 }
186
187 #else /* !DARWIN_DONT_PARSE_STACK; Use FindTopOfStack() */
188
189 typedef struct StackFrame {
190   unsigned long savedSP;
191   unsigned long savedCR;
192   unsigned long savedLR;
193   unsigned long reserved[2];
194   unsigned long savedRTOC;
195 } StackFrame;
196
197 static unsigned long FindTopOfStack(unsigned long stack_start)
198 {
199   StackFrame    *frame;
200
201   if (stack_start == 0) {
202 # ifdef POWERPC
203 #   if CPP_WORDSZ == 32
204       __asm__ volatile("lwz     %0,0(r1)" : "=r" (frame));
205 #   else
206       __asm__ volatile("ld      %0,0(r1)" : "=r" (frame));
207 #   endif
208 # endif
209   } else {
210     frame = (StackFrame *)stack_start;
211   }
212
213 # ifdef DEBUG_THREADS
214     /* GC_printf("FindTopOfStack start at sp = %p\n", frame); */
215 # endif
216   do {
217     if (frame->savedSP == 0)
218       break;
219     /* if there are no more stack frames, stop */
220
221     frame = (StackFrame*)frame->savedSP;
222
223     /* we do these next two checks after going to the next frame
224        because the LR for the first stack frame in the loop
225        is not set up on purpose, so we shouldn't check it. */
226     if ((frame->savedLR & ~3) == 0)
227       break; /* if the next LR is bogus, stop */
228     if ((~(frame->savedLR) & ~3) == 0)
229       break; /* ditto */
230   } while (1);
231
232 # ifdef DEBUG_THREADS
233     /* GC_printf("FindTopOfStack finish at sp = %p\n", frame); */
234 # endif
235   return (unsigned long)frame;
236 }
237
238 GC_INNER void GC_push_all_stacks(void)
239 {
240   unsigned int i;
241   task_t my_task;
242   kern_return_t r;
243   mach_port_t me;
244   ptr_t lo, hi;
245   word total_size = 0;
246   thread_act_array_t act_list = 0;
247   mach_msg_type_number_t listcount = 0;
248
249   me = mach_thread_self();
250   if (!GC_thr_initialized)
251     GC_thr_init();
252
253   my_task = current_task();
254   r = task_threads(my_task, &act_list, &listcount);
255   if(r != KERN_SUCCESS)
256     ABORT("task_threads failed");
257   for(i = 0; i < listcount; i++) {
258     thread_act_t thread = act_list[i];
259     if (thread == me) {
260       lo = GC_approx_sp();
261       hi = (ptr_t)FindTopOfStack(0);
262     } else {
263 #     if defined(POWERPC)
264         GC_THREAD_STATE_T info;
265         mach_msg_type_number_t outCount = THREAD_STATE_MAX;
266         r = thread_get_state(thread, GC_MACH_THREAD_STATE, (natural_t *)&info,
267                              &outCount);
268         if(r != KERN_SUCCESS)
269           ABORT("task_get_state failed");
270
271         lo = (void*)(info . THREAD_FLD (r1) - PPC_RED_ZONE_SIZE);
272         hi = (ptr_t)FindTopOfStack(info . THREAD_FLD (r1));
273
274         GC_push_one(info . THREAD_FLD (r0));
275         GC_push_one(info . THREAD_FLD (r2));
276         GC_push_one(info . THREAD_FLD (r3));
277         GC_push_one(info . THREAD_FLD (r4));
278         GC_push_one(info . THREAD_FLD (r5));
279         GC_push_one(info . THREAD_FLD (r6));
280         GC_push_one(info . THREAD_FLD (r7));
281         GC_push_one(info . THREAD_FLD (r8));
282         GC_push_one(info . THREAD_FLD (r9));
283         GC_push_one(info . THREAD_FLD (r10));
284         GC_push_one(info . THREAD_FLD (r11));
285         GC_push_one(info . THREAD_FLD (r12));
286         GC_push_one(info . THREAD_FLD (r13));
287         GC_push_one(info . THREAD_FLD (r14));
288         GC_push_one(info . THREAD_FLD (r15));
289         GC_push_one(info . THREAD_FLD (r16));
290         GC_push_one(info . THREAD_FLD (r17));
291         GC_push_one(info . THREAD_FLD (r18));
292         GC_push_one(info . THREAD_FLD (r19));
293         GC_push_one(info . THREAD_FLD (r20));
294         GC_push_one(info . THREAD_FLD (r21));
295         GC_push_one(info . THREAD_FLD (r22));
296         GC_push_one(info . THREAD_FLD (r23));
297         GC_push_one(info . THREAD_FLD (r24));
298         GC_push_one(info . THREAD_FLD (r25));
299         GC_push_one(info . THREAD_FLD (r26));
300         GC_push_one(info . THREAD_FLD (r27));
301         GC_push_one(info . THREAD_FLD (r28));
302         GC_push_one(info . THREAD_FLD (r29));
303         GC_push_one(info . THREAD_FLD (r30));
304         GC_push_one(info . THREAD_FLD (r31));
305
306 #     elif defined(I386)
307         /* FIXME: Remove after testing: */
308         WARN("This is completely untested and likely will not work\n", 0);
309         GC_THREAD_STATE_T info;
310         mach_msg_type_number_t outCount = THREAD_STATE_MAX;
311         r = thread_get_state(thread, GC_MACH_THREAD_STATE, (natural_t *)&info,
312                              &outCount);
313         if(r != KERN_SUCCESS)
314           ABORT("task_get_state failed");
315
316         lo = (void*)info . THREAD_FLD (esp);
317         hi = (ptr_t)FindTopOfStack(info . THREAD_FLD (esp));
318
319         GC_push_one(info . THREAD_FLD (eax));
320         GC_push_one(info . THREAD_FLD (ebx));
321         GC_push_one(info . THREAD_FLD (ecx));
322         GC_push_one(info . THREAD_FLD (edx));
323         GC_push_one(info . THREAD_FLD (edi));
324         GC_push_one(info . THREAD_FLD (esi));
325         /* GC_push_one(info . THREAD_FLD (ebp));  */
326         /* GC_push_one(info . THREAD_FLD (esp));  */
327         GC_push_one(info . THREAD_FLD (ss));
328         GC_push_one(info . THREAD_FLD (eip));
329         GC_push_one(info . THREAD_FLD (cs));
330         GC_push_one(info . THREAD_FLD (ds));
331         GC_push_one(info . THREAD_FLD (es));
332         GC_push_one(info . THREAD_FLD (fs));
333         GC_push_one(info . THREAD_FLD (gs));
334
335 #     elif defined(X86_64)
336         GC_THREAD_STATE_T info;
337         mach_msg_type_number_t outCount = THREAD_STATE_MAX;
338         r = thread_get_state(thread, GC_MACH_THREAD_STATE, (natural_t *)&info,
339                              &outCount);
340         if(r != KERN_SUCCESS)
341           ABORT("task_get_state failed");
342
343         lo = (void*)info . THREAD_FLD (rsp);
344         hi = (ptr_t)FindTopOfStack(info . THREAD_FLD (rsp));
345
346         GC_push_one(info . THREAD_FLD (rax));
347         GC_push_one(info . THREAD_FLD (rbx));
348         GC_push_one(info . THREAD_FLD (rcx));
349         GC_push_one(info . THREAD_FLD (rdx));
350         GC_push_one(info . THREAD_FLD (rdi));
351         GC_push_one(info . THREAD_FLD (rsi));
352         GC_push_one(info . THREAD_FLD (rbp));
353         GC_push_one(info . THREAD_FLD (rsp));
354         GC_push_one(info . THREAD_FLD (r8));
355         GC_push_one(info . THREAD_FLD (r9));
356         GC_push_one(info . THREAD_FLD (r10));
357         GC_push_one(info . THREAD_FLD (r11));
358         GC_push_one(info . THREAD_FLD (r12));
359         GC_push_one(info . THREAD_FLD (r13));
360         GC_push_one(info . THREAD_FLD (r14));
361         GC_push_one(info . THREAD_FLD (r15));
362         GC_push_one(info . THREAD_FLD (rip));
363         GC_push_one(info . THREAD_FLD (rflags));
364         GC_push_one(info . THREAD_FLD (cs));
365         GC_push_one(info . THREAD_FLD (fs));
366         GC_push_one(info . THREAD_FLD (gs));
367
368 #      elif defined(ARM32)
369         GC_THREAD_STATE_T info;
370         mach_msg_type_number_t outCount = THREAD_STATE_MAX;
371         r = thread_get_state(thread, GC_MACH_THREAD_STATE, (natural_t *)&info,
372                              &outCount);
373         if(r != KERN_SUCCESS)
374           ABORT("task_get_state failed");
375
376         hi = (ptr_t)FindTopOfStack(info . __sp);
377
378         lo = (void*)info.__sp;
379
380         GC_push_one(info.__r[0]);
381         GC_push_one(info.__r[1]);
382         GC_push_one(info.__r[2]);
383         GC_push_one(info.__r[3]);
384         GC_push_one(info.__r[4]);
385         GC_push_one(info.__r[5]);
386         GC_push_one(info.__r[6]);
387         GC_push_one(info.__r[7]);
388         GC_push_one(info.__r[8]);
389         GC_push_one(info.__r[9]);
390         GC_push_one(info.__r[10]);
391         GC_push_one(info.__r[11]);
392         GC_push_one(info.__r[12]);
393         /* GC_push_one(info.__sp); */
394         GC_push_one(info.__lr);
395         GC_push_one(info.__pc);
396         GC_push_one(info.__cpsr);
397
398 #     else
399 #       error FIXME for non-x86 || ppc || arm architectures
400 #     endif
401       }
402 #     ifdef DEBUG_THREADS
403         GC_printf("Darwin: Stack for thread 0x%lx = [%p,%p)\n",
404                   (unsigned long) thread, lo, hi);
405 #     endif
406       /* FIXME: use GC_push_all_stack_frames. */
407       GC_push_all_stack(lo, hi);
408       mach_port_deallocate(my_task, thread);
409       total_size += hi - lo; /* lo <= hi */
410     } /* for(i=0; ...) */
411     vm_deallocate(my_task, (vm_address_t)act_list,
412                   sizeof(thread_t) * listcount);
413     mach_port_deallocate(my_task, me);
414     GC_total_stacksize = total_size;
415 }
416 #endif /* !DARWIN_DONT_PARSE_STACK */
417
418 STATIC mach_port_t GC_mach_handler_thread = 0;
419 STATIC int GC_use_mach_handler_thread = 0;
420
421 static struct GC_mach_thread GC_mach_threads[THREAD_TABLE_SZ];
422 STATIC int GC_mach_threads_count = 0;
423
424 GC_INNER void GC_stop_init(void)
425 {
426   int i;
427
428   for (i = 0; i < THREAD_TABLE_SZ; i++) {
429     GC_mach_threads[i].thread = 0;
430     GC_mach_threads[i].already_suspended = 0;
431   }
432   GC_mach_threads_count = 0;
433 }
434
435 /* returns true if there's a thread in act_list that wasn't in old_list */
436 STATIC int GC_suspend_thread_list(thread_act_array_t act_list, int count,
437                                   thread_act_array_t old_list, int old_count)
438 {
439   mach_port_t my_thread = mach_thread_self();
440   int i, j;
441
442   int changed = 0;
443
444   for(i = 0; i < count; i++) {
445     thread_act_t thread = act_list[i];
446 #   ifdef DEBUG_THREADS
447       GC_printf("Attempting to suspend thread %p\n", thread);
448 #   endif
449     /* find the current thread in the old list */
450     int found = 0;
451     for(j = 0; j < old_count; j++) {
452       thread_act_t old_thread = old_list[j];
453       if (old_thread == thread) {
454         found = 1;
455         break;
456       }
457     }
458     if (!found) {
459       /* add it to the GC_mach_threads list */
460       GC_mach_threads[GC_mach_threads_count].thread = thread;
461       /* default is not suspended */
462       GC_mach_threads[GC_mach_threads_count].already_suspended = 0;
463       changed = 1;
464     }
465
466     if (thread != my_thread
467         && (!GC_use_mach_handler_thread
468             || (GC_use_mach_handler_thread
469                 && GC_mach_handler_thread != thread))) {
470       struct thread_basic_info info;
471       mach_msg_type_number_t outCount = THREAD_INFO_MAX;
472       kern_return_t kern_result = thread_info(thread, THREAD_BASIC_INFO,
473                                 (thread_info_t)&info, &outCount);
474       if(kern_result != KERN_SUCCESS) {
475         /* the thread may have quit since the thread_threads () call
476          * we mark already_suspended so it's not dealt with anymore later
477          */
478         if (!found) {
479           GC_mach_threads[GC_mach_threads_count].already_suspended = TRUE;
480           GC_mach_threads_count++;
481         }
482         continue;
483       }
484 #     ifdef DEBUG_THREADS
485         GC_printf("Thread state for 0x%lx = %d\n", (unsigned long)thread,
486                   info.run_state);
487 #     endif
488       if (!found) {
489         GC_mach_threads[GC_mach_threads_count].already_suspended
490           = info.suspend_count;
491       }
492       if (info.suspend_count)
493         continue;
494
495 #     ifdef DEBUG_THREADS
496         GC_printf("Suspending 0x%lx\n", (unsigned long)thread);
497 #     endif
498       /* Suspend the thread */
499       kern_result = thread_suspend(thread);
500       if(kern_result != KERN_SUCCESS) {
501         /* the thread may have quit since the thread_threads () call
502          * we mark already_suspended so it's not dealt with anymore later
503          */
504         if (!found) {
505           GC_mach_threads[GC_mach_threads_count].already_suspended = TRUE;
506           GC_mach_threads_count++;
507         }
508         continue;
509       }
510     }
511     if (!found) GC_mach_threads_count++;
512   }
513   mach_port_deallocate(current_task(), my_thread);
514   return changed;
515 }
516
517 #ifdef MPROTECT_VDB
518   GC_INNER void GC_mprotect_stop(void);
519   GC_INNER void GC_mprotect_resume(void);
520 #endif
521
522 /* Caller holds allocation lock.        */
523 GC_INNER void GC_stop_world(void)
524 {
525     unsigned int i, changes;
526     task_t my_task = current_task();
527     mach_port_t my_thread = mach_thread_self();
528     kern_return_t kern_result;
529     thread_act_array_t act_list, prev_list;
530     mach_msg_type_number_t listcount, prevcount;
531
532 #   ifdef DEBUG_THREADS
533       GC_printf("Stopping the world from 0x%lx\n",
534                 (unsigned long)mach_thread_self());
535 #   endif
536
537     /* clear out the mach threads list table */
538     GC_stop_init();
539
540     /* Make sure all free list construction has stopped before we start. */
541     /* No new construction can start, since free list construction is   */
542     /* required to acquire and release the GC lock before it starts,    */
543     /* and we have the lock.                                            */
544 #   ifdef PARALLEL_MARK
545       if (GC_parallel) {
546         GC_acquire_mark_lock();
547         GC_ASSERT(GC_fl_builder_count == 0);
548         /* We should have previously waited for it to become zero. */
549       }
550 #   endif /* PARALLEL_MARK */
551
552     /* Loop stopping threads until you have gone over the whole list
553        twice without a new one appearing. thread_create() won't
554        return (and thus the thread stop) until the new thread
555        exists, so there is no window whereby you could stop a
556        thread, recognise it is stopped, but then have a new thread
557        it created before stopping show up later.
558     */
559
560     /* FIXME: This seems to erroneously stop the parallel marker threads? */
561
562     changes = 1;
563     prev_list = NULL;
564     prevcount = 0;
565     do {
566       int result;
567       kern_result = task_threads(my_task, &act_list, &listcount);
568
569       if(kern_result == KERN_SUCCESS) {
570         result = GC_suspend_thread_list(act_list, listcount, prev_list,
571                                         prevcount);
572         changes = result;
573
574         if(prev_list != NULL) {
575           for(i = 0; i < prevcount; i++)
576             mach_port_deallocate(my_task, prev_list[i]);
577
578           vm_deallocate(my_task, (vm_address_t)prev_list,
579                         sizeof(thread_t) * prevcount);
580         }
581         prev_list = act_list;
582         prevcount = listcount;
583       }
584     } while (changes);
585     GC_ASSERT(prev_list != 0);
586     for(i = 0; i < prevcount; i++)
587       mach_port_deallocate(my_task, prev_list[i]);
588
589     vm_deallocate(my_task, (vm_address_t)act_list,
590                   sizeof(thread_t) * listcount);
591
592 #   ifdef MPROTECT_VDB
593       if(GC_incremental) {
594         GC_mprotect_stop();
595       }
596 #   endif
597
598 #   ifdef PARALLEL_MARK
599       if (GC_parallel)
600         GC_release_mark_lock();
601 #   endif
602 #   ifdef DEBUG_THREADS
603       GC_printf("World stopped from 0x%lx\n", (unsigned long)my_thread);
604 #   endif
605
606     mach_port_deallocate(my_task, my_thread);
607 }
608
609 /* Caller holds allocation lock, and has held it continuously since     */
610 /* the world stopped.                                                   */
611 GC_INNER void GC_start_world(void)
612 {
613   task_t my_task = current_task();
614   mach_port_t my_thread = mach_thread_self();
615   unsigned int i;
616   int j;
617   kern_return_t kern_result;
618   thread_act_array_t act_list;
619   mach_msg_type_number_t listcount;
620   struct thread_basic_info info;
621   mach_msg_type_number_t outCount = THREAD_INFO_MAX;
622
623 #   ifdef DEBUG_THREADS
624       GC_printf("World starting\n");
625 #   endif
626
627 #   ifdef MPROTECT_VDB
628       if(GC_incremental) {
629         GC_mprotect_resume();
630       }
631 #   endif
632
633     kern_result = task_threads(my_task, &act_list, &listcount);
634     for(i = 0; i < listcount; i++) {
635       thread_act_t thread = act_list[i];
636       if (thread != my_thread
637           && (!GC_use_mach_handler_thread
638               || (GC_use_mach_handler_thread
639                   && GC_mach_handler_thread != thread))) {
640         for(j = 0; j < GC_mach_threads_count; j++) {
641           if (thread == GC_mach_threads[j].thread) {
642             if (GC_mach_threads[j].already_suspended) {
643 #             ifdef DEBUG_THREADS
644                 GC_printf("Not resuming already suspended thread %p\n", thread);
645 #             endif
646               continue;
647             }
648             kern_result = thread_info(thread, THREAD_BASIC_INFO,
649                                       (thread_info_t)&info, &outCount);
650             if(kern_result != KERN_SUCCESS)
651               ABORT("thread_info failed");
652 #           ifdef DEBUG_THREADS
653               GC_printf("Thread state for 0x%lx = %d\n", (unsigned long)thread,
654                          info.run_state);
655               GC_printf("Resuming 0x%lx\n", (unsigned long)thread);
656 #           endif
657             /* Resume the thread */
658             kern_result = thread_resume(thread);
659             if(kern_result != KERN_SUCCESS)
660               ABORT("thread_resume failed");
661           }
662         }
663       }
664       mach_port_deallocate(my_task, thread);
665     }
666     vm_deallocate(my_task, (vm_address_t)act_list,
667                   sizeof(thread_t) * listcount);
668
669     mach_port_deallocate(my_task, my_thread);
670 #   ifdef DEBUG_THREADS
671       GC_printf("World started\n");
672 #   endif
673 }
674
675 GC_INNER void GC_darwin_register_mach_handler_thread(mach_port_t thread)
676 {
677   GC_mach_handler_thread = thread;
678   GC_use_mach_handler_thread = 1;
679 }
680
681 #endif