...

Text file src/runtime/cgo/gcc_libinit_unix.c

Documentation: runtime/cgo

     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 <stdio.h>
    16#include <stdlib.h>
    17#include "libcgo.h"
    18#include "libcgo_unix.h"
    19
    20static pthread_cond_t runtime_init_cond = PTHREAD_COND_INITIALIZER;
    21static pthread_mutex_t runtime_init_mu = PTHREAD_MUTEX_INITIALIZER;
    22static int runtime_init_done;
    23
    24// pthread_g is a pthread specific key, for storing the g that binded to the C thread.
    25// The registered pthread_key_destructor will dropm, when the pthread-specified value g is not NULL,
    26// while a C thread is exiting.
    27static pthread_key_t pthread_g;
    28static void pthread_key_destructor(void* g);
    29uintptr_t x_cgo_pthread_key_created;
    30void (*x_crosscall2_ptr)(void (*fn)(void *), void *, int, size_t);
    31
    32// The traceback function, used when tracing C calls.
    33static void (*cgo_traceback_function)(struct cgoTracebackArg*);
    34
    35// The context function, used when tracing back C calls into Go.
    36static void (*cgo_context_function)(struct cgoContextArg*);
    37
    38// The symbolizer function, used when symbolizing C frames.
    39static void (*cgo_symbolizer_function)(struct cgoSymbolizerArg*);
    40
    41uintptr_t
    42_cgo_wait_runtime_init_done(void) {
    43	void (*pfn)(struct cgoContextArg*);
    44	int done;
    45
    46	pfn = __atomic_load_n(&cgo_context_function, __ATOMIC_CONSUME);
    47
    48	done = 2;
    49	if (__atomic_load_n(&runtime_init_done, __ATOMIC_CONSUME) != done) {
    50		pthread_mutex_lock(&runtime_init_mu);
    51		while (__atomic_load_n(&runtime_init_done, __ATOMIC_CONSUME) == 0) {
    52			pthread_cond_wait(&runtime_init_cond, &runtime_init_mu);
    53		}
    54
    55		// The key and x_cgo_pthread_key_created are for the whole program,
    56		// whereas the specific and destructor is per thread.
    57		if (x_cgo_pthread_key_created == 0 && pthread_key_create(&pthread_g, pthread_key_destructor) == 0) {
    58			x_cgo_pthread_key_created = 1;
    59		}
    60
    61		// TODO(iant): For the case of a new C thread calling into Go, such
    62		// as when using -buildmode=c-archive, we know that Go runtime
    63		// initialization is complete but we do not know that all Go init
    64		// functions have been run. We should not fetch cgo_context_function
    65		// until they have been, because that is where a call to
    66		// SetCgoTraceback is likely to occur. We are going to wait for Go
    67		// initialization to be complete anyhow, later, by waiting for
    68		// main_init_done to be closed in cgocallbackg1. We should wait here
    69		// instead. See also issue #15943.
    70		pfn = __atomic_load_n(&cgo_context_function, __ATOMIC_CONSUME);
    71
    72		__atomic_store_n(&runtime_init_done, done, __ATOMIC_RELEASE);
    73		pthread_mutex_unlock(&runtime_init_mu);
    74	}
    75
    76	if (pfn != nil) {
    77		struct cgoContextArg arg;
    78
    79		arg.Context = 0;
    80		(*pfn)(&arg);
    81		return arg.Context;
    82	}
    83	return 0;
    84}
    85
    86// Store the g into a thread-specific value associated with the pthread key pthread_g.
    87// And pthread_key_destructor will dropm when the thread is exiting.
    88void x_cgo_bindm(void* g) {
    89	// We assume this will always succeed, otherwise, there might be extra M leaking,
    90	// when a C thread exits after a cgo call.
    91	// We only invoke this function once per thread in runtime.needAndBindM,
    92	// and the next calls just reuse the bound m.
    93	pthread_setspecific(pthread_g, g);
    94}
    95
    96void
    97x_cgo_notify_runtime_init_done(void* dummy __attribute__ ((unused))) {
    98	pthread_mutex_lock(&runtime_init_mu);
    99	__atomic_store_n(&runtime_init_done, 1, __ATOMIC_RELEASE);
   100	pthread_cond_broadcast(&runtime_init_cond);
   101	pthread_mutex_unlock(&runtime_init_mu);
   102}
   103
   104// Sets the traceback, context, and symbolizer functions. Called from
   105// runtime.SetCgoTraceback.
   106void x_cgo_set_traceback_functions(struct cgoSetTracebackFunctionsArg* arg) {
   107	__atomic_store_n(&cgo_traceback_function, arg->Traceback, __ATOMIC_RELEASE);
   108	__atomic_store_n(&cgo_context_function, arg->Context, __ATOMIC_RELEASE);
   109	__atomic_store_n(&cgo_symbolizer_function, arg->Symbolizer, __ATOMIC_RELEASE);
   110}
   111
   112// Gets the traceback function to call to trace C calls.
   113void (*(_cgo_get_traceback_function(void)))(struct cgoTracebackArg*) {
   114	return __atomic_load_n(&cgo_traceback_function, __ATOMIC_CONSUME);
   115}
   116
   117// Call the traceback function registered with x_cgo_set_traceback_functions.
   118//
   119// The traceback function is an arbitrary user C function which may be built
   120// with TSAN, and thus must be wrapped with TSAN acquire/release calls. For
   121// normal cgo calls, cmd/cgo automatically inserts TSAN acquire/release calls.
   122// Since the traceback, context, and symbolizer functions are registered at
   123// startup and called via the runtime, they do not get automatic TSAN
   124// acquire/release calls.
   125//
   126// The only purpose of this wrapper is to perform TSAN acquire/release.
   127// Alternatively, if the runtime arranged to safely call TSAN acquire/release,
   128// it could perform the call directly.
   129void x_cgo_call_traceback_function(struct cgoTracebackArg* arg) {
   130	void (*pfn)(struct cgoTracebackArg*);
   131
   132	pfn = _cgo_get_traceback_function();
   133	if (pfn == nil) {
   134		return;
   135	}
   136
   137	_cgo_tsan_acquire();
   138	(*pfn)(arg);
   139	_cgo_tsan_release();
   140}
   141
   142// Gets the context function to call to record the traceback context
   143// when calling a Go function from C code.
   144void (*(_cgo_get_context_function(void)))(struct cgoContextArg*) {
   145	return __atomic_load_n(&cgo_context_function, __ATOMIC_CONSUME);
   146}
   147
   148// Gets the symbolizer function to call to symbolize C frames.
   149void (*(_cgo_get_symbolizer_function(void)))(struct cgoSymbolizerArg*) {
   150	return __atomic_load_n(&cgo_symbolizer_function, __ATOMIC_CONSUME);
   151}
   152
   153// Call the symbolizer function registered with x_cgo_set_traceback_functions.
   154//
   155// See comment on x_cgo_call_traceback_function.
   156void x_cgo_call_symbolizer_function(struct cgoSymbolizerArg* arg) {
   157	void (*pfn)(struct cgoSymbolizerArg*);
   158
   159	pfn = _cgo_get_symbolizer_function();
   160	if (pfn == nil) {
   161		return;
   162	}
   163
   164	_cgo_tsan_acquire();
   165	(*pfn)(arg);
   166	_cgo_tsan_release();
   167}
   168
   169static void
   170pthread_key_destructor(void* g) {
   171	if (x_crosscall2_ptr != NULL) {
   172		// fn == NULL means dropm.
   173		// We restore g by using the stored g, before dropm in runtime.cgocallback,
   174		// since the g stored in the TLS by Go might be cleared in some platforms,
   175		// before this destructor invoked.
   176		x_crosscall2_ptr(NULL, g, 0, 0);
   177	}
   178}
   179
   180void
   181x_cgo_thread_start(ThreadStart *arg)
   182{
   183	ThreadStart *ts;
   184
   185	/* Make our own copy that can persist after we return. */
   186	_cgo_tsan_acquire();
   187	ts = malloc(sizeof *ts);
   188	_cgo_tsan_release();
   189	if(ts == nil) {
   190		fprintf(stderr, "runtime/cgo: out of memory in thread_start\n");
   191		abort();
   192	}
   193	*ts = *arg;
   194
   195	_cgo_sys_thread_start(ts);	/* OS-dependent half */
   196}

View as plain text