github.com/mangodowner/go-gm@v0.0.0-20180818020936-8baa2bd4408c/src/runtime/cgo/gcc_util.c (about)

     1  // Copyright 2009 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  #include "libcgo.h"
     6  
     7  /* Stub for creating a new thread */
     8  void
     9  x_cgo_thread_start(ThreadStart *arg)
    10  {
    11  	ThreadStart *ts;
    12  
    13  	/* Make our own copy that can persist after we return. */
    14  	_cgo_tsan_acquire();
    15  	ts = malloc(sizeof *ts);
    16  	_cgo_tsan_release();
    17  	if(ts == nil) {
    18  		fprintf(stderr, "runtime/cgo: out of memory in thread_start\n");
    19  		abort();
    20  	}
    21  	*ts = *arg;
    22  
    23  	_cgo_sys_thread_start(ts);	/* OS-dependent half */
    24  }
    25  
    26  #ifndef CGO_TSAN
    27  void(* const _cgo_yield)() = NULL;
    28  #else
    29  
    30  #include <string.h>
    31  
    32  /*
    33  Stub for allowing libc interceptors to execute.
    34  
    35  _cgo_yield is set to NULL if we do not expect libc interceptors to exist.
    36  */
    37  static void
    38  x_cgo_yield()
    39  {
    40  	/*
    41  	The libc function(s) we call here must form a no-op and include at least one
    42  	call that triggers TSAN to process pending asynchronous signals.
    43  
    44  	sleep(0) would be fine, but it's not portable C (so it would need more header
    45  	guards).
    46  	free(NULL) has a fast-path special case in TSAN, so it doesn't
    47  	trigger signal delivery.
    48  	free(malloc(0)) would work (triggering the interceptors in malloc), but
    49  	it also runs a bunch of user-supplied malloc hooks.
    50  
    51  	So we choose strncpy(_, _, 0): it requires an extra header,
    52  	but it's standard and should be very efficient.
    53  	*/
    54  	char nothing = 0;
    55  	strncpy(&nothing, &nothing, 0);
    56  }
    57  
    58  void(* const _cgo_yield)() = &x_cgo_yield;
    59  
    60  #endif  /* GO_TSAN */