github.com/twelsh-aw/go/src@v0.0.0-20230516233729-a56fe86a7c81/runtime/cgo/gcc_libinit.c (about)

     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  #include <pthread.h>
     8  #include <errno.h>
     9  #include <stdio.h>
    10  #include <stdlib.h>
    11  #include <string.h> // strerror
    12  #include <time.h>
    13  #include "libcgo.h"
    14  #include "libcgo_unix.h"
    15  
    16  static pthread_cond_t runtime_init_cond = PTHREAD_COND_INITIALIZER;
    17  static pthread_mutex_t runtime_init_mu = PTHREAD_MUTEX_INITIALIZER;
    18  static int runtime_init_done;
    19  
    20  // The context function, used when tracing back C calls into Go.
    21  static void (*cgo_context_function)(struct context_arg*);
    22  
    23  void
    24  x_cgo_sys_thread_create(void* (*func)(void*), void* arg) {
    25  	pthread_t p;
    26  	int err = _cgo_try_pthread_create(&p, NULL, func, arg);
    27  	if (err != 0) {
    28  		fprintf(stderr, "pthread_create failed: %s", strerror(err));
    29  		abort();
    30  	}
    31  }
    32  
    33  uintptr_t
    34  _cgo_wait_runtime_init_done(void) {
    35  	void (*pfn)(struct context_arg*);
    36  
    37  	pthread_mutex_lock(&runtime_init_mu);
    38  	while (runtime_init_done == 0) {
    39  		pthread_cond_wait(&runtime_init_cond, &runtime_init_mu);
    40  	}
    41  
    42  	// TODO(iant): For the case of a new C thread calling into Go, such
    43  	// as when using -buildmode=c-archive, we know that Go runtime
    44  	// initialization is complete but we do not know that all Go init
    45  	// functions have been run. We should not fetch cgo_context_function
    46  	// until they have been, because that is where a call to
    47  	// SetCgoTraceback is likely to occur. We are going to wait for Go
    48  	// initialization to be complete anyhow, later, by waiting for
    49  	// main_init_done to be closed in cgocallbackg1. We should wait here
    50  	// instead. See also issue #15943.
    51  	pfn = cgo_context_function;
    52  
    53  	pthread_mutex_unlock(&runtime_init_mu);
    54  	if (pfn != nil) {
    55  		struct context_arg arg;
    56  
    57  		arg.Context = 0;
    58  		(*pfn)(&arg);
    59  		return arg.Context;
    60  	}
    61  	return 0;
    62  }
    63  
    64  void
    65  x_cgo_notify_runtime_init_done(void* dummy __attribute__ ((unused))) {
    66  	pthread_mutex_lock(&runtime_init_mu);
    67  	runtime_init_done = 1;
    68  	pthread_cond_broadcast(&runtime_init_cond);
    69  	pthread_mutex_unlock(&runtime_init_mu);
    70  }
    71  
    72  // Sets the context function to call to record the traceback context
    73  // when calling a Go function from C code. Called from runtime.SetCgoTraceback.
    74  void x_cgo_set_context_function(void (*context)(struct context_arg*)) {
    75  	pthread_mutex_lock(&runtime_init_mu);
    76  	cgo_context_function = context;
    77  	pthread_mutex_unlock(&runtime_init_mu);
    78  }
    79  
    80  // Gets the context function.
    81  void (*(_cgo_get_context_function(void)))(struct context_arg*) {
    82  	void (*ret)(struct context_arg*);
    83  
    84  	pthread_mutex_lock(&runtime_init_mu);
    85  	ret = cgo_context_function;
    86  	pthread_mutex_unlock(&runtime_init_mu);
    87  	return ret;
    88  }
    89  
    90  // _cgo_try_pthread_create retries pthread_create if it fails with
    91  // EAGAIN.
    92  int
    93  _cgo_try_pthread_create(pthread_t* thread, const pthread_attr_t* attr, void* (*pfn)(void*), void* arg) {
    94  	int tries;
    95  	int err;
    96  	struct timespec ts;
    97  
    98  	for (tries = 0; tries < 20; tries++) {
    99  		err = pthread_create(thread, attr, pfn, arg);
   100  		if (err == 0) {
   101  			pthread_detach(*thread);
   102  			return 0;
   103  		}
   104  		if (err != EAGAIN) {
   105  			return err;
   106  		}
   107  		ts.tv_sec = 0;
   108  		ts.tv_nsec = (tries + 1) * 1000 * 1000; // Milliseconds.
   109  		nanosleep(&ts, nil);
   110  	}
   111  	return EAGAIN;
   112  }