1 /* go-cgo.c -- SWIG support routines for libgo.
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. */
9 /* Used for _cgo_wait_runtime_init_done. This is based on code in
10 runtime/cgo/gcc_libinit.c in the master library. */
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
;
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. */
21 _cgo_wait_runtime_init_done (void)
25 if (__atomic_load_n (&runtime_init_done
, __ATOMIC_ACQUIRE
))
28 err
= pthread_mutex_lock (&runtime_init_mu
);
31 while (!__atomic_load_n (&runtime_init_done
, __ATOMIC_ACQUIRE
))
33 err
= pthread_cond_wait (&runtime_init_cond
, &runtime_init_mu
);
37 err
= pthread_mutex_unlock (&runtime_init_mu
);
42 /* This is called by runtime_main after the Go runtime is
46 _cgo_notify_runtime_init_done (void)
50 err
= pthread_mutex_lock (&runtime_init_mu
);
53 __atomic_store_n (&runtime_init_done
, 1, __ATOMIC_RELEASE
);
54 err
= pthread_cond_broadcast (&runtime_init_cond
);
57 err
= pthread_mutex_unlock (&runtime_init_mu
);
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.