2016-07-13 Thomas Preud'homme <thomas.preudhomme@arm.com>
[official-gcc.git] / libgo / runtime / go-libmain.c
blob6884f3a5f56d796271ebe70f16f0f46e018c6094
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 "go-alloc.h"
17 #include "array.h"
18 #include "arch.h"
19 #include "malloc.h"
21 /* This is used when building a standalone Go library using the Go
22 command's -buildmode=c-archive or -buildmode=c-shared option. It
23 starts up the Go code as a global constructor but does not take any
24 other action. The main program is written in some other language
25 and calls exported Go functions as needed. */
27 static void die (const char *, int);
28 static void initfn (int, char **, char **);
29 static void *gostart (void *);
31 /* Used to pass arguments to the thread that runs the Go startup. */
33 struct args {
34 int argc;
35 char **argv;
38 /* We use .init_array so that we can get the command line arguments.
39 This obviously assumes .init_array support; different systems may
40 require other approaches. */
42 typedef void (*initarrayfn) (int, char **, char **);
44 static initarrayfn initarray[1]
45 __attribute__ ((section (".init_array"), used)) =
46 { initfn };
48 /* This function is called at program startup time. It starts a new
49 thread to do the actual Go startup, so that program startup is not
50 paused waiting for the Go initialization functions. Exported cgo
51 functions will wait for initialization to complete if
52 necessary. */
54 static void
55 initfn (int argc, char **argv, char** env __attribute__ ((unused)))
57 int err;
58 pthread_attr_t attr;
59 struct args *a;
60 pthread_t tid;
62 runtime_isarchive = true;
64 runtime_initsig(true);
66 a = (struct args *) malloc (sizeof *a);
67 if (a == NULL)
68 die ("malloc", errno);
69 a->argc = argc;
70 a->argv = argv;
72 err = pthread_attr_init (&attr);
73 if (err != 0)
74 die ("pthread_attr_init", err);
75 err = pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
76 if (err != 0)
77 die ("pthread_attr_setdetachstate", err);
79 err = pthread_create (&tid, &attr, gostart, (void *) a);
80 if (err != 0)
81 die ("pthread_create", err);
83 err = pthread_attr_destroy (&attr);
84 if (err != 0)
85 die ("pthread_attr_destroy", err);
88 /* Start up the Go runtime. */
90 static void *
91 gostart (void *arg)
93 struct args *a = (struct args *) arg;
95 if (runtime_isstarted)
96 return NULL;
97 runtime_isstarted = true;
99 runtime_check ();
100 runtime_args (a->argc, (byte **) a->argv);
101 runtime_osinit ();
102 runtime_schedinit ();
103 __go_go (runtime_main, NULL);
104 runtime_mstart (runtime_m ());
105 abort ();
108 /* If something goes wrong during program startup, crash. There is no
109 way to report failure and nobody to whom to report it. */
111 static void
112 die (const char *fn, int err)
114 fprintf (stderr, "%s: %d\n", fn, err);
115 exit (EXIT_FAILURE);