2018-04-26 Richard Biener <rguenther@suse.de>
[official-gcc.git] / libgo / runtime / go-cgo.c
blobe80b6b519e18c47d916630ccb3f01a70ace88c42
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. */
7 #include "runtime.h"
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. */
20 void
21 _cgo_wait_runtime_init_done (void)
23 int err;
25 if (__atomic_load_n (&runtime_init_done, __ATOMIC_ACQUIRE))
26 return;
28 err = pthread_mutex_lock (&runtime_init_mu);
29 if (err != 0)
30 abort ();
31 while (!__atomic_load_n (&runtime_init_done, __ATOMIC_ACQUIRE))
33 err = pthread_cond_wait (&runtime_init_cond, &runtime_init_mu);
34 if (err != 0)
35 abort ();
37 err = pthread_mutex_unlock (&runtime_init_mu);
38 if (err != 0)
39 abort ();
42 /* This is called by runtime_main after the Go runtime is
43 initialized. */
45 void
46 _cgo_notify_runtime_init_done (void)
48 int err;
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 ();
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;