Text file src/runtime/cgo/gcc_freebsd.c

     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  //go:build freebsd && (386 || arm || arm64 || riscv64)
     6  
     7  #include <sys/types.h>
     8  #include <sys/signalvar.h>
     9  #include <machine/sysarch.h>
    10  #include <pthread.h>
    11  #include <signal.h>
    12  #include <string.h>
    13  #include "libcgo.h"
    14  #include "libcgo_unix.h"
    15  
    16  #ifdef ARM_TP_ADDRESS
    17  // ARM_TP_ADDRESS is (ARM_VECTORS_HIGH + 0x1000) or 0xffff1000
    18  // and is known to runtime.read_tls_fallback. Verify it with
    19  // cpp.
    20  #if ARM_TP_ADDRESS != 0xffff1000
    21  #error Wrong ARM_TP_ADDRESS!
    22  #endif
    23  #endif
    24  
    25  static void* threadentry(void*);
    26  static void (*setg_gcc)(void*);
    27  
    28  void
    29  x_cgo_init(G *g, void (*setg)(void*))
    30  {
    31  	setg_gcc = setg;
    32  	_cgo_set_stacklo(g, NULL);
    33  }
    34  
    35  void
    36  _cgo_sys_thread_start(ThreadStart *ts)
    37  {
    38  	pthread_attr_t attr;
    39  	sigset_t ign, oset;
    40  	pthread_t p;
    41  	size_t size;
    42  	int err;
    43  
    44  	SIGFILLSET(ign);
    45  	pthread_sigmask(SIG_SETMASK, &ign, &oset);
    46  
    47  	pthread_attr_init(&attr);
    48  	pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
    49  	pthread_attr_getstacksize(&attr, &size);
    50  	// Leave stacklo=0 and set stackhi=size; mstart will do the rest.
    51  	ts->g->stackhi = size;
    52  	err = _cgo_try_pthread_create(&p, &attr, threadentry, ts);
    53  
    54  	pthread_sigmask(SIG_SETMASK, &oset, nil);
    55  
    56  	if (err != 0) {
    57  		fatalf("pthread_create failed: %s", strerror(err));
    58  	}
    59  }
    60  
    61  extern void crosscall1(void (*fn)(void), void (*setg_gcc)(void*), void *g);
    62  static void*
    63  threadentry(void *v)
    64  {
    65  	ThreadStart ts;
    66  
    67  	ts = *(ThreadStart*)v;
    68  	free(v);
    69  
    70  	crosscall1(ts.fn, setg_gcc, ts.g);
    71  	return nil;
    72  }
    73  

View as plain text