| 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 |
int err; |
| 43 |
|
| 44 |
pthread_attr_init(&attr); |
| 45 |
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
| 46 |
err = _cgo_try_pthread_create(&p, &attr, func, arg); |
| 47 |
if (err != 0) { |
| 48 |
fprintf(stderr, "pthread_create failed: %s", strerror(err)); |
| 49 |
abort(); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
uintptr_t |
| 54 |
_cgo_wait_runtime_init_done(void) { |
| 55 |
void (*pfn)(struct context_arg*); |
| 56 |
int done; |
| 57 |
|
| 58 |
pfn = __atomic_load_n(&cgo_context_function, __ATOMIC_CONSUME); |
| 59 |
|
| 60 |
done = 2; |
| 61 |
if (__atomic_load_n(&runtime_init_done, __ATOMIC_CONSUME) != done) { |
| 62 |
pthread_mutex_lock(&runtime_init_mu); |
| 63 |
while (__atomic_load_n(&runtime_init_done, __ATOMIC_CONSUME) == 0) { |
| 64 |
pthread_cond_wait(&runtime_init_cond, &runtime_init_mu); |
| 65 |
} |
| 66 |
|
| 67 |
// The key and x_cgo_pthread_key_created are for the whole program, |
| 68 |
// whereas the specific and destructor is per thread. |
| 69 |
if (x_cgo_pthread_key_created == 0 && pthread_key_create(&pthread_g, pthread_key_destructor) == 0) { |
| 70 |
x_cgo_pthread_key_created = 1; |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
// TODO(iant): For the case of a new C thread calling into Go, such |
| 75 |
// as when using -buildmode=c-archive, we know that Go runtime |
| 76 |
// initialization is complete but we do not know that all Go init |
| 77 |
// functions have been run. We should not fetch cgo_context_function |
| 78 |
// until they have been, because that is where a call to |
| 79 |
// SetCgoTraceback is likely to occur. We are going to wait for Go |
| 80 |
// initialization to be complete anyhow, later, by waiting for |
| 81 |
// main_init_done to be closed in cgocallbackg1. We should wait here |
| 82 |
// instead. See also issue #15943. |
| 83 |
pfn = __atomic_load_n(&cgo_context_function, __ATOMIC_CONSUME); |
| 84 |
|
| 85 |
__atomic_store_n(&runtime_init_done, done, __ATOMIC_RELEASE); |
| 86 |
pthread_mutex_unlock(&runtime_init_mu); |
| 87 |
} |
| 88 |
|
| 89 |
if (pfn != nil) { |
| 90 |
struct context_arg arg; |
| 91 |
|
| 92 |
arg.Context = 0; |
| 93 |
(*pfn)(&arg); |
| 94 |
return arg.Context; |
| 95 |
} |
| 96 |
return 0; |
| 97 |
} |
| 98 |
|
| 99 |
// _cgo_set_stacklo sets g->stacklo based on the stack size. |
| 100 |
// This is common code called from x_cgo_init, which is itself |
| 101 |
// called by rt0_go in the runtime package. |
| 102 |
void _cgo_set_stacklo(G *g, uintptr *pbounds) |
| 103 |
{ |
| 104 |
uintptr bounds[2]; |
| 105 |
|
| 106 |
// pbounds can be passed in by the caller; see gcc_linux_amd64.c. |
| 107 |
if (pbounds == NULL) { |
| 108 |
pbounds = &bounds[0]; |
| 109 |
} |
| 110 |
|
| 111 |
x_cgo_getstackbound(pbounds); |
| 112 |
|
| 113 |
g->stacklo = *pbounds; |
| 114 |
|
| 115 |
// Sanity check the results now, rather than getting a |
| 116 |
// morestack on g0 crash. |
| 117 |
if (g->stacklo >= g->stackhi) { |
| 118 |
fprintf(stderr, "runtime/cgo: bad stack bounds: lo=%p hi=%p\n", (void*)(g->stacklo), (void*)(g->stackhi)); |
| 119 |
abort(); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
// Store the g into a thread-specific value associated with the pthread key pthread_g. |
| 124 |
// And pthread_key_destructor will dropm when the thread is exiting. |
| 125 |
void x_cgo_bindm(void* g) { |
| 126 |
// We assume this will always succeed, otherwise, there might be extra M leaking, |
| 127 |
// when a C thread exits after a cgo call. |
| 128 |
// We only invoke this function once per thread in runtime.needAndBindM, |
| 129 |
// and the next calls just reuse the bound m. |
| 130 |
pthread_setspecific(pthread_g, g); |
| 131 |
} |
| 132 |
|
| 133 |
void |
| 134 |
x_cgo_notify_runtime_init_done(void* dummy __attribute__ ((unused))) { |
| 135 |
pthread_mutex_lock(&runtime_init_mu); |
| 136 |
__atomic_store_n(&runtime_init_done, 1, __ATOMIC_RELEASE); |
| 137 |
pthread_cond_broadcast(&runtime_init_cond); |
| 138 |
pthread_mutex_unlock(&runtime_init_mu); |
| 139 |
} |
| 140 |
|
| 141 |
// Sets the context function to call to record the traceback context |
| 142 |
// when calling a Go function from C code. Called from runtime.SetCgoTraceback. |
| 143 |
void x_cgo_set_context_function(void (*context)(struct context_arg*)) { |
| 144 |
__atomic_store_n(&cgo_context_function, context, __ATOMIC_RELEASE); |
| 145 |
} |
| 146 |
|
| 147 |
// Gets the context function. |
| 148 |
void (*(_cgo_get_context_function(void)))(struct context_arg*) { |
| 149 |
return __atomic_load_n(&cgo_context_function, __ATOMIC_CONSUME); |
| 150 |
} |
| 151 |
|
| 152 |
// _cgo_try_pthread_create retries pthread_create if it fails with |
| 153 |
// EAGAIN. |
| 154 |
int |
| 155 |
_cgo_try_pthread_create(pthread_t* thread, const pthread_attr_t* attr, void* (*pfn)(void*), void* arg) { |
| 156 |
int tries; |
| 157 |
int err; |
| 158 |
struct timespec ts; |
| 159 |
|
| 160 |
for (tries = 0; tries < 20; tries++) { |
| 161 |
err = pthread_create(thread, attr, pfn, arg); |
| 162 |
if (err == 0) { |
| 163 |
return 0; |
| 164 |
} |
| 165 |
if (err != EAGAIN) { |
| 166 |
return err; |
| 167 |
} |
| 168 |
ts.tv_sec = 0; |
| 169 |
ts.tv_nsec = (tries + 1) * 1000 * 1000; // Milliseconds. |
| 170 |
nanosleep(&ts, nil); |
| 171 |
} |
| 172 |
return EAGAIN; |
| 173 |
} |
| 174 |
|
| 175 |
static void |
| 176 |
pthread_key_destructor(void* g) { |
| 177 |
if (x_crosscall2_ptr != NULL) { |
| 178 |
// fn == NULL means dropm. |
| 179 |
// We restore g by using the stored g, before dropm in runtime.cgocallback, |
| 180 |
// since the g stored in the TLS by Go might be cleared in some platforms, |
| 181 |
// before this destructor invoked. |
| 182 |
x_crosscall2_ptr(NULL, g, 0, 0); |
| 183 |
} |
| 184 |
} |