github.com/geraldss/go/src@v0.0.0-20210511222824-ac7d0ebfc235/runtime/cgo/gcc_darwin_amd64.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 <string.h> /* for strerror */
     6  #include <pthread.h>
     7  #include <signal.h>
     8  #include "libcgo.h"
     9  #include "libcgo_unix.h"
    10  
    11  static void* threadentry(void*);
    12  
    13  void
    14  x_cgo_init(G *g)
    15  {
    16  	pthread_attr_t attr;
    17  	size_t size;
    18  
    19  	pthread_attr_init(&attr);
    20  	pthread_attr_getstacksize(&attr, &size);
    21  	g->stacklo = (uintptr)&attr - size + 4096;
    22  	pthread_attr_destroy(&attr);
    23  }
    24  
    25  
    26  void
    27  _cgo_sys_thread_start(ThreadStart *ts)
    28  {
    29  	pthread_attr_t attr;
    30  	sigset_t ign, oset;
    31  	pthread_t p;
    32  	size_t size;
    33  	int err;
    34  
    35  	sigfillset(&ign);
    36  	pthread_sigmask(SIG_SETMASK, &ign, &oset);
    37  
    38  	pthread_attr_init(&attr);
    39  	pthread_attr_getstacksize(&attr, &size);
    40  	// Leave stacklo=0 and set stackhi=size; mstart will do the rest.
    41  	ts->g->stackhi = size;
    42  	err = _cgo_try_pthread_create(&p, &attr, threadentry, ts);
    43  
    44  	pthread_sigmask(SIG_SETMASK, &oset, nil);
    45  
    46  	if (err != 0) {
    47  		fprintf(stderr, "runtime/cgo: pthread_create failed: %s\n", strerror(err));
    48  		abort();
    49  	}
    50  }
    51  
    52  static void*
    53  threadentry(void *v)
    54  {
    55  	ThreadStart ts;
    56  
    57  	ts = *(ThreadStart*)v;
    58  	free(v);
    59  
    60  	// Move the g pointer into the slot reserved in thread local storage.
    61  	// Constant must match the one in cmd/link/internal/ld/sym.go.
    62  	asm volatile("movq %0, %%gs:0x30" :: "r"(ts.g));
    63  
    64  	crosscall_amd64(ts.fn);
    65  	return nil;
    66  }