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. */
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. */
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
)) =
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
55 initfn (int argc
, char **argv
, char** env
__attribute__ ((unused
)))
62 a
= (struct args
*) malloc (sizeof *a
);
64 die ("malloc", errno
);
68 err
= pthread_attr_init (&attr
);
70 die ("pthread_attr_init", err
);
71 err
= pthread_attr_setdetachstate (&attr
, PTHREAD_CREATE_DETACHED
);
73 die ("pthread_attr_setdetachstate", err
);
75 err
= pthread_create (&tid
, &attr
, gostart
, (void *) a
);
77 die ("pthread_create", err
);
79 err
= pthread_attr_destroy (&attr
);
81 die ("pthread_attr_destroy", err
);
84 /* Start up the Go runtime. */
89 struct args
*a
= (struct args
*) arg
;
91 runtime_isarchive
= true;
93 if (runtime_isstarted
)
95 runtime_isstarted
= true;
98 runtime_args (a
->argc
, (byte
**) a
->argv
);
100 runtime_schedinit ();
101 __go_go (runtime_main
, NULL
);
102 runtime_mstart (runtime_m ());
106 /* If something goes wrong during program startup, crash. There is no
107 way to report failure and nobody to whom to report it. */
110 die (const char *fn
, int err
)
112 fprintf (stderr
, "%s: %d\n", fn
, err
);