github.com/guyezi/gofrontend@v0.0.0-20200228202240-7a62a49e62c0/libgo/runtime/go-cgo.c (about)

     1  /* go-cgo.c -- SWIG support routines for libgo.
     2  
     3     Copyright 2011 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 "runtime.h"
     8  
     9  /* Used for _cgo_wait_runtime_init_done.  This is based on code in
    10     runtime/cgo/gcc_libinit.c in the master library.  */
    11  
    12  static pthread_cond_t runtime_init_cond = PTHREAD_COND_INITIALIZER;
    13  static pthread_mutex_t runtime_init_mu = PTHREAD_MUTEX_INITIALIZER;
    14  static _Bool runtime_init_done;
    15  
    16  /* This is called by exported cgo functions to ensure that the runtime
    17     has been initialized before we enter the function.  This is needed
    18     when building with -buildmode=c-archive or similar.  */
    19  
    20  void
    21  _cgo_wait_runtime_init_done (void)
    22  {
    23    int err;
    24  
    25    if (__atomic_load_n (&runtime_init_done, __ATOMIC_ACQUIRE))
    26      return;
    27  
    28    err = pthread_mutex_lock (&runtime_init_mu);
    29    if (err != 0)
    30      abort ();
    31    while (!__atomic_load_n (&runtime_init_done, __ATOMIC_ACQUIRE))
    32      {
    33        err = pthread_cond_wait (&runtime_init_cond, &runtime_init_mu);
    34        if (err != 0)
    35  	abort ();
    36      }
    37    err = pthread_mutex_unlock (&runtime_init_mu);
    38    if (err != 0)
    39      abort ();
    40  }
    41  
    42  /* This is called by runtime_main after the Go runtime is
    43     initialized.  */
    44  
    45  void
    46  _cgo_notify_runtime_init_done (void)
    47  {
    48    int err;
    49  
    50    err = pthread_mutex_lock (&runtime_init_mu);
    51    if (err != 0)
    52      abort ();
    53    __atomic_store_n (&runtime_init_done, 1, __ATOMIC_RELEASE);
    54    err = pthread_cond_broadcast (&runtime_init_cond);
    55    if (err != 0)
    56      abort ();
    57    err = pthread_mutex_unlock (&runtime_init_mu);
    58    if (err != 0)
    59      abort ();
    60  }
    61  
    62  // runtime_iscgo is set to true if some cgo code is linked in.
    63  // This is done by a constructor in the cgo generated code.
    64  _Bool runtime_iscgo;