go-libmain.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /* go-libmain.c -- the startup function for a Go library.
  2. Copyright 2015 The Go Authors. All rights reserved.
  3. Use of this source code is governed by a BSD-style
  4. license that can be found in the LICENSE file. */
  5. #include "config.h"
  6. #include <errno.h>
  7. #include <pthread.h>
  8. #include <stdlib.h>
  9. #include <time.h>
  10. #include <unistd.h>
  11. #include "runtime.h"
  12. #include "go-alloc.h"
  13. #include "array.h"
  14. #include "arch.h"
  15. #include "malloc.h"
  16. /* This is used when building a standalone Go library using the Go
  17. command's -buildmode=c-archive or -buildmode=c-shared option. It
  18. starts up the Go code as a global constructor but does not take any
  19. other action. The main program is written in some other language
  20. and calls exported Go functions as needed. */
  21. static void die (const char *, int);
  22. static void initfn (int, char **, char **);
  23. static void *gostart (void *);
  24. /* Used to pass arguments to the thread that runs the Go startup. */
  25. struct args {
  26. int argc;
  27. char **argv;
  28. };
  29. /* We use .init_array so that we can get the command line arguments.
  30. This obviously assumes .init_array support; different systems may
  31. require other approaches. */
  32. typedef void (*initarrayfn) (int, char **, char **);
  33. static initarrayfn initarray[1]
  34. __attribute__ ((section (".init_array"), used)) =
  35. { initfn };
  36. /* This function is called at program startup time. It starts a new
  37. thread to do the actual Go startup, so that program startup is not
  38. paused waiting for the Go initialization functions. Exported cgo
  39. functions will wait for initialization to complete if
  40. necessary. */
  41. static void
  42. initfn (int argc, char **argv, char** env __attribute__ ((unused)))
  43. {
  44. int err;
  45. pthread_attr_t attr;
  46. struct args *a;
  47. pthread_t tid;
  48. a = (struct args *) malloc (sizeof *a);
  49. if (a == NULL)
  50. die ("malloc", errno);
  51. a->argc = argc;
  52. a->argv = argv;
  53. err = pthread_attr_init (&attr);
  54. if (err != 0)
  55. die ("pthread_attr_init", err);
  56. err = pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
  57. if (err != 0)
  58. die ("pthread_attr_setdetachstate", err);
  59. err = pthread_create (&tid, &attr, gostart, (void *) a);
  60. if (err != 0)
  61. die ("pthread_create", err);
  62. err = pthread_attr_destroy (&attr);
  63. if (err != 0)
  64. die ("pthread_attr_destroy", err);
  65. }
  66. /* Start up the Go runtime. */
  67. static void *
  68. gostart (void *arg)
  69. {
  70. struct args *a = (struct args *) arg;
  71. runtime_isarchive = true;
  72. if (runtime_isstarted)
  73. return NULL;
  74. runtime_isstarted = true;
  75. runtime_check ();
  76. runtime_args (a->argc, (byte **) a->argv);
  77. runtime_osinit ();
  78. runtime_schedinit ();
  79. __go_go (runtime_main, NULL);
  80. runtime_mstart (runtime_m ());
  81. abort ();
  82. }
  83. /* If something goes wrong during program startup, crash. There is no
  84. way to report failure and nobody to whom to report it. */
  85. static void
  86. die (const char *fn, int err)
  87. {
  88. fprintf (stderr, "%s: %d\n", fn, err);
  89. exit (EXIT_FAILURE);
  90. }