OSDN Git Service

mn10300: Add attribute enabled.
[pf3gnuchains/gcc-fork.git] / libgo / runtime / go-go.c
1 /* go-go.c -- the go function.
2
3    Copyright 2009 The Go Authors. All rights reserved.
4    Use of this source code is governed by a BSD-style
5    license that can be found in the LICENSE file.  */
6
7 #include <errno.h>
8 #include <limits.h>
9 #include <signal.h>
10 #include <stdint.h>
11 #include <stdlib.h>
12 #include <pthread.h>
13 #include <semaphore.h>
14
15 #include "config.h"
16 #include "go-assert.h"
17 #include "go-panic.h"
18 #include "go-alloc.h"
19 #include "runtime.h"
20 #include "malloc.h"
21
22 #ifdef USING_SPLIT_STACK
23 /* FIXME: This is not declared anywhere.  */
24 extern void *__splitstack_find (void *, void *, size_t *, void **, void **,
25                                 void **);
26 #endif
27
28 /* We stop the threads by sending them the signal GO_SIG_STOP and we
29    start them by sending them the signal GO_SIG_START.  */
30
31 #define GO_SIG_START (SIGRTMIN + 1)
32 #define GO_SIG_STOP (SIGRTMIN + 2)
33
34 #ifndef SA_RESTART
35   #define SA_RESTART 0
36 #endif
37
38 /* A doubly linked list of the threads we have started.  */
39
40 struct __go_thread_id
41 {
42   /* Links.  */
43   struct __go_thread_id *prev;
44   struct __go_thread_id *next;
45   /* True if the thread ID has not yet been filled in.  */
46   _Bool tentative;
47   /* Thread ID.  */
48   pthread_t id;
49   /* Thread's M structure.  */
50   struct M *m;
51   /* If the thread ID has not been filled in, the function we are
52      running.  */
53   void (*pfn) (void *);
54   /* If the thread ID has not been filled in, the argument to the
55      function.  */
56   void *arg;
57 };
58
59 static struct __go_thread_id *__go_all_thread_ids;
60
61 /* A lock to control access to ALL_THREAD_IDS.  */
62
63 static pthread_mutex_t __go_thread_ids_lock = PTHREAD_MUTEX_INITIALIZER;
64
65 /* A semaphore used to wait until all the threads have stopped.  */
66
67 static sem_t __go_thread_ready_sem;
68
69 /* A signal set used to wait until garbage collection is complete.  */
70
71 static sigset_t __go_thread_wait_sigset;
72
73 /* Remove the current thread from the list of threads.  */
74
75 static void
76 remove_current_thread (void)
77 {
78   struct __go_thread_id *list_entry;
79   MCache *mcache;
80   int i;
81   
82   list_entry = m->list_entry;
83   mcache = m->mcache;
84
85   i = pthread_mutex_lock (&__go_thread_ids_lock);
86   __go_assert (i == 0);
87
88   if (list_entry->prev != NULL)
89     list_entry->prev->next = list_entry->next;
90   else
91     __go_all_thread_ids = list_entry->next;
92   if (list_entry->next != NULL)
93     list_entry->next->prev = list_entry->prev;
94
95   runtime_MCache_ReleaseAll (mcache);
96
97   i = pthread_mutex_unlock (&__go_thread_ids_lock);
98   __go_assert (i == 0);
99
100   runtime_lock (&runtime_mheap);
101   mstats.heap_alloc += mcache->local_alloc;
102   mstats.heap_objects += mcache->local_objects;
103   __builtin_memset (mcache, 0, sizeof (struct MCache));
104   runtime_FixAlloc_Free (&runtime_mheap.cachealloc, mcache);
105   runtime_unlock (&runtime_mheap);
106
107   free (list_entry);
108 }
109
110 /* Start the thread.  */
111
112 static void *
113 start_go_thread (void *thread_arg)
114 {
115   struct M *newm = (struct M *) thread_arg;
116   void (*pfn) (void *);
117   void *arg;
118   struct __go_thread_id *list_entry;
119   int i;
120
121 #ifdef __rtems__
122   __wrap_rtems_task_variable_add ((void **) &m);
123   __wrap_rtems_task_variable_add ((void **) &__go_panic_defer);
124 #endif
125
126   m = newm;
127
128   list_entry = newm->list_entry;
129
130   pfn = list_entry->pfn;
131   arg = list_entry->arg;
132
133 #ifndef USING_SPLIT_STACK
134   /* If we don't support split stack, record the current stack as the
135      top of the stack.  There shouldn't be anything relevant to the
136      garbage collector above this point.  */
137   m->gc_sp = (void *) &arg;
138 #endif
139
140   /* Finish up the entry on the thread list.  */
141
142   i = pthread_mutex_lock (&__go_thread_ids_lock);
143   __go_assert (i == 0);
144
145   list_entry->id = pthread_self ();
146   list_entry->pfn = NULL;
147   list_entry->arg = NULL;
148   list_entry->tentative = 0;
149
150   i = pthread_mutex_unlock (&__go_thread_ids_lock);
151   __go_assert (i == 0);
152
153   (*pfn) (arg);
154
155   remove_current_thread ();
156
157   return NULL;
158 }
159
160 /* The runtime.Goexit function.  */
161
162 void Goexit (void) asm ("libgo_runtime.runtime.Goexit");
163
164 void
165 Goexit (void)
166 {
167   remove_current_thread ();
168   pthread_exit (NULL);
169   abort ();
170 }
171
172 /* Implement the go statement.  */
173
174 void
175 __go_go (void (*pfn) (void*), void *arg)
176 {
177   int i;
178   pthread_attr_t attr;
179   struct M *newm;
180   struct __go_thread_id *list_entry;
181   pthread_t tid;
182
183   i = pthread_attr_init (&attr);
184   __go_assert (i == 0);
185   i = pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
186   __go_assert (i == 0);
187
188 #ifdef LINKER_SUPPORTS_SPLIT_STACK
189   /* The linker knows how to handle calls between code which uses
190      -fsplit-stack and code which does not.  That means that we can
191      run with a smaller stack and rely on the -fsplit-stack support to
192      save us.  The GNU/Linux glibc library won't let us have a very
193      small stack, but we make it as small as we can.  */
194 #ifndef PTHREAD_STACK_MIN
195 #define PTHREAD_STACK_MIN 8192
196 #endif
197   i = pthread_attr_setstacksize (&attr, PTHREAD_STACK_MIN);
198   __go_assert (i == 0);
199 #endif
200
201   newm = __go_alloc (sizeof (M));
202
203   list_entry = malloc (sizeof (struct __go_thread_id));
204   list_entry->prev = NULL;
205   list_entry->next = NULL;
206   list_entry->tentative = 1;
207   list_entry->m = newm;
208   list_entry->pfn = pfn;
209   list_entry->arg = arg;
210
211   newm->list_entry = list_entry;
212
213   newm->mcache = runtime_allocmcache ();
214
215   /* Add the thread to the list of all threads, marked as tentative
216      since it is not yet ready to go.  */
217   i = pthread_mutex_lock (&__go_thread_ids_lock);
218   __go_assert (i == 0);
219
220   if (__go_all_thread_ids != NULL)
221     __go_all_thread_ids->prev = list_entry;
222   list_entry->next = __go_all_thread_ids;
223   __go_all_thread_ids = list_entry;
224
225   i = pthread_mutex_unlock (&__go_thread_ids_lock);
226   __go_assert (i == 0);
227
228   /* Start the thread.  */
229   i = pthread_create (&tid, &attr, start_go_thread, newm);
230   __go_assert (i == 0);
231
232   i = pthread_attr_destroy (&attr);
233   __go_assert (i == 0);
234 }
235
236 /* This is the signal handler for GO_SIG_START.  The garbage collector
237    will send this signal to a thread when it wants the thread to
238    start.  We don't have to actually do anything here, but we need a
239    signal handler since ignoring the signal will mean that the
240    sigsuspend will never see it.  */
241
242 static void
243 gc_start_handler (int sig __attribute__ ((unused)))
244 {
245 }
246
247 /* Tell the garbage collector that we are ready, and wait for the
248    garbage collector to tell us that it is done.  This may be called
249    by a signal handler, so it is restricted to using functions which
250    are async cancel safe.  */
251
252 static void
253 stop_for_gc (void)
254 {
255   int i;
256
257   /* Tell the garbage collector about our stack.  */
258 #ifdef USING_SPLIT_STACK
259   m->gc_sp = __splitstack_find (NULL, NULL, &m->gc_len,
260                                 &m->gc_next_segment, &m->gc_next_sp,
261                                 &m->gc_initial_sp);
262 #else
263   {
264     uintptr_t top = (uintptr_t) m->gc_sp;
265     uintptr_t bottom = (uintptr_t) &top;
266     if (top < bottom)
267       {
268         m->gc_next_sp = m->gc_sp;
269         m->gc_len = bottom - top;
270       }
271     else
272       {
273         m->gc_next_sp = (void *) bottom;
274         m->gc_len = top - bottom;
275       }
276   }
277 #endif
278
279   /* FIXME: Perhaps we should just move __go_panic_defer into M.  */
280   m->gc_panic_defer = __go_panic_defer;
281
282   /* Tell the garbage collector that we are ready by posting to the
283      semaphore.  */
284   i = sem_post (&__go_thread_ready_sem);
285   __go_assert (i == 0);
286
287   /* Wait for the garbage collector to tell us to continue.  */
288   sigsuspend (&__go_thread_wait_sigset);
289 }
290
291 /* This is the signal handler for GO_SIG_STOP.  The garbage collector
292    will send this signal to a thread when it wants the thread to
293    stop.  */
294
295 static void
296 gc_stop_handler (int sig __attribute__ ((unused)))
297 {
298   struct M *pm = m;
299
300   if (__sync_bool_compare_and_swap (&pm->mallocing, 1, 1))
301     {
302       /* m->mallocing was already non-zero.  We can't interrupt the
303          thread while it is running an malloc.  Instead, tell it to
304          call back to us when done.  */
305       __sync_bool_compare_and_swap (&pm->gcing, 0, 1);
306       return;
307     }
308
309   if (__sync_bool_compare_and_swap (&pm->nomemprof, 1, 1))
310     {
311       /* Similarly, we can't interrupt the thread while it is building
312          profiling information.  Otherwise we can get into a deadlock
313          when sweepspan calls MProf_Free.  */
314       __sync_bool_compare_and_swap (&pm->gcing_for_prof, 0, 1);
315       return;
316     }
317
318   stop_for_gc ();
319 }
320
321 /* This is called by malloc when it gets a signal during the malloc
322    call itself.  */
323
324 int
325 __go_run_goroutine_gc (int r)
326 {
327   /* Force callee-saved registers to be saved on the stack.  This is
328      not needed if we are invoked from the signal handler, but it is
329      needed if we are called directly, since otherwise we might miss
330      something that a function somewhere up the call stack is holding
331      in a register.  */
332   __builtin_unwind_init ();
333
334   stop_for_gc ();
335
336   /* This avoids tail recursion, to make sure that the saved registers
337      are on the stack.  */
338   return r;
339 }
340
341 /* Stop all the other threads for garbage collection.  */
342
343 void
344 runtime_stoptheworld (void)
345 {
346   int i;
347   pthread_t me;
348   int c;
349   struct __go_thread_id *p;
350
351   i = pthread_mutex_lock (&__go_thread_ids_lock);
352   __go_assert (i == 0);
353
354   me = pthread_self ();
355   c = 0;
356   p = __go_all_thread_ids;
357   while (p != NULL)
358     {
359       if (p->tentative || pthread_equal (me, p->id))
360         p = p->next;
361       else
362         {
363           i = pthread_kill (p->id, GO_SIG_STOP);
364           if (i == 0)
365             {
366               ++c;
367               p = p->next;
368             }
369           else if (i == ESRCH)
370             {
371               struct __go_thread_id *next;
372
373               /* This thread died somehow.  Remove it from the
374                  list.  */
375               next = p->next;
376               if (p->prev != NULL)
377                 p->prev->next = next;
378               else
379                 __go_all_thread_ids = next;
380               if (next != NULL)
381                 next->prev = p->prev;
382               free (p);
383               p = next;
384             }
385           else
386             abort ();
387         }
388     }
389
390   /* Wait for each thread to receive the signal and post to the
391      semaphore.  If a thread receives the signal but contrives to die
392      before it posts to the semaphore, then we will hang forever
393      here.  */
394
395   while (c > 0)
396     {
397       i = sem_wait (&__go_thread_ready_sem);
398       if (i < 0 && errno == EINTR)
399         continue;
400       __go_assert (i == 0);
401       --c;
402     }
403
404   /* The gc_panic_defer field should now be set for all M's except the
405      one in this thread.  Set this one now.  */
406   m->gc_panic_defer = __go_panic_defer;
407
408   /* Leave with __go_thread_ids_lock held.  */
409 }
410
411 /* Scan all the stacks for garbage collection.  This should be called
412    with __go_thread_ids_lock held.  */
413
414 void
415 __go_scanstacks (void (*scan) (byte *, int64))
416 {
417   pthread_t me;
418   struct __go_thread_id *p;
419
420   /* Make sure all the registers for this thread are on the stack.  */
421   __builtin_unwind_init ();
422
423   me = pthread_self ();
424   for (p = __go_all_thread_ids; p != NULL; p = p->next)
425     {
426       if (p->tentative)
427         {
428           /* The goroutine function and argument can be allocated on
429              the heap, so we have to scan them for a thread that has
430              not yet started.  */
431           scan ((void *) &p->pfn, sizeof (void *));
432           scan ((void *) &p->arg, sizeof (void *));
433           scan ((void *) &p->m, sizeof (void *));
434           continue;
435         }
436
437 #ifdef USING_SPLIT_STACK
438
439       void *sp;
440       size_t len;
441       void *next_segment;
442       void *next_sp;
443       void *initial_sp;
444
445       if (pthread_equal (me, p->id))
446         {
447           next_segment = NULL;
448           next_sp = NULL;
449           initial_sp = NULL;
450           sp = __splitstack_find (NULL, NULL, &len, &next_segment,
451                                   &next_sp, &initial_sp);
452         }
453       else
454         {
455           sp = p->m->gc_sp;
456           len = p->m->gc_len;
457           next_segment = p->m->gc_next_segment;
458           next_sp = p->m->gc_next_sp;
459           initial_sp = p->m->gc_initial_sp;
460         }
461
462       while (sp != NULL)
463         {
464           scan (sp, len);
465           sp = __splitstack_find (next_segment, next_sp, &len,
466                                   &next_segment, &next_sp, &initial_sp);
467         }
468
469 #else /* !defined(USING_SPLIT_STACK) */
470
471       if (pthread_equal (me, p->id))
472         {
473           uintptr_t top = (uintptr_t) m->gc_sp;
474           uintptr_t bottom = (uintptr_t) &top;
475           if (top < bottom)
476             scan (m->gc_sp, bottom - top);
477           else
478             scan ((void *) bottom, top - bottom);
479         }
480       else
481         {
482           scan (p->m->gc_next_sp, p->m->gc_len);
483         }
484         
485 #endif /* !defined(USING_SPLIT_STACK) */
486
487       /* Also scan the M structure while we're at it.  */
488
489       scan ((void *) &p->m, sizeof (void *));
490     }
491 }
492
493 /* Release all the memory caches.  This is called with
494    __go_thread_ids_lock held.  */
495
496 void
497 __go_stealcache (void)
498 {
499   struct __go_thread_id *p;
500
501   for (p = __go_all_thread_ids; p != NULL; p = p->next)
502     runtime_MCache_ReleaseAll (p->m->mcache);
503 }
504
505 /* Gather memory cache statistics.  This is called with
506    __go_thread_ids_lock held.  */
507
508 void
509 __go_cachestats (void)
510 {
511   struct __go_thread_id *p;
512
513   for (p = __go_all_thread_ids; p != NULL; p = p->next)
514     {
515       MCache *c;
516
517       c = p->m->mcache;
518       mstats.heap_alloc += c->local_alloc;
519       c->local_alloc = 0;
520       mstats.heap_objects += c->local_objects;
521       c->local_objects = 0;
522     }
523 }
524
525 /* Start the other threads after garbage collection.  */
526
527 void
528 runtime_starttheworld (void)
529 {
530   int i;
531   pthread_t me;
532   struct __go_thread_id *p;
533
534   /* Here __go_thread_ids_lock should be held.  */
535
536   me = pthread_self ();
537   p = __go_all_thread_ids;
538   while (p != NULL)
539     {
540       if (p->tentative || pthread_equal (me, p->id))
541         p = p->next;
542       else
543         {
544           i = pthread_kill (p->id, GO_SIG_START);
545           if (i == 0)
546             p = p->next;
547           else
548             abort ();
549         }
550     }
551
552   i = pthread_mutex_unlock (&__go_thread_ids_lock);
553   __go_assert (i == 0);
554 }
555
556 /* Initialize the interaction between goroutines and the garbage
557    collector.  */
558
559 void
560 __go_gc_goroutine_init (void *sp __attribute__ ((unused)))
561 {
562   struct __go_thread_id *list_entry;
563   int i;
564   sigset_t sset;
565   struct sigaction act;
566
567   /* Add the initial thread to the list of all threads.  */
568
569   list_entry = malloc (sizeof (struct __go_thread_id));
570   list_entry->prev = NULL;
571   list_entry->next = NULL;
572   list_entry->tentative = 0;
573   list_entry->id = pthread_self ();
574   list_entry->m = m;
575   list_entry->pfn = NULL;
576   list_entry->arg = NULL;
577   __go_all_thread_ids = list_entry;
578
579   /* Initialize the semaphore which signals when threads are ready for
580      GC.  */
581
582   i = sem_init (&__go_thread_ready_sem, 0, 0);
583   __go_assert (i == 0);
584
585   /* Fetch the current signal mask.  */
586
587   i = sigemptyset (&sset);
588   __go_assert (i == 0);
589   i = sigprocmask (SIG_BLOCK, NULL, &sset);
590   __go_assert (i == 0);
591
592   /* Make sure that GO_SIG_START is not blocked and GO_SIG_STOP is
593      blocked, and save that set for use with later calls to sigsuspend
594      while waiting for GC to complete.  */
595
596   i = sigdelset (&sset, GO_SIG_START);
597   __go_assert (i == 0);
598   i = sigaddset (&sset, GO_SIG_STOP);
599   __go_assert (i == 0);
600   __go_thread_wait_sigset = sset;
601
602   /* Block SIG_SET_START and unblock SIG_SET_STOP, and use that for
603      the process signal mask.  */
604
605   i = sigaddset (&sset, GO_SIG_START);
606   __go_assert (i == 0);
607   i = sigdelset (&sset, GO_SIG_STOP);
608   __go_assert (i == 0);
609   i = sigprocmask (SIG_SETMASK, &sset, NULL);
610   __go_assert (i == 0);
611
612   /* Install the signal handlers.  */
613   memset (&act, 0, sizeof act);
614   i = sigemptyset (&act.sa_mask);
615   __go_assert (i == 0);
616
617   act.sa_handler = gc_start_handler;
618   act.sa_flags = SA_RESTART;
619   i = sigaction (GO_SIG_START, &act, NULL);
620   __go_assert (i == 0);
621
622   /* We could consider using an alternate signal stack for this.  The
623      function does not use much stack space, so it may be OK.  */
624   act.sa_handler = gc_stop_handler;
625   i = sigaction (GO_SIG_STOP, &act, NULL);
626   __go_assert (i == 0);
627
628 #ifndef USING_SPLIT_STACK
629   /* If we don't support split stack, record the current stack as the
630      top of the stack.  */
631   m->gc_sp = sp;
632 #endif
633 }