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. */
20 /* This is used when building a standalone Go library using the Go
21 command's -buildmode=c-archive or -buildmode=c-shared option. It
22 starts up the Go code as a global constructor but does not take any
23 other action. The main program is written in some other language
24 and calls exported Go functions as needed. */
26 static void die (const char *, int);
27 static void initfn (int, char **, char **);
28 static void *gostart (void *);
30 /* Used to pass arguments to the thread that runs the Go startup. */
37 /* We use .init_array so that we can get the command line arguments.
38 This obviously assumes .init_array support; different systems may
39 require other approaches. */
41 typedef void (*initarrayfn
) (int, char **, char **);
43 static initarrayfn initarray
[1]
44 __attribute__ ((section (".init_array"), used
)) =
47 /* This function is called at program startup time. It starts a new
48 thread to do the actual Go startup, so that program startup is not
49 paused waiting for the Go initialization functions. Exported cgo
50 functions will wait for initialization to complete if
54 initfn (int argc
, char **argv
, char** env
__attribute__ ((unused
)))
61 runtime_isarchive
= true;
65 runtime_initsig(true);
67 a
= (struct args
*) malloc (sizeof *a
);
69 die ("malloc", errno
);
73 err
= pthread_attr_init (&attr
);
75 die ("pthread_attr_init", err
);
76 err
= pthread_attr_setdetachstate (&attr
, PTHREAD_CREATE_DETACHED
);
78 die ("pthread_attr_setdetachstate", err
);
80 err
= pthread_create (&tid
, &attr
, gostart
, (void *) a
);
82 die ("pthread_create", err
);
84 err
= pthread_attr_destroy (&attr
);
86 die ("pthread_attr_destroy", err
);
89 /* Start up the Go runtime. */
94 struct args
*a
= (struct args
*) arg
;
96 if (runtime_isstarted
)
98 runtime_isstarted
= true;
101 runtime_args (a
->argc
, (byte
**) a
->argv
);
103 runtime_schedinit ();
104 __go_go (runtime_main
, NULL
);
105 runtime_mstart (runtime_m ());
109 /* If something goes wrong during program startup, crash. There is no
110 way to report failure and nobody to whom to report it. */
113 die (const char *fn
, int err
)
115 fprintf (stderr
, "%s: %d\n", fn
, err
);