1 // Copyright 2015 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 //go:build unix
6
7 // When cross-compiling with clang to linux/armv5, atomics are emulated
8 // and cause a compiler warning. This results in a build failure since
9 // cgo uses -Werror. See #65290.
10 #pragma GCC diagnostic ignored "-Wpragmas"
11 #pragma GCC diagnostic ignored "-Wunknown-warning-option"
12 #pragma GCC diagnostic ignored "-Watomic-alignment"
13
14 #include <pthread.h>
15 #include <errno.h>
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h> // strerror
19 #include <time.h>
20 #include "libcgo.h"
21 #include "libcgo_unix.h"
22
23 static pthread_cond_t runtime_init_cond = PTHREAD_COND_INITIALIZER;
24 static pthread_mutex_t runtime_init_mu = PTHREAD_MUTEX_INITIALIZER;
25 static int runtime_init_done;
26
27 // pthread_g is a pthread specific key, for storing the g that binded to the C thread.
28 // The registered pthread_key_destructor will dropm, when the pthread-specified value g is not NULL,
29 // while a C thread is exiting.
30 static pthread_key_t pthread_g;
31 static void pthread_key_destructor(void* g);
32 uintptr_t x_cgo_pthread_key_created;
33 void (*x_crosscall2_ptr)(void (*fn)(void *), void *, int, size_t);
34
35 // The context function, used when tracing back C calls into Go.
36 static void (*cgo_context_function)(struct context_arg*);
37
38 void
39 x_cgo_sys_thread_create(void* (*func)(void*), void* arg) {
40 pthread_attr_t attr;
41 pthread_t p;
42
43 pthread_attr_init(&attr);
44 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
45 int err = _cgo_try_pthread_create(&p, &attr, func, arg);
46 if (err != 0) {
47 fprintf(stderr, "pthread_create failed: %s", strerror(err));
48 abort();
49 }
50 }
51
52 uintptr_t
53 _cgo_wait_runtime_init_done(void) {
54 void (*pfn)(struct context_arg*);
55 pfn = __atomic_load_n(&cgo_context_function, __ATOMIC_CONSUME);
56
57 int done = 2;
58 if (__atomic_load_n(&runtime_init_done, __ATOMIC_CONSUME) != done) {
59 pthread_mutex_lock(&runtime_init_mu);
60 while (__atomic_load_n(&runtime_init_done, __ATOMIC_CONSUME) == 0) {
61 pthread_cond_wait(&runtime_init_cond, &runtime_init_mu);
62 }
63
64 // The key and x_cgo_pthread_key_created are for the whole program,
65 // whereas the specific and destructor is per thread.
66 if (x_cgo_pthread_key_created == 0 && pthread_key_create(&pthread_g, pthread_key_destructor) == 0) {
67 x_cgo_pthread_key_created = 1;
68 }
69
70
71 // TODO(iant): For the case of a new C thread calling into Go, such
72 // as when using -buildmode=c-archive, we know that Go runtime
73 // initialization is complete but we do not know that all Go init
74 // functions have been run. We should not fetch cgo_context_function
75 // until they have been, because that is where a call to
76 // SetCgoTraceback is likely to occur. We are going to wait for Go
77 // initialization to be complete anyhow, later, by waiting for
78 // main_init_done to be closed in cgocallbackg1. We should wait here
79 // instead. See also issue #15943.
80 pfn = __atomic_load_n(&cgo_context_function, __ATOMIC_CONSUME);
81
82 __atomic_store_n(&runtime_init_done, done, __ATOMIC_RELEASE);
83 pthread_mutex_unlock(&runtime_init_mu);
84 }
85
86 if (pfn != nil) {
87 struct context_arg arg;
88
89 arg.Context = 0;
90 (*pfn)(&arg);
91 return arg.Context;
92 }
93 return 0;
94 }
95
96 // _cgo_set_stacklo sets g->stacklo based on the stack size.
97 // This is common code called from x_cgo_init, which is itself
98 // called by rt0_go in the runtime package.
99 void _cgo_set_stacklo(G *g, uintptr *pbounds)
100 {
101 uintptr bounds[2];
102
103 // pbounds can be passed in by the caller; see gcc_linux_amd64.c.
104 if (pbounds == NULL) {
105 pbounds = &bounds[0];
106 }
107
108 x_cgo_getstackbound(pbounds);
109
110 g->stacklo = *pbounds;
111
112 // Sanity check the results now, rather than getting a
113 // morestack on g0 crash.
114 if (g->stacklo >= g->stackhi) {
115 fprintf(stderr, "runtime/cgo: bad stack bounds: lo=%p hi=%p\n", (void*)(g->stacklo), (void*)(g->stackhi));
116 abort();
117 }
118 }
119
120 // Store the g into a thread-specific value associated with the pthread key pthread_g.
121 // And pthread_key_destructor will dropm when the thread is exiting.
122 void x_cgo_bindm(void* g) {
123 // We assume this will always succeed, otherwise, there might be extra M leaking,
124 // when a C thread exits after a cgo call.
125 // We only invoke this function once per thread in runtime.needAndBindM,
126 // and the next calls just reuse the bound m.
127 pthread_setspecific(pthread_g, g);
128 }
129
130 void
131 x_cgo_notify_runtime_init_done(void* dummy __attribute__ ((unused))) {
132 pthread_mutex_lock(&runtime_init_mu);
133 __atomic_store_n(&runtime_init_done, 1, __ATOMIC_RELEASE);
134 pthread_cond_broadcast(&runtime_init_cond);
135 pthread_mutex_unlock(&runtime_init_mu);
136 }
137
138 // Sets the context function to call to record the traceback context
139 // when calling a Go function from C code. Called from runtime.SetCgoTraceback.
140 void x_cgo_set_context_function(void (*context)(struct context_arg*)) {
141 __atomic_store_n(&cgo_context_function, context, __ATOMIC_RELEASE);
142 }
143
144 // Gets the context function.
145 void (*(_cgo_get_context_function(void)))(struct context_arg*) {
146 return __atomic_load_n(&cgo_context_function, __ATOMIC_CONSUME);
147 }
148
149 // _cgo_try_pthread_create retries pthread_create if it fails with
150 // EAGAIN.
151 int
152 _cgo_try_pthread_create(pthread_t* thread, const pthread_attr_t* attr, void* (*pfn)(void*), void* arg) {
153 int tries;
154 int err;
155 struct timespec ts;
156
157 for (tries = 0; tries < 20; tries++) {
158 err = pthread_create(thread, attr, pfn, arg);
159 if (err == 0) {
160 return 0;
161 }
162 if (err != EAGAIN) {
163 return err;
164 }
165 ts.tv_sec = 0;
166 ts.tv_nsec = (tries + 1) * 1000 * 1000; // Milliseconds.
167 nanosleep(&ts, nil);
168 }
169 return EAGAIN;
170 }
171
172 static void
173 pthread_key_destructor(void* g) {
174 if (x_crosscall2_ptr != NULL) {
175 // fn == NULL means dropm.
176 // We restore g by using the stored g, before dropm in runtime.cgocallback,
177 // since the g stored in the TLS by Go might be cleared in some platforms,
178 // before this destructor invoked.
179 x_crosscall2_ptr(NULL, g, 0, 0);
180 }
181 }
182
View as plain text