runtime: initialize variable to avoid compiler warning
[official-gcc.git] / libgo / runtime / go-libmain.c
blobf578aab43b7f244b4e66ff1302a6aabc44c264c8
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 a = (struct args *) malloc (sizeof *a);
63 if (a == NULL)
64 die ("malloc", errno);
65 a->argc = argc;
66 a->argv = argv;
68 err = pthread_attr_init (&attr);
69 if (err != 0)
70 die ("pthread_attr_init", err);
71 err = pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
72 if (err != 0)
73 die ("pthread_attr_setdetachstate", err);
75 err = pthread_create (&tid, &attr, gostart, (void *) a);
76 if (err != 0)
77 die ("pthread_create", err);
79 err = pthread_attr_destroy (&attr);
80 if (err != 0)
81 die ("pthread_attr_destroy", err);
84 /* Start up the Go runtime. */
86 static void *
87 gostart (void *arg)
89 struct args *a = (struct args *) arg;
91 runtime_isarchive = true;
93 if (runtime_isstarted)
94 return NULL;
95 runtime_isstarted = true;
97 runtime_check ();
98 runtime_args (a->argc, (byte **) a->argv);
99 runtime_osinit ();
100 runtime_schedinit ();
101 __go_go (runtime_main, NULL);
102 runtime_mstart (runtime_m ());
103 abort ();
106 /* If something goes wrong during program startup, crash. There is no
107 way to report failure and nobody to whom to report it. */
109 static void
110 die (const char *fn, int err)
112 fprintf (stderr, "%s: %d\n", fn, err);
113 exit (EXIT_FAILURE);