i386: move alignment defaults to processor_costs.
[official-gcc.git] / libgo / runtime / go-libmain.c
blob5e3b8d9e48ad2d1222b38304b723f94fc4ac0fd9
1 /* go-libmain.c -- the startup function for a Go library.
3 Copyright 2015 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. */
7 #include "config.h"
9 #include <errno.h>
10 #include <pthread.h>
11 #include <stdlib.h>
12 #include <time.h>
13 #include <unistd.h>
15 #include "runtime.h"
16 #include "array.h"
17 #include "arch.h"
19 /* This is used when building a standalone Go library using the Go
20 command's -buildmode=c-archive or -buildmode=c-shared option. It
21 starts up the Go code as a global constructor but does not take any
22 other action. The main program is written in some other language
23 and calls exported Go functions as needed. */
25 static void die (const char *, int);
26 /* .init_array section does not exist in AIX XCOFF.
27 -Wl,-binitfini:__go_init option will be required to build go
28 libraries and make sure __go_init is called when the library is
29 loaded. This requires __go_init to be exported. */
31 void __go_init (int, char **, char **);
32 static void *gostart (void *);
34 /* Used to pass arguments to the thread that runs the Go startup. */
36 struct args {
37 int argc;
38 char **argv;
41 #ifndef _AIX
42 /* We use .init_array so that we can get the command line arguments.
43 This obviously assumes .init_array support; different systems may
44 require other approaches. */
46 typedef void (*initarrayfn) (int, char **, char **);
48 static initarrayfn initarray[1]
49 __attribute__ ((section (".init_array"), used)) =
50 { __go_init };
51 #endif
53 /* This function is called at program startup time. It starts a new
54 thread to do the actual Go startup, so that program startup is not
55 paused waiting for the Go initialization functions. Exported cgo
56 functions will wait for initialization to complete if
57 necessary. */
59 void
60 __go_init (int argc, char **argv, char** env __attribute__ ((unused)))
62 int err;
63 pthread_attr_t attr;
64 struct args *a;
65 pthread_t tid;
67 runtime_isarchive = true;
69 setIsCgo ();
70 runtime_cpuinit ();
71 runtime_initsig(true);
73 a = (struct args *) malloc (sizeof *a);
74 if (a == NULL)
75 die ("malloc", errno);
76 a->argc = argc;
77 a->argv = argv;
79 err = pthread_attr_init (&attr);
80 if (err != 0)
81 die ("pthread_attr_init", err);
82 err = pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
83 if (err != 0)
84 die ("pthread_attr_setdetachstate", err);
86 err = pthread_create (&tid, &attr, gostart, (void *) a);
87 if (err != 0)
88 die ("pthread_create", err);
90 err = pthread_attr_destroy (&attr);
91 if (err != 0)
92 die ("pthread_attr_destroy", err);
95 /* Start up the Go runtime. */
97 static void *
98 gostart (void *arg)
100 struct args *a = (struct args *) arg;
102 if (runtime_isstarted)
103 return NULL;
104 runtime_isstarted = true;
106 runtime_check ();
107 runtime_args (a->argc, (byte **) a->argv);
108 setncpu (getproccount ());
109 setpagesize (getpagesize ());
110 runtime_sched = runtime_getsched();
111 runtime_schedinit ();
112 __go_go (runtime_main, NULL);
113 runtime_mstart (runtime_m ());
114 abort ();
117 /* If something goes wrong during program startup, crash. There is no
118 way to report failure and nobody to whom to report it. */
120 static void
121 die (const char *fn, int err)
123 fprintf (stderr, "%s: %d\n", fn, err);
124 exit (EXIT_FAILURE);