github.com/llvm-mirror/llgo@v0.0.0-20190322182713-bf6f0a60fce1/third_party/gofrontend/libgo/runtime/go-libmain.c (about)

     1  /* go-libmain.c -- the startup function for a Go library.
     2  
     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.  */
     6  
     7  #include "config.h"
     8  
     9  #include <errno.h>
    10  #include <pthread.h>
    11  #include <stdlib.h>
    12  #include <time.h>
    13  #include <unistd.h>
    14  
    15  #include "runtime.h"
    16  #include "go-alloc.h"
    17  #include "array.h"
    18  #include "arch.h"
    19  #include "malloc.h"
    20  
    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.  */
    26  
    27  static void die (const char *, int);
    28  static void initfn (int, char **, char **);
    29  static void *gostart (void *);
    30  
    31  /* Used to pass arguments to the thread that runs the Go startup.  */
    32  
    33  struct args {
    34    int argc;
    35    char **argv;
    36  };
    37  
    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.  */
    41  
    42  typedef void (*initarrayfn) (int, char **, char **);
    43  
    44  static initarrayfn initarray[1]
    45  __attribute__ ((section (".init_array"), used)) =
    46    { initfn };
    47  
    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.  */
    53  
    54  static void
    55  initfn (int argc, char **argv, char** env __attribute__ ((unused)))
    56  {
    57    int err;
    58    pthread_attr_t attr;
    59    struct args *a;
    60    pthread_t tid;
    61  
    62    a = (struct args *) malloc (sizeof *a);
    63    if (a == NULL)
    64      die ("malloc", errno);
    65    a->argc = argc;
    66    a->argv = argv;
    67  
    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);
    74  
    75    err = pthread_create (&tid, &attr, gostart, (void *) a);
    76    if (err != 0)
    77      die ("pthread_create", err);
    78  
    79    err = pthread_attr_destroy (&attr);
    80    if (err != 0)
    81      die ("pthread_attr_destroy", err);
    82  }
    83  
    84  /* Start up the Go runtime.  */
    85  
    86  static void *
    87  gostart (void *arg)
    88  {
    89    struct args *a = (struct args *) arg;
    90  
    91    runtime_isarchive = true;
    92  
    93    if (runtime_isstarted)
    94      return NULL;
    95    runtime_isstarted = true;
    96  
    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 ();
   104  }
   105  
   106  /* If something goes wrong during program startup, crash.  There is no
   107     way to report failure and nobody to whom to report it.  */
   108  
   109  static void
   110  die (const char *fn, int err)
   111  {
   112    fprintf (stderr, "%s: %d\n", fn, err);
   113    exit (EXIT_FAILURE);
   114  }