Retro68/gcc/libgo/runtime/go-cgo.c

65 lines
1.7 KiB
C
Raw Normal View History

2012-03-27 23:13:14 +00:00
/* go-cgo.c -- SWIG support routines for libgo.
Copyright 2011 The Go Authors. All rights reserved.
Use of this source code is governed by a BSD-style
license that can be found in the LICENSE file. */
2014-09-21 17:33:12 +00:00
#include "runtime.h"
2015-08-28 15:33:40 +00:00
/* Used for _cgo_wait_runtime_init_done. This is based on code in
runtime/cgo/gcc_libinit.c in the master library. */
static pthread_cond_t runtime_init_cond = PTHREAD_COND_INITIALIZER;
static pthread_mutex_t runtime_init_mu = PTHREAD_MUTEX_INITIALIZER;
static _Bool runtime_init_done;
2014-09-21 17:33:12 +00:00
2015-08-28 15:33:40 +00:00
/* This is called by exported cgo functions to ensure that the runtime
has been initialized before we enter the function. This is needed
when building with -buildmode=c-archive or similar. */
2014-09-21 17:33:12 +00:00
2015-08-28 15:33:40 +00:00
void
_cgo_wait_runtime_init_done (void)
2014-09-21 17:33:12 +00:00
{
2015-08-28 15:33:40 +00:00
int err;
2014-09-21 17:33:12 +00:00
2015-08-28 15:33:40 +00:00
if (__atomic_load_n (&runtime_init_done, __ATOMIC_ACQUIRE))
return;
err = pthread_mutex_lock (&runtime_init_mu);
if (err != 0)
abort ();
while (!__atomic_load_n (&runtime_init_done, __ATOMIC_ACQUIRE))
{
err = pthread_cond_wait (&runtime_init_cond, &runtime_init_mu);
if (err != 0)
abort ();
}
err = pthread_mutex_unlock (&runtime_init_mu);
if (err != 0)
abort ();
2014-09-21 17:33:12 +00:00
}
2015-08-28 15:33:40 +00:00
/* This is called by runtime_main after the Go runtime is
initialized. */
void
_cgo_notify_runtime_init_done (void)
{
int err;
err = pthread_mutex_lock (&runtime_init_mu);
if (err != 0)
abort ();
__atomic_store_n (&runtime_init_done, 1, __ATOMIC_RELEASE);
err = pthread_cond_broadcast (&runtime_init_cond);
if (err != 0)
abort ();
err = pthread_mutex_unlock (&runtime_init_mu);
if (err != 0)
abort ();
}
// runtime_iscgo is set to true if some cgo code is linked in.
// This is done by a constructor in the cgo generated code.
_Bool runtime_iscgo;