Text file src/runtime/cgo/gcc_libinit_unix.c

     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  
    20  static pthread_cond_t runtime_init_cond = PTHREAD_COND_INITIALIZER;
    21  static pthread_mutex_t runtime_init_mu = PTHREAD_MUTEX_INITIALIZER;
    22  static 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.
    27  static pthread_key_t pthread_g;
    28  static void pthread_key_destructor(void* g);
    29  uintptr_t x_cgo_pthread_key_created;
    30  void (*x_crosscall2_ptr)(void (*fn)(void *), void *, int, size_t);
    31  
    32  // The traceback function, used when tracing C calls.
    33  static void (*cgo_traceback_function)(struct cgoTracebackArg*);
    34  
    35  // The context function, used when tracing back C calls into Go.
    36  static void (*cgo_context_function)(struct cgoContextArg*);
    37  
    38  // The symbolizer function, used when symbolizing C frames.
    39  static void (*cgo_symbolizer_function)(struct cgoSymbolizerArg*);
    40  
    41  uintptr_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.
    88  void 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  
    96  void (* _cgo_bindm)(void*) = x_cgo_bindm;
    97  
    98  void
    99  x_cgo_notify_runtime_init_done(void* dummy __attribute__ ((unused))) {
   100  	pthread_mutex_lock(&runtime_init_mu);
   101  	__atomic_store_n(&runtime_init_done, 1, __ATOMIC_RELEASE);
   102  	pthread_cond_broadcast(&runtime_init_cond);
   103  	pthread_mutex_unlock(&runtime_init_mu);
   104  }
   105  
   106  // Sets the traceback, context, and symbolizer functions. Called from
   107  // runtime.SetCgoTraceback.
   108  void x_cgo_set_traceback_functions(struct cgoSetTracebackFunctionsArg* arg) {
   109  	__atomic_store_n(&cgo_traceback_function, arg->Traceback, __ATOMIC_RELEASE);
   110  	__atomic_store_n(&cgo_context_function, arg->Context, __ATOMIC_RELEASE);
   111  	__atomic_store_n(&cgo_symbolizer_function, arg->Symbolizer, __ATOMIC_RELEASE);
   112  }
   113  
   114  // Gets the traceback function to call to trace C calls.
   115  void (*(_cgo_get_traceback_function(void)))(struct cgoTracebackArg*) {
   116  	return __atomic_load_n(&cgo_traceback_function, __ATOMIC_CONSUME);
   117  }
   118  
   119  // Call the traceback function registered with x_cgo_set_traceback_functions.
   120  //
   121  // The traceback function is an arbitrary user C function which may be built
   122  // with TSAN, and thus must be wrapped with TSAN acquire/release calls. For
   123  // normal cgo calls, cmd/cgo automatically inserts TSAN acquire/release calls.
   124  // Since the traceback, context, and symbolizer functions are registered at
   125  // startup and called via the runtime, they do not get automatic TSAN
   126  // acquire/release calls.
   127  //
   128  // The only purpose of this wrapper is to perform TSAN acquire/release.
   129  // Alternatively, if the runtime arranged to safely call TSAN acquire/release,
   130  // it could perform the call directly.
   131  void x_cgo_call_traceback_function(struct cgoTracebackArg* arg) {
   132  	void (*pfn)(struct cgoTracebackArg*);
   133  
   134  	pfn = _cgo_get_traceback_function();
   135  	if (pfn == nil) {
   136  		return;
   137  	}
   138  
   139  	_cgo_tsan_acquire();
   140  	(*pfn)(arg);
   141  	_cgo_tsan_release();
   142  }
   143  
   144  // Gets the context function to call to record the traceback context
   145  // when calling a Go function from C code.
   146  void (*(_cgo_get_context_function(void)))(struct cgoContextArg*) {
   147  	return __atomic_load_n(&cgo_context_function, __ATOMIC_CONSUME);
   148  }
   149  
   150  // Gets the symbolizer function to call to symbolize C frames.
   151  void (*(_cgo_get_symbolizer_function(void)))(struct cgoSymbolizerArg*) {
   152  	return __atomic_load_n(&cgo_symbolizer_function, __ATOMIC_CONSUME);
   153  }
   154  
   155  // Call the symbolizer function registered with x_cgo_set_traceback_functions.
   156  //
   157  // See comment on x_cgo_call_traceback_function.
   158  void x_cgo_call_symbolizer_function(struct cgoSymbolizerArg* arg) {
   159  	void (*pfn)(struct cgoSymbolizerArg*);
   160  
   161  	pfn = _cgo_get_symbolizer_function();
   162  	if (pfn == nil) {
   163  		return;
   164  	}
   165  
   166  	_cgo_tsan_acquire();
   167  	(*pfn)(arg);
   168  	_cgo_tsan_release();
   169  }
   170  
   171  static void
   172  pthread_key_destructor(void* g) {
   173  	if (x_crosscall2_ptr != NULL) {
   174  		// fn == NULL means dropm.
   175  		// We restore g by using the stored g, before dropm in runtime.cgocallback,
   176  		// since the g stored in the TLS by Go might be cleared in some platforms,
   177  		// before this destructor invoked.
   178  		x_crosscall2_ptr(NULL, g, 0, 0);
   179  	}
   180  }
   181  
   182  void
   183  x_cgo_thread_start(ThreadStart *arg)
   184  {
   185  	ThreadStart *ts;
   186  
   187  	/* Make our own copy that can persist after we return. */
   188  	ts = malloc(sizeof *ts);
   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  }
   197  
   198  void (* _cgo_thread_start)(ThreadStart*) = x_cgo_thread_start;
   199  

View as plain text